Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
ProjectPropertyTypes.hpp
Go to the documentation of this file.
1//
2// Created by robin on 01.08.22.
3//
4
5#ifndef TILESON_PROJECTPROPERTYTYPES_HPP
6#define TILESON_PROJECTPROPERTYTYPES_HPP
7
8namespace tson
9{
11 {
12 public:
13 inline ProjectPropertyTypes() = default;
14 inline bool parse(IJson &json, tson::Project *project);
15
16 inline const std::vector<tson::EnumDefinition> &getEnums() const;
17 inline const std::vector<tson::TiledClass> &getClasses() const;
18 [[nodiscard]] inline tson::EnumDefinition* getEnumDefinition(std::string_view name);
19 [[nodiscard]] inline tson::TiledClass* getClass(std::string_view name);
20 inline bool isUnhandledContentFound() const;
21
22 private:
23 std::vector<tson::EnumDefinition> m_enums;
24 std::vector<tson::TiledClass> m_classes;
25 bool m_unhandledContentFound {false};
26
27 };
28
30 {
31 m_enums.clear();
32 m_classes.clear();
33 m_unhandledContentFound = false;
34
35 if(json.count("propertyTypes") > 0 && json["propertyTypes"].isArray())
36 {
37 auto &array = json.array("propertyTypes");
38 std::vector<tson::IJson*> classes; //Classes must be handled after enums
39 std::vector<tson::IJson*> other; //Unhandled stuff - just to keep track if something is missing...
40 std::for_each(array.begin(), array.end(), [&](std::unique_ptr<IJson> &item)
41 {
42 IJson &j = *item;
43 if(j.count("type") > 0)
44 {
45 std::string t = j["type"].get<std::string>();
46 if(t == "enum")
47 {
48 m_enums.emplace_back(j); //Can be resolved directly
49 }
50 else if(t == "class")
51 {
52 classes.push_back(item.get()); //Must be resolved later
53 }
54 else
55 other.push_back(item.get()); //Only used to set flag for whether unhandled content was found.
56 }
57 });
58
59 std::for_each(classes.begin(), classes.end(), [&](IJson *item)
60 {
61 m_classes.emplace_back(*item, project);
62 });
63
64 if(!other.empty())
65 m_unhandledContentFound = true;
66
67 }
68 return false;
69 }
70
71 const std::vector<tson::EnumDefinition> &ProjectPropertyTypes::getEnums() const
72 {
73 return m_enums;
74 }
75
76 const std::vector<tson::TiledClass> &ProjectPropertyTypes::getClasses() const
77 {
78 return m_classes;
79 }
80
81 bool ProjectPropertyTypes::isUnhandledContentFound() const
82 {
83 return m_unhandledContentFound;
84 }
85
86 tson::EnumDefinition *ProjectPropertyTypes::getEnumDefinition(std::string_view name)
87 {
88 auto it = std::find_if(m_enums.begin(), m_enums.end(), [&](const EnumDefinition &def)
89 {
90 return def.getName() == name;
91 });
92
93 if(it != m_enums.end())
94 return &it.operator*();
95
96 return nullptr;
97 }
98
99 tson::TiledClass *ProjectPropertyTypes::getClass(std::string_view name)
100 {
101 auto it = std::find_if(m_classes.begin(), m_classes.end(), [&](const TiledClass &def)
102 {
103 return def.getName() == name;
104 });
105
106 if(it != m_classes.end())
107 return &it.operator*();
108
109 return nullptr;
110 }
111}
112
113#endif //TILESON_PROJECTPROPERTYTYPES_HPP
Definition TiledEnum.hpp:11
Definition IJson.hpp:11
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 ProjectPropertyTypes.hpp:11
const std::vector< tson::EnumDefinition > & getEnums() const
Definition ProjectPropertyTypes.hpp:71
const std::vector< tson::TiledClass > & getClasses() const
Definition ProjectPropertyTypes.hpp:76
tson::EnumDefinition * getEnumDefinition(std::string_view name)
Definition ProjectPropertyTypes.hpp:86
tson::TiledClass * getClass(std::string_view name)
Definition ProjectPropertyTypes.hpp:99
bool isUnhandledContentFound() const
Definition ProjectPropertyTypes.hpp:81
bool parse(IJson &json, tson::Project *project)
Definition ProjectPropertyTypes.hpp:29
Definition Project.hpp:20
Definition TiledClass.hpp:11
Definition Base64.hpp:12