Luanti 5.11.0-dev
 
Loading...
Searching...
No Matches
metadata.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irr_v3d.h"
8#include <iostream>
9#include <vector>
10#include "util/string.h"
11
12// Basic metadata interface
14{
15public:
16 virtual ~IMetadata() = default;
17
18 virtual void clear() = 0;
19
20 bool operator==(const IMetadata &other) const;
21 inline bool operator!=(const IMetadata &other) const
22 {
23 return !(*this == other);
24 }
25
26 //
27 // Key-value related
28 //
29
30 virtual bool contains(const std::string &name) const = 0;
31
32 // May (not must!) put a string in `place` and return a reference to that string.
33 const std::string &getString(const std::string &name, std::string *place,
34 u16 recursion = 0) const;
35
36 // If the entry is present, puts the value in str and returns true;
37 // otherwise just returns false.
38 bool getStringToRef(const std::string &name, std::string &str, u16 recursion = 0) const;
39
40 // Returns whether the metadata was (potentially) changed.
41 virtual bool setString(const std::string &name, std::string_view var) = 0;
42
43 inline bool removeString(const std::string &name) { return setString(name, ""); }
44
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
48 // May (not must!) put keys in `place` and return a reference to these keys.
49 virtual const std::vector<std::string> &getKeys(std::vector<std::string> *place) const = 0;
50
51 // Add support for variable names in values. Uses place like getString.
52 const std::string &resolveString(const std::string &str, std::string *place,
53 u16 recursion = 0, bool deprecated = false) const;
54
55protected:
56 // Returns nullptr to indicate absence of value. Uses place like getString.
57 virtual const std::string *getStringRaw(const std::string &name,
58 std::string *place) const = 0;
59};
60
61// Simple metadata parent class (in-memory storage)
62class SimpleMetadata: public virtual IMetadata
63{
64 bool m_modified = false;
65public:
66 virtual ~SimpleMetadata() = default;
67
68 virtual void clear() override;
69 virtual bool empty() const;
70
71 //
72 // Key-value related
73 //
74
75 size_t size() const;
76 bool contains(const std::string &name) const override;
77 virtual bool setString(const std::string &name, std::string_view var) override;
78 const StringMap &getStrings(StringMap *) const override final;
79 const std::vector<std::string> &getKeys(std::vector<std::string> *place)
80 const override final;
81
82 // Simple version of getters, possible due to in-memory storage:
83
84 inline const std::string &getString(const std::string &name, u16 recursion = 0) const
85 {
86 return IMetadata::getString(name, nullptr, recursion);
87 }
88
89 inline const std::string &resolveString(const std::string &str, u16 recursion = 0) const
90 {
91 return IMetadata::resolveString(str, nullptr, recursion);
92 }
93
94 inline const StringMap &getStrings() const
95 {
96 return SimpleMetadata::getStrings(nullptr);
97 }
98
99 inline bool isModified() const { return m_modified; }
100 inline void setModified(bool v) { m_modified = v; }
101
102protected:
104
105 const std::string *getStringRaw(const std::string &name,
106 std::string *) const override final;
107};
Definition metadata.h:14
const std::string & getString(const std::string &name, std::string *place, u16 recursion=0) const
Definition metadata.cpp:30
virtual const std::string * getStringRaw(const std::string &name, std::string *place) const =0
bool operator!=(const IMetadata &other) const
Definition metadata.h:21
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:55
bool removeString(const std::string &name)
Definition metadata.h:43
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:12
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:42
Definition metadata.h:63
virtual void clear() override
Definition metadata.cpp:75
const StringMap & getStrings() const
Definition metadata.h:94
virtual bool empty() const
Definition metadata.cpp:81
void setModified(bool v)
Definition metadata.h:100
virtual bool setString(const std::string &name, std::string_view var) override
Definition metadata.cpp:116
const std::string & resolveString(const std::string &str, u16 recursion=0) const
Definition metadata.h:89
const std::vector< std::string > & getKeys(std::vector< std::string > *place) const override final
Definition metadata.cpp:101
size_t size() const
Definition metadata.cpp:86
bool contains(const std::string &name) const override
Definition metadata.cpp:91
bool isModified() const
Definition metadata.h:99
const std::string & getString(const std::string &name, u16 recursion=0) const
Definition metadata.h:84
virtual ~SimpleMetadata()=default
const std::string * getStringRaw(const std::string &name, std::string *) const override final
Definition metadata.cpp:110
bool m_modified
Definition metadata.h:64
StringMap m_stringvars
Definition metadata.h:103
std::unordered_map< std::string, std::string > StringMap
Definition string.h:65