Tileson 1.4.0
A helpful json parser for Tiled maps
Loading...
Searching...
No Matches
Property.hpp
Go to the documentation of this file.
1//
2// Created by robin on 22.03.2020.
3//
4
5#ifndef TILESON_PROPERTY_HPP
6#define TILESON_PROPERTY_HPP
7
8//#include "../../TilesonConfig.h"
9
10//#if USE_CPP17_FILESYSTEM
11
12#include <any>
13#include <string>
14#include "../common/Enums.hpp"
15//#include "../external/json.hpp"
16#include "Color.hpp"
17
18namespace tson
19{
20 class Project;
21
23 {
24 public:
25 inline Property();
26 inline explicit Property(IJson &json, tson::Project *project = nullptr);
27 inline Property(std::string name, std::any value, Type type);
28
29 inline void setValue(const std::any &value);
30 inline void setStrValue(const std::string &value);
31 inline void setName(const std::string &name);
32
33 [[nodiscard]] inline const std::type_info& getValueType() const;
34 inline std::string getValueTypeInfo();
35 [[nodiscard]]inline const std::any &getValue() const;
36 template <typename T>
37 inline T getValue() const;
38 [[nodiscard]] inline const std::string &getName() const;
39 [[nodiscard]] inline Type getType() const;
40 [[nodiscard]] inline const std::string &getPropertyType() const;
41
42 //Became public in v1.4.0
43 inline void setValueByType(IJson &json); //Definition in tileson_forward.hpp
44
45 protected:
46 inline void setTypeByString(const std::string &str);
47
48 tson::Project *m_project = nullptr; //Used for resolving 'enum' and 'class' objects
50 std::string m_name {};
51 std::string m_propertyType {};
52 std::any m_value; //Using std::any to assign any type
53 };
54
55 template<typename T>
57 {
58 bool isCorrectType = (m_value.type() == typeid(T));
59
60 if(isCorrectType)
61 {
62 T value = std::any_cast<T>(m_value);
63 return value;
64 }
65 else
66 {
67 static T defaultValue;
68 return defaultValue;
69 }
70 }
71}
72
73tson::Property::Property() : m_name {"unnamed"}
74{
75
76}
77
78tson::Property::Property(IJson &json, tson::Project *project) : m_project {project}
79{
80 m_name = json["name"].get<std::string>();
81 if(json.count("propertytype") > 0)
82 m_propertyType = json["propertytype"].get<std::string>();
83 else if(json.count("propertyType") > 0) //Somehow Tiled's class objects uses propertyType with 'T'.
84 m_propertyType = json["propertyType"].get<std::string>();
85
86 setTypeByString(json["type"].get<std::string>());
87 setValueByType(json["value"]);
88}
89
90tson::Property::Property(std::string name, std::any value, Type type) : m_type {type}, m_name { std::move(name) }, m_value { std::move(value) }
91{
92
93}
94
95void tson::Property::setValue(const std::any &value)
96{
97 m_value = value;
98}
99
106void tson::Property::setStrValue(const std::string &value)
107{
108 m_value = value;
109}
110
111const std::any &tson::Property::getValue() const
112{
113 return m_value;
114}
115
116void tson::Property::setName(const std::string &name)
117{
118 m_name = name;
119}
120
121const std::string &tson::Property::getName() const
122{
123 return m_name;
124}
125
133const std::type_info &tson::Property::getValueType() const
134{
135 return m_value.type();
136}
137
147{
148 return m_value.type().name();
149}
150
152{
153 return m_type;
154}
155
156void tson::Property::setTypeByString(const std::string &str)
157{
158 if(str == "color")
159 m_type = tson::Type::Color;
160 else if(str == "file")
161 m_type = tson::Type::File;
162 else if(str == "int")
163 m_type = tson::Type::Int;
164 else if(str == "bool")
165 m_type = tson::Type::Boolean;
166 else if(str == "float")
167 m_type = tson::Type::Float;
168 else if(str == "string")
169 m_type = tson::Type::String;
170 else if(str == "class")
171 m_type = tson::Type::Class;
172 else if(str == "object")
173 m_type = tson::Type::Object;
174 else
175 m_type = tson::Type::Undefined;
176}
177
178const std::string &tson::Property::getPropertyType() const
179{
180 return m_propertyType;
181}
182
183#endif //TILESON_PROPERTY_HPP
Definition IJson.hpp:11
T get(std::string_view key)
Definition IJson.hpp:82
virtual size_t count(std::string_view key) const =0
Definition Project.hpp:20
Definition Property.hpp:23
Property()
Definition Property.hpp:73
const std::string & getPropertyType() const
Definition Property.hpp:178
void setTypeByString(const std::string &str)
Definition Property.hpp:156
std::any m_value
Definition Property.hpp:52
void setValue(const std::any &value)
Definition Property.hpp:95
std::string getValueTypeInfo()
Definition Property.hpp:146
const std::any & getValue() const
Definition Property.hpp:111
tson::Project * m_project
Definition Property.hpp:48
const std::string & getName() const
Definition Property.hpp:121
Type m_type
Definition Property.hpp:49
void setValueByType(IJson &json)
Definition tileson_forward.hpp:603
const std::type_info & getValueType() const
Definition Property.hpp:133
std::string m_propertyType
Definition Property.hpp:51
void setStrValue(const std::string &value)
Definition Property.hpp:106
Type getType() const
Definition Property.hpp:151
void setName(const std::string &name)
Definition Property.hpp:116
std::string m_name
Definition Property.hpp:50
Definition Base64.hpp:12
Type
Definition Enums.hpp:16