Minetest 5.9.0-dev
 
Loading...
Searching...
No Matches
metadata.h
Go to the documentation of this file.
1/*
2Minetest
3Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU Lesser General Public License as published by
7the Free Software Foundation; either version 2.1 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU Lesser General Public License for more details.
14
15You should have received a copy of the GNU Lesser General Public License along
16with this program; if not, write to the Free Software Foundation, Inc.,
1751 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
29{
30public:
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, std::string_view 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
70protected:
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)
77class SimpleMetadata: public virtual IMetadata
78{
79 bool m_modified = false;
80public:
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, std::string_view 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
117protected:
119
120 const std::string *getStringRaw(const std::string &name,
121 std::string *) const override final;
122};
Definition: metadata.h:29
const std::string & getString(const std::string &name, std::string *place, u16 recursion=0) const
Definition: metadata.cpp:45
virtual const std::string * getStringRaw(const std::string &name, std::string *place) const =0
bool operator!=(const IMetadata &other) const
Definition: metadata.h:36
virtual const StringMap & getStrings(StringMap *place) const =0
const std::string & resolveString(const std::string &str, std::string *place, u16 recursion=0, bool deprecated=false) const
Definition: metadata.cpp:70
bool removeString(const std::string &name)
Definition: metadata.h:58
virtual void clear()=0
virtual ~IMetadata()=default
virtual const std::vector< std::string > & getKeys(std::vector< std::string > *place) const =0
bool operator==(const IMetadata &other) const
Definition: metadata.cpp:27
virtual bool setString(const std::string &name, std::string_view var)=0
virtual bool contains(const std::string &name) const =0
bool getStringToRef(const std::string &name, std::string &str, u16 recursion=0) const
Definition: metadata.cpp:57
Definition: metadata.h:78
virtual void clear() override
Definition: metadata.cpp:90
const StringMap & getStrings() const
Definition: metadata.h:109
virtual bool empty() const
Definition: metadata.cpp:96
void setModified(bool v)
Definition: metadata.h:115
virtual bool setString(const std::string &name, std::string_view var) override
Definition: metadata.cpp:131
const std::string & resolveString(const std::string &str, u16 recursion=0) const
Definition: metadata.h:104
const std::vector< std::string > & getKeys(std::vector< std::string > *place) const override final
Definition: metadata.cpp:116
size_t size() const
Definition: metadata.cpp:101
bool contains(const std::string &name) const override
Definition: metadata.cpp:106
bool isModified() const
Definition: metadata.h:114
const std::string & getString(const std::string &name, u16 recursion=0) const
Definition: metadata.h:99
virtual ~SimpleMetadata()=default
const std::string * getStringRaw(const std::string &name, std::string *) const override final
Definition: metadata.cpp:125
bool m_modified
Definition: metadata.h:79
StringMap m_stringvars
Definition: metadata.h:118
std::unordered_map< std::string, std::string > StringMap
Definition: string.h:78