item_attributes.h

main 116 lines · 3.4 KB Raw
Alex Alex Commit Map Editor 18/10/2026 23:40
1//////////////////////////////////////////////////////////////////////
2// This file is part of Remere's Map Editor
3//////////////////////////////////////////////////////////////////////
4// Remere's Map Editor is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Remere's Map Editor is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16//////////////////////////////////////////////////////////////////////
17
18#ifndef RME_ITEM_ATTRIBUTES_H_
19#define RME_ITEM_ATTRIBUTES_H_
20
21#include <string>
22#include <map>
23
24#include "filehandle.h"
25
26#include <boost/static_assert.hpp>
27
28class IOMap;
29class ItemAttribute;
30
31class PropWriteStream;
32class PropStream;
33
34class ItemAttribute {
35public:
36 ItemAttribute();
37 ItemAttribute(const std::string& str);
38 ItemAttribute(int32_t i);
39 ItemAttribute(double f);
40 ItemAttribute(bool b);
41 ItemAttribute(const ItemAttribute& o);
42 ItemAttribute& operator=(const ItemAttribute& o);
43 ~ItemAttribute();
44
45 enum Type {
46 STRING = 1,
47 INTEGER = 2,
48 FLOAT = 3,
49 BOOLEAN = 4,
50 DOUBLE = 5,
51 NONE = 0
52 } type;
53
54 void serialize(const IOMap& maphandle, NodeFileWriteHandle& f) const;
55 bool unserialize(const IOMap& maphandle, BinaryNode* f);
56
57 void clear();
58
59 void set(const std::string& str);
60 void set(int32_t i);
61 void set(double f);
62 void set(bool b);
63
64 const std::string* getString() const;
65 const int32_t* getInteger() const;
66 const double* getFloat() const;
67 const bool* getBoolean() const;
68
69private:
70 char data[sizeof(std::string) > sizeof(double) ? sizeof(std::string) : sizeof(double)];
71};
72
73typedef std::map<std::string, ItemAttribute> ItemAttributeMap;
74
75class ItemAttributes {
76public:
77 ItemAttributes();
78 ItemAttributes(const ItemAttributes& i);
79 virtual ~ItemAttributes();
80
81 // Save / load
82 void serializeAttributeMap(const IOMap& maphandle, NodeFileWriteHandle& f) const;
83 bool unserializeAttributeMap(const IOMap& maphandle, BinaryNode* node);
84
85public:
86 void setAttribute(const std::string& key, const ItemAttribute& attr);
87 void setAttribute(const std::string& key, const std::string& value);
88 void setAttribute(const std::string& key, int32_t value);
89 void setAttribute(const std::string& key, double value);
90 void setAttribute(const std::string& key, bool set);
91
92 // returns nullptr if the attribute is not set
93 const std::string* getStringAttribute(const std::string& key) const;
94 const int32_t* getIntegerAttribute(const std::string& key) const;
95 const double* getFloatAttribute(const std::string& key) const;
96 const bool* getBooleanAttribute(const std::string& key) const;
97
98 // Returns true if the attribute (of that type) exists
99 bool hasStringAttribute(const std::string& key) const;
100 bool hasIntegerAttribute(const std::string& key) const;
101 bool hasFloatAttribute(const std::string& key) const;
102 bool hasBooleanAttribute(const std::string& key) const;
103
104 void eraseAttribute(const std::string& key);
105
106 void clearAllAttributes();
107 ItemAttributeMap getAttributes() const;
108
109protected:
110 ItemAttributeMap* attributes;
111
112 void createAttributes();
113};
114
115#endif
116
Top