-
Notifications
You must be signed in to change notification settings - Fork 9
/
itemattributes.h
104 lines (85 loc) · 3.43 KB
/
itemattributes.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
////////////////////////////////////////////////////////////////////////
// OpenTibia - an opensource roleplaying game
////////////////////////////////////////////////////////////////////////
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
////////////////////////////////////////////////////////////////////////
#ifndef __ITEM_ATTRIBUTES__
#define __ITEM_ATTRIBUTES__
#include "otsystem.h"
#include <boost/any.hpp>
class PropWriteStream;
class PropStream;
class ItemAttribute
{
public:
ItemAttribute() {}
ItemAttribute(const ItemAttribute& o) {*this = o;}
~ItemAttribute() {}
ItemAttribute(const std::string& s) {m_data = s;}
ItemAttribute(int32_t i) {m_data = i;}
ItemAttribute(float f) {m_data = f;}
ItemAttribute(bool b) {m_data = b;}
ItemAttribute& operator=(const ItemAttribute& o);
void serialize(PropWriteStream& stream) const;
bool unserialize(PropStream& stream);
void set(const std::string& s) { m_data = s; }
void set(int32_t i) { m_data = i; }
void set(float f) { m_data = f; }
void set(bool b) { m_data = b; }
void set(boost::any a) { m_data = a; }
std::string getString(bool &ok) const;
int32_t getInteger(bool &ok) const;
float getFloat(bool &ok) const;
bool getBoolean(bool &ok) const;
boost::any get() const { return m_data; }
private:
enum Type
{
NONE = 0,
STRING = 1,
INTEGER = 2,
FLOAT = 3,
BOOLEAN = 4
};
boost::any m_data;
};
class ItemAttributes
{
public:
ItemAttributes(): attributes(NULL) {}
ItemAttributes(const ItemAttributes &i);
virtual ~ItemAttributes() {delete attributes;}
void serializeMap(PropWriteStream& stream) const;
bool unserializeMap(PropStream& stream);
void eraseAttribute(const char* key);
void setAttribute(const char* key, boost::any value);
boost::any getAttribute(const char* key) const;
void setAttribute(const char* key, const std::string& value);
void setAttribute(const char* key, int32_t value);
void setAttribute(const char* key, float value);
void setAttribute(const char* key, bool value);
std::string getStringAttribute(const std::string& key, bool &ok) const;
int32_t getIntegerAttribute(const std::string& key, bool &ok) const;
float getFloatAttribute(const std::string& key, bool &ok) const;
bool getBooleanAttribute(const std::string& key, bool &ok) const;
bool hasStringAttribute(const std::string& key) const { bool ok; getStringAttribute(key, ok); return ok; }
bool hasIntegerAttribute(const std::string& key) const { bool ok; getIntegerAttribute(key, ok); return ok; }
bool hasFloatAttribute(const std::string& key) const { bool ok; getFloatAttribute(key, ok); return ok; }
bool hasBooleanAttribute(const std::string& key) const { bool ok; getBooleanAttribute(key, ok); return ok; }
protected:
void createAttributes();
typedef std::map<std::string, ItemAttribute> AttributeMap;
AttributeMap* attributes;
};
#endif