Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
TiledClass.hpp
Go to the documentation of this file.
1//
2// Created by robin on 06.06.22.
3//
4
5#ifndef TILESON_TILEDCLASS_HPP
6#define TILESON_TILEDCLASS_HPP
7
8namespace tson
9{
11 {
12 public:
13 inline explicit TiledClass() = default;
14 inline explicit TiledClass(IJson &json, tson::Project *project = nullptr);
15
16 [[nodiscard]] inline uint32_t getId() const;
17 [[nodiscard]] inline const std::string &getName() const;
18 [[nodiscard]] inline const std::string &getType() const;
19 [[nodiscard]] inline PropertyCollection &getMembers();
20 inline void update(IJson &json);
21 inline void update(PropertyCollection &properties);
22
23 template <typename T>
24 inline T get(const std::string &name);
25 inline tson::Property *getMember(const std::string &name);
26
27 private:
28 uint32_t m_id {};
29 std::string m_name {};
30 std::string m_type {};
31 PropertyCollection m_members {};
32
33 };
34
36 {
37 if(json.count("id") > 0)
38 m_id = json["id"].get<uint32_t>();
39
40 if(json.count("name") > 0)
41 m_name = json["name"].get<std::string>();
42 if(json.count("type") > 0)
43 m_type = json["type"].get<std::string>();
44
45 if(json.count("members") > 0 && json["members"].isArray())
46 {
47 auto &array = json.array("members");
48 std::for_each(array.begin(), array.end(), [&](std::unique_ptr<IJson> &item)
49 {
50 m_members.add(*item, project);
51 });
52 }
53 }
54
55 uint32_t TiledClass::getId() const
56 {
57 return m_id;
58 }
59
60 const std::string &TiledClass::getName() const
61 {
62 return m_name;
63 }
64
65 const std::string &TiledClass::getType() const
66 {
67 return m_type;
68 }
69
71 {
72 return m_members;
73 }
74
75 template<typename T>
76 T TiledClass::get(const std::string &name)
77 {
78 return m_members.getValue<T>(name);
79 }
80
81 tson::Property *TiledClass::getMember(const std::string &name)
82 {
83 if(m_members.hasProperty(name))
84 return m_members.getProperty(name);
85 return nullptr;
86 }
87
93 {
94 for(Property *property : m_members.get())
95 {
96 if(json.any(property->getName()))
97 {
98 property->setValueByType(json[property->getName()]);
99 }
100 }
101 }
102
104 {
105 std::vector<Property *> toUpdate;
106 for(Property *member : m_members.get())
107 {
108 if(properties.hasProperty(member->getName()))
109 {
110 Property *property = properties.getProperty(member->getName());
111 if(member->getType() == property->getType())
112 {
113 toUpdate.push_back(property);
114 }
115 }
116 }
117
118 std::for_each(toUpdate.begin(), toUpdate.end(), [&](Property *p)
119 {
120 m_members.setProperty(p->getName(), *p);
121 });
122 }
123}
124
125#endif //TILESON_TILEDCLASS_HPP
Definition IJson.hpp:11
virtual bool any(std::string_view key) const =0
T get(std::string_view key)
Definition IJson.hpp:82
virtual bool isArray() const =0
virtual size_t count(std::string_view key) const =0
virtual std::vector< std::unique_ptr< IJson > > array()=0
Definition Project.hpp:20
Definition PropertyCollection.hpp:15
T getValue(const std::string &name)
Definition PropertyCollection.hpp:47
std::vector< Property * > get()
Definition PropertyCollection.hpp:129
bool hasProperty(const std::string &name)
Definition PropertyCollection.hpp:110
tson::Property * getProperty(const std::string &name)
Definition PropertyCollection.hpp:115
Definition Property.hpp:23
void setValueByType(IJson &json)
Definition tileson_forward.hpp:603
Definition TiledClass.hpp:11
PropertyCollection & getMembers()
Definition TiledClass.hpp:70
TiledClass()=default
const std::string & getName() const
Definition TiledClass.hpp:60
tson::Property * getMember(const std::string &name)
Definition TiledClass.hpp:81
T get(const std::string &name)
Definition TiledClass.hpp:76
void update(IJson &json)
Definition TiledClass.hpp:92
const std::string & getType() const
Definition TiledClass.hpp:65
uint32_t getId() const
Definition TiledClass.hpp:55
Definition Base64.hpp:12