Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
PropertyCollection.hpp
Go to the documentation of this file.
1//
2// Created by robin on 22.03.2020.
3//
4
5#ifndef TILESON_PROPERTYCOLLECTION_HPP
6#define TILESON_PROPERTYCOLLECTION_HPP
7
8#include "Property.hpp"
9//#include "../external/json.hpp"
10#include <map>
11
12namespace tson
13{
15 {
16 public:
17 inline PropertyCollection() = default;
18
19 inline explicit PropertyCollection(std::string id);
20
21 inline tson::Property * add(const tson::Property &property);
22 inline tson::Property * add(IJson &json, tson::Project *project = nullptr);
23 inline tson::Property * add(const std::string &name, const std::any &value, tson::Type type);
24
25 inline void remove(const std::string &name);
26
27 inline void setValue(const std::string &name, const std::any &value);
28 inline void setProperty(const std::string &name, const tson::Property &value);
29 inline void setId(const std::string &id);
30
31 inline bool hasProperty(const std::string &name);
32 inline tson::Property * getProperty(const std::string &name);
33 inline std::map<std::string, Property> &getProperties();
34 inline std::vector<Property*> get();
35 template <typename T>
36 inline T getValue(const std::string &name);
37 [[nodiscard]] inline const std::string &getId() const;
38 [[nodiscard]] inline size_t getSize() const;
39
40 protected:
41 std::string m_id;
42 std::map<std::string, tson::Property> m_properties;
43 };
44}
45
46template<typename T>
47T tson::PropertyCollection::getValue(const std::string &name)
48{
49 static T defaultT;
50 return (m_properties.count(name) > 0) ? m_properties[name].getValue<T>() : defaultT;
51}
52
53tson::PropertyCollection::PropertyCollection(std::string id) : m_id {std::move(id)}
54{
55
56}
57
59{
60 m_properties[property.getName()] = property;
61 return &m_properties[property.getName()];
62}
63
65{
66 tson::Property property = tson::Property(json, project);
67 const std::string name = property.getName();
68 m_properties[name] = std::move(property);
69 return &m_properties[name];
70}
71
72tson::Property *tson::PropertyCollection::add(const std::string &name, const std::any &value, tson::Type type)
73{
74 m_properties[name] = {name, value, type};
75 return &m_properties[name];
76}
77
78void tson::PropertyCollection::remove(const std::string &name)
79{
80 m_properties.erase(name);
81}
82
89void tson::PropertyCollection::setValue(const std::string &name, const std::any &value)
90{
91 if(m_properties.count(name) > 0)
92 m_properties[name].setValue(value);
93}
94
100void tson::PropertyCollection::setProperty(const std::string &name, const tson::Property &value)
101{
102 m_properties[name] = value;
103}
104
105void tson::PropertyCollection::setId(const std::string &id)
106{
107 m_id = id;
108}
109
110bool tson::PropertyCollection::hasProperty(const std::string &name)
111{
112 return m_properties.count(name) > 0;
113}
114
116{
117 return (m_properties.count(name) > 0) ? &m_properties[name] : nullptr;
118}
119
120std::map<std::string, tson::Property> &tson::PropertyCollection::getProperties()
121{
122 return m_properties;
123}
124
129std::vector<tson::Property *> tson::PropertyCollection::get()
130{
131 std::vector<tson::Property *> props;
132 for(auto &i : m_properties)
133 props.emplace_back(&i.second);
134
135 return props;
136}
137
138
139const std::string &tson::PropertyCollection::getId() const
140{
141 return m_id;
142}
143
145{
146 return m_properties.size();
147}
148
149#endif //TILESON_PROPERTYCOLLECTION_HPP
Definition IJson.hpp:11
Definition Project.hpp:20
Definition PropertyCollection.hpp:15
tson::Property * add(const tson::Property &property)
Definition PropertyCollection.hpp:58
void remove(const std::string &name)
Definition PropertyCollection.hpp:78
std::map< std::string, Property > & getProperties()
Definition PropertyCollection.hpp:120
T getValue(const std::string &name)
Definition PropertyCollection.hpp:47
std::string m_id
Definition PropertyCollection.hpp:41
void setProperty(const std::string &name, const tson::Property &value)
Definition PropertyCollection.hpp:100
std::vector< Property * > get()
Definition PropertyCollection.hpp:129
void setValue(const std::string &name, const std::any &value)
Definition PropertyCollection.hpp:89
bool hasProperty(const std::string &name)
Definition PropertyCollection.hpp:110
std::map< std::string, tson::Property > m_properties
Definition PropertyCollection.hpp:42
tson::Property * getProperty(const std::string &name)
Definition PropertyCollection.hpp:115
const std::string & getId() const
Definition PropertyCollection.hpp:139
void setId(const std::string &id)
Definition PropertyCollection.hpp:105
size_t getSize() const
Definition PropertyCollection.hpp:144
Definition Property.hpp:23
void setValue(const std::any &value)
Definition Property.hpp:95
Definition Base64.hpp:12
Type
Definition Enums.hpp:16