tmxparser  2.1.0
 All Classes Functions Variables Pages
TmxProperty.h
1 //-----------------------------------------------------------------------------
2 // TmxProperty.h
3 //
4 // Copyright (c) 2016, Tamir Atias
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 // Author: Tamir Atias
27 //-----------------------------------------------------------------------------
28 #pragma once
29 
30 #include <string>
31 #include "TmxColor.h"
32 
33 namespace tinyxml2 {
34  class XMLElement;
35 }
36 
37 namespace Tmx
38 {
39  //-------------------------------------------------------------------------
41  //-------------------------------------------------------------------------
42  enum PropertyType
43  {
45  TMX_PROPERTY_STRING,
46 
48  TMX_PROPERTY_BOOL,
49 
51  TMX_PROPERTY_INT,
52 
54  TMX_PROPERTY_FLOAT,
55 
57  TMX_PROPERTY_COLOR,
58 
60  TMX_PROPERTY_FILE
61  };
62 
63  //-------------------------------------------------------------------------
65  //-------------------------------------------------------------------------
66  class Property
67  {
68  public:
69  Property();
70 
72  void Parse(const tinyxml2::XMLElement *propertyElem);
73 
75  PropertyType GetType() const { return type; }
76 
78  bool IsOfType(PropertyType propertyType) const { return GetType() == propertyType; }
79 
81  const std::string &GetValue() const { return value; }
82 
84  bool IsValueEmpty() const { return value.empty(); }
85 
87  bool GetBoolValue(bool defaultValue = false) const;
88 
90  int GetIntValue(int defaultValue = 0) const;
91 
93  float GetFloatValue(float defaultValue = 0.0f) const;
94 
96  Tmx::Color GetColorValue(Tmx::Color defaultValue = Tmx::Color()) const;
97  private:
98  PropertyType type;
99  std::string value;
100  };
101 
102  inline bool Property::GetBoolValue(bool defaultValue) const
103  {
104  if (!IsOfType(TMX_PROPERTY_BOOL))
105  return defaultValue;
106 
107  return value.compare("true") == 0;
108  }
109 
110  inline int Property::GetIntValue(int defaultValue) const
111  {
112  if (!IsOfType(TMX_PROPERTY_INT))
113  return defaultValue;
114 
115  return std::stoi(value);
116  }
117 
118  inline float Property::GetFloatValue(float defaultValue) const
119  {
120  if (!IsOfType(TMX_PROPERTY_FLOAT))
121  return defaultValue;
122 
123  return std::stof(value);
124  }
125 
126  inline Tmx::Color Property::GetColorValue(Tmx::Color defaultValue) const
127  {
128  if (!IsOfType(TMX_PROPERTY_COLOR))
129  return defaultValue;
130 
131  return Tmx::Color(value);
132  }
133 }
void Parse(const tinyxml2::XMLElement *propertyElem)
Parse the property element.
Definition: TmxProperty.cpp:39
PropertyType GetType() const
Get the type of the property (default: TMX_PROPERTY_STRING)
Definition: TmxProperty.h:75
float GetFloatValue(float defaultValue=0.0f) const
Convert the value to a float and return it (or the default value if not a float). ...
Definition: TmxProperty.h:118
int GetIntValue(int defaultValue=0) const
Convert the value to an integer and return it (or the default value if not an integer).
Definition: TmxProperty.h:110
bool IsOfType(PropertyType propertyType) const
Check if the property is of a certain type.
Definition: TmxProperty.h:78
Tmx::Color GetColorValue(Tmx::Color defaultValue=Tmx::Color()) const
Convert the value to a color and return it (or the default value if not a color). ...
Definition: TmxProperty.h:126
A class used for storing information about a color.
Definition: TmxColor.h:38
Used to store a (typed) property.
Definition: TmxProperty.h:66
bool IsValueEmpty() const
Return whether the value is empty or was not specified.
Definition: TmxProperty.h:84
bool GetBoolValue(bool defaultValue=false) const
Convert the value to a boolean and return it (or the default value if not a boolean.)
Definition: TmxProperty.h:102
const std::string & GetValue() const
Return the value of the property.
Definition: TmxProperty.h:81