tmxlite
lightweight Tiled tmx map parser for C++
Property.hpp
1 /*********************************************************************
2 Matt Marchant 2016
3 http://trederia.blogspot.com
4 
5 tmxlite - Zlib license.
6 
7 This software is provided 'as-is', without any express or
8 implied warranty. In no event will the authors be held
9 liable for any damages arising from the use of this software.
10 
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute
13 it freely, subject to the following restrictions:
14 
15 1. The origin of this software must not be misrepresented;
16 you must not claim that you wrote the original software.
17 If you use this software in a product, an acknowledgment
18 in the product documentation would be appreciated but
19 is not required.
20 
21 2. Altered source versions must be plainly marked as such,
22 and must not be misrepresented as being the original software.
23 
24 3. This notice may not be removed or altered from any
25 source distribution.
26 *********************************************************************/
27 
28 #ifndef TMXLITE_PROPERTY_HPP_
29 #define TMXLITE_PROPERTY_HPP_
30 
31 #include <tmxlite/Config.hpp>
32 #include <tmxlite/Types.hpp>
33 
34 #include <string>
35 #include <cassert>
36 
37 namespace pugi
38 {
39  class xml_node;
40 }
41 
42 namespace tmx
43 {
51  class TMXLITE_EXPORT_API Property final
52  {
53  public:
54 
55  enum class Type
56  {
57  Boolean,
58  Float,
59  Int,
60  String,
61  Colour,
62  File,
63  Undef
64  };
65 
66  Property();
67  ~Property() = default;
68 
72  void parse(const pugi::xml_node&);
79  Type getType() const { return m_type; }
83  const std::string& getName() const { return m_name; }
87  bool getBoolValue() const { assert(m_type == Type::Boolean); return m_boolValue; }
91  float getFloatValue() const { assert(m_type == Type::Float); return m_floatValue; }
95  int getIntValue() const { assert(m_type == Type::Int); return m_intValue; }
99  const std::string& getStringValue() const { assert(m_type == Type::String); return m_stringValue; }
103  const Colour& getColourValue() const { assert(m_type == Type::Colour); return m_colourValue; }
107  const std::string& getFileValue() const { assert(m_type == Type::File); return m_stringValue; }
108 
109 
110  private:
111  union
112  {
113  bool m_boolValue;
114  float m_floatValue;
115  int m_intValue;
116  };
117  std::string m_stringValue;
118  std::string m_name;
119  Colour m_colourValue;
120 
121  Type m_type;
122  };
123 }
124 
125 #endif //TMXLITE_PROPERTY_HPP_
Definition: Log.hpp:58
const std::string & getStringValue() const
Returns the property&#39;s value as a string.
Definition: Property.hpp:99
const Colour & getColourValue() const
Returns the property&#39;s value as a Colour struct.
Definition: Property.hpp:103
int getIntValue() const
Returns the property&#39;s value as an integer.
Definition: Property.hpp:95
Contains the red, green, blue and alpha values of a colour in the range 0 - 255.
Definition: Types.hpp:109
float getFloatValue() const
Returns the property&#39;s value as a float.
Definition: Property.hpp:91
Definition: Layer.hpp:39
Type getType() const
Returns the type of data stored in the property. This should generally be called first before trying ...
Definition: Property.hpp:79
Represents a custom property. Tiles, objects and layers of a tmx map may have custom properties assig...
Definition: Property.hpp:51
const std::string & getName() const
Returns the name of this property.
Definition: Property.hpp:83
const std::string & getFileValue() const
Returns the file path property as a string, relative to the map file.
Definition: Property.hpp:107
bool getBoolValue() const
Returns the property&#39;s value as a boolean.
Definition: Property.hpp:87