Tileson  1.3.0
A helpful json parser for Tiled maps
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 
12 namespace 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);
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 setId(const std::string &id);
29 
30  inline bool hasProperty(const std::string &name);
31  inline tson::Property * getProperty(const std::string &name);
32  inline std::map<std::string, Property> &getProperties();
33  inline std::vector<Property*> get();
34  template <typename T>
35  inline T getValue(const std::string &name);
36  [[nodiscard]] inline const std::string &getId() const;
37  [[nodiscard]] inline size_t getSize() const;
38 
39  protected:
40  std::string m_id;
41  std::map<std::string, tson::Property> m_properties;
42  };
43 }
44 
45 template<typename T>
46 T tson::PropertyCollection::getValue(const std::string &name)
47 {
48  static T defaultT;
49  return (m_properties.count(name) > 0) ? m_properties[name].getValue<T>() : defaultT;
50 }
51 
52 tson::PropertyCollection::PropertyCollection(std::string id) : m_id {std::move(id)}
53 {
54 
55 }
56 
58 {
59  m_properties[property.getName()] = property;
60  return &m_properties[property.getName()];
61 }
62 
64 {
65  tson::Property property = tson::Property(json);
66  std::string name = property.getName();
67  m_properties[name] = std::move(property);
68  return &m_properties[name];
69 }
70 
71 tson::Property *tson::PropertyCollection::add(const std::string &name, const std::any &value, tson::Type type)
72 {
73  m_properties[name] = {name, value, type};
74  return &m_properties[name];
75 }
76 
77 void tson::PropertyCollection::remove(const std::string &name)
78 {
79  m_properties.erase(name);
80 }
81 
88 void tson::PropertyCollection::setValue(const std::string &name, const std::any &value)
89 {
90  if(m_properties.count(name) > 0)
91  m_properties[name].setValue(value);
92 }
93 
94 void tson::PropertyCollection::setId(const std::string &id)
95 {
96  m_id = id;
97 }
98 
99 bool tson::PropertyCollection::hasProperty(const std::string &name)
100 {
101  return m_properties.count(name) > 0;
102 }
103 
105 {
106  return (m_properties.count(name) > 0) ? &m_properties[name] : nullptr;
107 }
108 
109 std::map<std::string, tson::Property> &tson::PropertyCollection::getProperties()
110 {
111  return m_properties;
112 }
113 
118 std::vector<tson::Property *> tson::PropertyCollection::get()
119 {
120  std::vector<tson::Property *> props;
121  for(auto &i : m_properties)
122  props.emplace_back(&i.second);
123 
124  return props;
125 }
126 
127 
128 const std::string &tson::PropertyCollection::getId() const
129 {
130  return m_id;
131 }
132 
134 {
135  return m_properties.size();
136 }
137 
138 #endif //TILESON_PROPERTYCOLLECTION_HPP
Definition: IJson.hpp:11
Definition: PropertyCollection.hpp:15
tson::Property * add(const tson::Property &property)
Definition: PropertyCollection.hpp:57
void remove(const std::string &name)
Definition: PropertyCollection.hpp:77
std::map< std::string, Property > & getProperties()
Definition: PropertyCollection.hpp:109
T getValue(const std::string &name)
Definition: PropertyCollection.hpp:46
std::string m_id
Definition: PropertyCollection.hpp:40
std::vector< Property * > get()
Definition: PropertyCollection.hpp:118
void setValue(const std::string &name, const std::any &value)
Definition: PropertyCollection.hpp:88
bool hasProperty(const std::string &name)
Definition: PropertyCollection.hpp:99
std::map< std::string, tson::Property > m_properties
Definition: PropertyCollection.hpp:41
tson::Property * getProperty(const std::string &name)
Definition: PropertyCollection.hpp:104
const std::string & getId() const
Definition: PropertyCollection.hpp:128
void setId(const std::string &id)
Definition: PropertyCollection.hpp:94
size_t getSize() const
Definition: PropertyCollection.hpp:133
Definition: Property.hpp:21
Definition: Base64.hpp:12
Type
Definition: Enums.hpp:16