]> git.lizzy.rs Git - minetest.git/blob - src/metadata.h
Disable Prometheus in singleplayer mode
[minetest.git] / src / metadata.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #pragma once
21
22 #include "irr_v3d.h"
23 #include <iostream>
24 #include <vector>
25 #include "util/string.h"
26
27 // Basic metadata interface
28 class IMetadata
29 {
30 public:
31         virtual ~IMetadata() = default;
32
33         virtual void clear() = 0;
34
35         bool operator==(const IMetadata &other) const;
36         inline bool operator!=(const IMetadata &other) const
37         {
38                 return !(*this == other);
39         }
40
41         //
42         // Key-value related
43         //
44
45         virtual bool contains(const std::string &name) const = 0;
46
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;
50
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;
54
55         // Returns whether the metadata was (potentially) changed.
56         virtual bool setString(const std::string &name, const std::string &var) = 0;
57
58         inline bool removeString(const std::string &name) { return setString(name, ""); }
59
60         // May (not must!) put strings in `place` and return a reference to these strings.
61         virtual const StringMap &getStrings(StringMap *place) const = 0;
62
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;
65
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;
69
70 protected:
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;
74 };
75
76 // Simple metadata parent class (in-memory storage)
77 class SimpleMetadata: public virtual IMetadata
78 {
79         bool m_modified = false;
80 public:
81         virtual ~SimpleMetadata() = default;
82
83         virtual void clear() override;
84         virtual bool empty() const;
85
86         //
87         // Key-value related
88         //
89
90         size_t size() 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)
95                 const override final;
96
97         // Simple version of getters, possible due to in-memory storage:
98
99         inline const std::string &getString(const std::string &name, u16 recursion = 0) const
100         {
101                 return IMetadata::getString(name, nullptr, recursion);
102         }
103
104         inline const std::string &resolveString(const std::string &str, u16 recursion = 0) const
105         {
106                 return IMetadata::resolveString(str, nullptr, recursion);
107         }
108
109         inline const StringMap &getStrings() const
110         {
111                 return SimpleMetadata::getStrings(nullptr);
112         }
113
114         inline bool isModified() const  { return m_modified; }
115         inline void setModified(bool v) { m_modified = v; }
116
117 protected:
118         StringMap m_stringvars;
119
120         const std::string *getStringRaw(const std::string &name,
121                         std::string *) const override final;
122 };