diff options
| author | Jude Melton-Houghton <jwmhjwmh@gmail.com> | 2022-09-26 17:03:43 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-09-26 17:03:43 -0400 |
| commit | f4a01f3a5dc0d8fe2f4f6d804d790da91d1bc30c (patch) | |
| tree | 089ddc309e22ae8549ec3b86765ee272f57a33df /src/metadata.h | |
| parent | 03428d9825cfdf2cfaed6ac9410dafccac0d4f3a (diff) | |
| download | minetest-f4a01f3a5dc0d8fe2f4f6d804d790da91d1bc30c.tar.xz | |
Avoid duplication of mod metadata in memory (#12562)
Co-authored-by: sfan5 <sfan5@live.de>
Diffstat (limited to 'src/metadata.h')
| -rw-r--r-- | src/metadata.h | 84 |
1 files changed, 69 insertions, 15 deletions
diff --git a/src/metadata.h b/src/metadata.h index 5333f8a9d..df61b3f7a 100644 --- a/src/metadata.h +++ b/src/metadata.h @@ -24,17 +24,16 @@ with this program; if not, write to the Free Software Foundation, Inc., #include <vector> #include "util/string.h" -class Metadata +// Basic metadata interface +class IMetadata { - bool m_modified = false; public: - virtual ~Metadata() = default; + virtual ~IMetadata() = default; - virtual void clear(); - virtual bool empty() const; + virtual void clear() = 0; - bool operator==(const Metadata &other) const; - inline bool operator!=(const Metadata &other) const + bool operator==(const IMetadata &other) const; + inline bool operator!=(const IMetadata &other) const { return !(*this == other); } @@ -43,21 +42,76 @@ public: // Key-value related // - size_t size() const; - bool contains(const std::string &name) const; - const std::string &getString(const std::string &name, u16 recursion = 0) const; + virtual bool contains(const std::string &name) const = 0; + + // May (not must!) put a string in `place` and return a reference to that string. + const std::string &getString(const std::string &name, std::string *place, + u16 recursion = 0) const; + + // If the entry is present, puts the value in str and returns true; + // otherwise just returns false. bool getStringToRef(const std::string &name, std::string &str, u16 recursion = 0) const; - virtual bool setString(const std::string &name, const std::string &var); + + // Returns whether the metadata was (potentially) changed. + virtual bool setString(const std::string &name, const std::string &var) = 0; + inline bool removeString(const std::string &name) { return setString(name, ""); } - const StringMap &getStrings() const + + // May (not must!) put strings in `place` and return a reference to these strings. + virtual const StringMap &getStrings(StringMap *place) const = 0; + + // Add support for variable names in values. Uses place like getString. + const std::string &resolveString(const std::string &str, std::string *place, + u16 recursion = 0) const; + +protected: + // Returns nullptr to indicate absence of value. Uses place like getString. + virtual const std::string *getStringRaw(const std::string &name, + std::string *place) const = 0; +}; + +// Simple metadata parent class (in-memory storage) +class SimpleMetadata: public virtual IMetadata +{ + bool m_modified = false; +public: + virtual ~SimpleMetadata() = default; + + virtual void clear() override; + virtual bool empty() const; + + // + // Key-value related + // + + size_t size() const; + bool contains(const std::string &name) const override; + virtual bool setString(const std::string &name, const std::string &var) override; + const StringMap &getStrings(StringMap *) const override final; + + // Simple version of getters, possible due to in-memory storage: + + inline const std::string &getString(const std::string &name, u16 recursion = 0) const { - return m_stringvars; + return IMetadata::getString(name, nullptr, recursion); + } + + inline const std::string &resolveString(const std::string &str, u16 recursion = 0) const + { + return IMetadata::resolveString(str, nullptr, recursion); + } + + inline const StringMap &getStrings() const + { + return SimpleMetadata::getStrings(nullptr); } - // Add support for variable names in values - const std::string &resolveString(const std::string &str, u16 recursion = 0) const; inline bool isModified() const { return m_modified; } inline void setModified(bool v) { m_modified = v; } + protected: StringMap m_stringvars; + + const std::string *getStringRaw(const std::string &name, + std::string *) const override final; }; |
