metadata.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irr_v3d.h"
  18. #include <iostream>
  19. #include <vector>
  20. #include "util/string.h"
  21. // Basic metadata interface
  22. class IMetadata
  23. {
  24. public:
  25. virtual ~IMetadata() = default;
  26. virtual void clear() = 0;
  27. bool operator==(const IMetadata &other) const;
  28. inline bool operator!=(const IMetadata &other) const
  29. {
  30. return !(*this == other);
  31. }
  32. //
  33. // Key-value related
  34. //
  35. virtual bool contains(const std::string &name) const = 0;
  36. // May (not must!) put a string in `place` and return a reference to that string.
  37. const std::string &getString(const std::string &name, std::string *place,
  38. u16 recursion = 0) const;
  39. // If the entry is present, puts the value in str and returns true;
  40. // otherwise just returns false.
  41. bool getStringToRef(const std::string &name, std::string &str, u16 recursion = 0) const;
  42. // Returns whether the metadata was (potentially) changed.
  43. virtual bool setString(const std::string &name, std::string_view var) = 0;
  44. inline bool removeString(const std::string &name) { return setString(name, ""); }
  45. // May (not must!) put strings in `place` and return a reference to these strings.
  46. virtual const StringMap &getStrings(StringMap *place) const = 0;
  47. // May (not must!) put keys in `place` and return a reference to these keys.
  48. virtual const std::vector<std::string> &getKeys(std::vector<std::string> *place) const = 0;
  49. // Add support for variable names in values. Uses place like getString.
  50. const std::string &resolveString(const std::string &str, std::string *place,
  51. u16 recursion = 0, bool deprecated = false) const;
  52. protected:
  53. // Returns nullptr to indicate absence of value. Uses place like getString.
  54. virtual const std::string *getStringRaw(const std::string &name,
  55. std::string *place) const = 0;
  56. };
  57. // Simple metadata parent class (in-memory storage)
  58. class SimpleMetadata: public virtual IMetadata
  59. {
  60. bool m_modified = false;
  61. public:
  62. virtual ~SimpleMetadata() = default;
  63. virtual void clear() override;
  64. virtual bool empty() const;
  65. //
  66. // Key-value related
  67. //
  68. size_t size() const;
  69. bool contains(const std::string &name) const override;
  70. virtual bool setString(const std::string &name, std::string_view var) override;
  71. const StringMap &getStrings(StringMap *) const override final;
  72. const std::vector<std::string> &getKeys(std::vector<std::string> *place)
  73. const override final;
  74. // Simple version of getters, possible due to in-memory storage:
  75. inline const std::string &getString(const std::string &name, u16 recursion = 0) const
  76. {
  77. return IMetadata::getString(name, nullptr, recursion);
  78. }
  79. inline const std::string &resolveString(const std::string &str, u16 recursion = 0) const
  80. {
  81. return IMetadata::resolveString(str, nullptr, recursion);
  82. }
  83. inline const StringMap &getStrings() const
  84. {
  85. return SimpleMetadata::getStrings(nullptr);
  86. }
  87. inline bool isModified() const { return m_modified; }
  88. inline void setModified(bool v) { m_modified = v; }
  89. protected:
  90. StringMap m_stringvars;
  91. const std::string *getStringRaw(const std::string &name,
  92. std::string *) const override final;
  93. };