Tileson  1.3.0
A helpful json parser for Tiled maps
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 
18 namespace tson
19 {
20  class Property
21  {
22  public:
23 
24  //enum class Type : uint8_t
25  //{
26  // Undefined = 0,
27  // Color = 1, /*! color */
28  // File = 2, /*! file */
29  // Int = 3, /*! int */
30  // Boolean = 4, /*! bool */
31  // Float = 5, /*! float */
32  // String = 6 /*! string */
33  //};
34 
35  inline Property();
36  inline Property(IJson &json);
37  inline Property(std::string name, std::any value, Type type);
38 
39  inline void setValue(const std::any &value);
40  inline void setStrValue(const std::string &value);
41  inline void setName(const std::string &name);
42 
43  [[nodiscard]] inline const std::type_info& getValueType() const;
44  inline std::string getValueTypeInfo();
45  [[nodiscard]]inline const std::any &getValue() const;
46  template <typename T>
47  inline T getValue() const;
48  [[nodiscard]] inline const std::string &getName() const;
49  [[nodiscard]] inline Type getType() const;
50 
51  protected:
52  inline void setTypeByString(const std::string &str);
53  inline void setValueByType(IJson &json);
54 
56  std::string m_name;
57  std::any m_value; //Using std::any to assign any type
58  };
59 
60  template<typename T>
62  {
63  bool isCorrectType = (m_value.type() == typeid(T));
64 
65  if(isCorrectType)
66  {
67  T value = std::any_cast<T>(m_value);
68  return value;
69  }
70  else
71  {
72  static T defaultValue;
73  return defaultValue;
74  }
75  }
76 }
77 
78 tson::Property::Property() : m_name {"unnamed"}
79 {
80 
81 }
82 
84 {
85  setTypeByString(json["type"].get<std::string>());
86  setValueByType(json["value"]);
87  m_name = json["name"].get<std::string>();
88 }
89 
90 tson::Property::Property(std::string name, std::any value, Type type) : m_name { move(name) }, m_value { move(value) }, m_type {type}
91 {
92 
93 }
94 
95 void tson::Property::setValue(const std::any &value)
96 {
97  m_value = value;
98 }
99 
106 void tson::Property::setStrValue(const std::string &value)
107 {
108  m_value = value;
109 }
110 
111 const std::any &tson::Property::getValue() const
112 {
113  return m_value;
114 }
115 
116 void tson::Property::setName(const std::string &name)
117 {
118  m_name = name;
119 }
120 
121 const std::string &tson::Property::getName() const
122 {
123  return m_name;
124 }
125 
133 const 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 
156 void 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
171  m_type = tson::Type::Undefined;
172 }
173 
175 {
176  switch(m_type)
177  {
178  case Type::Color:
179  m_value = Colori(json.get<std::string>());
180  break;
181 
182  case Type::File:
183  m_value = fs::path(json.get<std::string>());
184  break;
185 
186  case Type::Int:
187  m_value = json.get<int>();
188  break;
189 
190  case Type::Boolean:
191  m_value = json.get<bool>();
192  break;
193 
194  case Type::Float:
195  m_value = json.get<float>();
196  break;
197 
198  case Type::String:
199  setStrValue(json.get<std::string>());
200  break;
201 
202  default:
203  setStrValue(json.get<std::string>());
204  break;
205 
206  }
207 }
208 
209 #endif //TILESON_PROPERTY_HPP
Definition: IJson.hpp:11
T get(std::string_view key)
Definition: IJson.hpp:72
Definition: Property.hpp:21
Property()
Definition: Property.hpp:78
void setTypeByString(const std::string &str)
Definition: Property.hpp:156
std::any m_value
Definition: Property.hpp:57
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
const std::string & getName() const
Definition: Property.hpp:121
Type m_type
Definition: Property.hpp:55
void setValueByType(IJson &json)
Definition: Property.hpp:174
const std::type_info & getValueType() const
Definition: Property.hpp:133
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:56
Definition: Base64.hpp:12
Type
Definition: Enums.hpp:16
Color< uint8_t > Colori
Definition: Color.hpp:89