3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "util/string.h"
27 // Basic metadata interface
31 virtual ~IMetadata() = default;
33 virtual void clear() = 0;
35 bool operator==(const IMetadata &other) const;
36 inline bool operator!=(const IMetadata &other) const
38 return !(*this == other);
45 virtual bool contains(const std::string &name) const = 0;
47 // May (not must!) put a string in `place` and return a reference to that string.
48 const std::string &getString(const std::string &name, std::string *place,
49 u16 recursion = 0) const;
51 // If the entry is present, puts the value in str and returns true;
52 // otherwise just returns false.
53 bool getStringToRef(const std::string &name, std::string &str, u16 recursion = 0) const;
55 // Returns whether the metadata was (potentially) changed.
56 virtual bool setString(const std::string &name, const std::string &var) = 0;
58 inline bool removeString(const std::string &name) { return setString(name, ""); }
60 // May (not must!) put strings in `place` and return a reference to these strings.
61 virtual const StringMap &getStrings(StringMap *place) const = 0;
63 // May (not must!) put keys in `place` and return a reference to these keys.
64 virtual const std::vector<std::string> &getKeys(std::vector<std::string> *place) const = 0;
66 // Add support for variable names in values. Uses place like getString.
67 const std::string &resolveString(const std::string &str, std::string *place,
68 u16 recursion = 0, bool deprecated = false) const;
71 // Returns nullptr to indicate absence of value. Uses place like getString.
72 virtual const std::string *getStringRaw(const std::string &name,
73 std::string *place) const = 0;
76 // Simple metadata parent class (in-memory storage)
77 class SimpleMetadata: public virtual IMetadata
79 bool m_modified = false;
81 virtual ~SimpleMetadata() = default;
83 virtual void clear() override;
84 virtual bool empty() const;
91 bool contains(const std::string &name) const override;
92 virtual bool setString(const std::string &name, const std::string &var) override;
93 const StringMap &getStrings(StringMap *) const override final;
94 const std::vector<std::string> &getKeys(std::vector<std::string> *place)
97 // Simple version of getters, possible due to in-memory storage:
99 inline const std::string &getString(const std::string &name, u16 recursion = 0) const
101 return IMetadata::getString(name, nullptr, recursion);
104 inline const std::string &resolveString(const std::string &str, u16 recursion = 0) const
106 return IMetadata::resolveString(str, nullptr, recursion);
109 inline const StringMap &getStrings() const
111 return SimpleMetadata::getStrings(nullptr);
114 inline bool isModified() const { return m_modified; }
115 inline void setModified(bool v) { m_modified = v; }
118 StringMap m_stringvars;
120 const std::string *getStringRaw(const std::string &name,
121 std::string *) const override final;