Spinning Topp Logo BlackTopp Studios
inc
attribute.h
Go to the documentation of this file.
1 // © Copyright 2010 - 2016 BlackTopp Studios Inc.
2 /* This file is part of The Mezzanine Engine.
3 
4  The Mezzanine Engine is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  The Mezzanine Engine is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with The Mezzanine Engine. If not, see <http://www.gnu.org/licenses/>.
16 */
17 /* The original authors have included a copy of the license specified above in the
18  'Docs' folder. See 'gpl.txt'
19 */
20 /* We welcome the use of the Mezzanine engine to anyone, including companies who wish to
21  Build professional software and charge for their product.
22 
23  However there are some practical restrictions, so if your project involves
24  any of the following you should contact us and we will try to work something
25  out:
26  - DRM or Copy Protection of any kind(except Copyrights)
27  - Software Patents You Do Not Wish to Freely License
28  - Any Kind of Linking to Non-GPL licensed Works
29  - Are Currently In Violation of Another Copyright Holder's GPL License
30  - If You want to change our code and not add a few hundred MB of stuff to
31  your distribution
32 
33  These and other limitations could cause serious legal problems if you ignore
34  them, so it is best to simply contact us or the Free Software Foundation, if
35  you have any questions.
36 
37  Joseph Toppi - toppij@gmail.com
38  John Blackwood - makoenergy02@gmail.com
39 */
40 /*
41  *
42  * Software, Files, Libraries and all other items referenced in this clause refers only
43  * to the contents of this file and associated documentation.
44  *
45  * Pugixml parser - version 1.0
46  * --------------------------------------------------------
47  * Copyright © 2006-2012, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com)
48  * Report bugs and download new versions at http://pugixml.org/
49  *
50  * This library is distributed under the MIT License. See notice at the end
51  * of this file.
52  *
53  * This work is based on the pugxml parser, which is:
54  * Copyright © 2003, by Kristen Wegner (kristen@tima.net)
55  */
56 
57 #ifndef _xmlattribute_h
58 #define _xmlattribute_h
59 
60 /// @file
61 /// @brief Contains the definition for the XML::Attribute class.
62 
63 #include "datatypes.h"
64 
65 
66 
67 namespace Mezzanine
68 {
69  namespace XML
70  {
71  struct AttributeStruct;
72 
73  /// @brief A light-weight handle for manipulating attributes in DOM tree
75  {
76  friend class AttributeIterator;
77  friend class Node;
78 
79  private:
80  /// @internal
81  /// @brief Stores pointers to the attribute data and some metadata about the attribute.
82  AttributeStruct* AttributeData;
83 
84  #ifndef SWIG
85  /// @brief Used to prevent casting to numerical types acccidentally.
86  /// @note Not available in scripting languages because conversion is handled on a per langauge basis
87  typedef void (*unspecified_bool_type)(Attribute***);
88  #endif
89 
90  public:
91  /// @brief Constructs an empty Attribute.
92  Attribute();
93 
94  /// @brief Constructs attribute from internal pointer.
95  /// @param attr An internal AttributeStruct pointer containing all the data to create an attribute.
96  explicit Attribute(AttributeStruct* attr);
97 
98  #ifndef SWIG
99  /// @brief Used to convert this to a boolean value in a safe way
100  /// @return Returns true if the internal data is set and false otherwise.
101  /// @note Not available in scripting languages because conversion is handled on a per langauge basis
102  operator unspecified_bool_type() const;
103  #endif
104 
105  /// @brief Used to convert this attribute the opposite of it's normal boolean value
106  /// @details This is described in the PugiXML source a a workaround for a borland c++ issue.
107  /// @return Returns false if the internal pointer AttributeStruct is set and true otherwise.
108  bool operator!() const;
109 
110  #ifndef SWIG
111  /// @brief Compares the internal values to check equality.
112  /// @param r The other @ref Attribute this is being compared to.
113  /// @details Many of the internal values are pointers, and it is the addresses of these that are being compared.
114  /// @return Returns true if all the internal values match between this and the other Attribute.
115  bool operator==(const Attribute& r) const;
116  #endif
117 
118  /// @brief Compares the internal values to check inequality.
119  /// @param r The other @ref Attribute this is being compared to.
120  /// @details Many of the internal values are pointers, and it is the addresses of these that are being compared.
121  /// @return Returns true if any of the internal values don't match between this and the other @ref Attribute.
122  bool operator!=(const Attribute& r) const;
123 
124  /// @brief Compares the internal values to check for inequality.
125  /// @param r The other @ref Attribute this is being compared to.
126  /// @details Many of the internal values are pointers, and it is the addresses of these that are being compared.
127  /// @return Returns True if the other @ref Attribute is greater than this one as per sequential comparison of internal pointers.
128  bool operator<(const Attribute& r) const;
129 
130  /// @brief Compares the internal values to check for inequality.
131  /// @param r The other @ref Attribute this is being compared to.
132  /// @details Many of the internal values are pointers, and it is the addresses of these that are being compared.
133  /// @return Returns True if the other @ref Attribute is less than this one as per sequential comparison of internal pointers.
134  bool operator>(const Attribute& r) const;
135 
136  /// @brief Compares the internal values to check for inequality.
137  /// @param r The other @ref Attribute this is being compared to.
138  /// @details Many of the internal values are pointers, and it is the addresses of these that are being compared.
139  /// @return Returns True if the other @ref Attribute is greater than or equal to this one as per sequential comparison of internal pointers.
140  bool operator<=(const Attribute& r) const;
141 
142  /// @brief Compares the internal values to check for inequality.
143  /// @param r The other @ref Attribute this is being compared to.
144  /// @details Many of the internal values are pointers, and it is the addresses of these that are being compared.
145  /// @return Returns True if the other @ref Attribute is less than or equal to this one as per sequential comparison of internal pointers.
146  bool operator>=(const Attribute& r) const;
147 
148  /// @brief Is this storing anything at all?
149  /// @return Returns True if this @ref Attribute is storing nothing. False if it is storing anything.
150  bool Empty() const;
151 
152  /// @brief Get the name of this @ref Attribute.
153  /// @return Returns A pointer to a const c-style array of the the character type (usually char or wchar_t) containing the name.
154  /// @warning returns "" if attribute is empty.
155  const Char8* Name() const;
156 
157  /// @brief Get the Value of this @ref Attribute.
158  /// @return Returns A pointer to a const c-style array of the the character type (usually char or wchar_t) containing the value.
159  /// @warning Returns "" if attribute is empty.
160  const Char8* Value() const;
161 
162  /// @brief Attempts to convert the value of the attribute to a String and returns the results.
163  /// @return If the value of this attribute can be converted to a Real by reading the character
164  /// and interpretting them a number, that number is returned. Returns def on failure.
165  /// @param def Is returned if the attribute is empty
166  /// @exception This can throw exception in certain overflow conditions
167  const Char8* AsString(const Char8* def = "") const;
168 
169  /// @brief Attempts to convert the value of the attribute to an int and returns the results.
170  /// @return If the value of this attribute can be convert to an int by reading the character and interpretting them a number, that numberis returned. Returns passed parameter on failure.
171  /// @todo Update Attribute::AsInt() to check errno and throw exceptions were appropriate, and throw a exception on failure instead of producing a valid return value.
172  /// @param def Is returned if the attribute is empty.
173  /// @warning This may silently fail if the value of the attribute exceeds the maximum value that can be stored in and int. Check "errno" and see if it is set to "ERANGE" to test for this condition.
174  int AsInt(int def = 0) const;
175 
176  /// @brief Attempts to convert the value of the attribute to an unsigned int and returns the results.
177  /// @return If the value of this attribute can be convert to an unsigned int by reading the character and interpretting them a number, that numberis returned. Returns 0 on failure.
178  /// @param def Is returned if the attribute is empty
179  /// @todo Update Attribute::AsUint() to check errno and throw exceptions were appropriate, and throw a exception on failure instead of producing a valid return value.
180  /// @warning This may silently fail if the value of the attribute exceeds the maximum value that can be stored in and int. Check "errno" and see if it is set to "ERANGE" to test for this condition.
181  unsigned int AsUint(unsigned int def = 0) const;
182 
183  /// @brief Attempts to convert the value of the attribute to a double and returns the results.
184  /// @return If the value of this attribute can be convert to a double by reading the character and interpretting them a number, that numberis returned. Returns parameter def on failure.
185  /// @param def Is returned if the attribute is empty
186  /// @todo Update Attribute::AsDouble() to check errno and throw exceptions were appropriate, and throw a exception on failure instead of producing a valid return value.
187  /// @warning This may silently fail if the value of the attribute exceeds the maximum value that can be stored in and double. Check "errno" and see if it is set to "ERANGE" to test for this condition.
188  double AsDouble(double def = 0) const;
189 
190  /// @brief Attempts to convert the value of the attribute to a float and returns the results.
191  /// @return If the value of this attribute can be convert to a float by reading the character and interpretting them a number, that numberis returned. Returns def on failure.
192  /// @param def Is returned if the attribute is empty
193  /// @todo Update Attribute::AsFloat() to check errno and throw exceptions were appropriate, and throw a exception on failure instead of producing a valid return value.
194  /// @warning This may silently fail if the value of the attribute exceeds the maximum value that can be stored in and float. Check "errno" and see if it is set to "ERANGE" to test for this condition.
195  float AsFloat(float def = 0) const;
196 
197  /// @brief Attempts to convert the value of the attribute to a Real and returns the results.
198  /// @return If the value of this attribute can be converted to a Real by reading the character
199  /// and interpretting them a number, that number is returned. Returns argument def on failure.
200  /// @param def Is returned if the attribute is empty
201  /// @exception This can throw exception in certain overflow conditions
202  Real AsReal(Real def = 0) const;
203 
204  /// @brief Attempts to convert the value of the attribute to a Whole and returns the results.
205  /// @return If the value of this attribute can be converted to a Whole by reading the character
206  /// and interpretting them a number, that number is returned. Returns passed parameter on failure.
207  /// @param def Is returned if the attribute is empty
208  /// @exception This can throw exception in certain overflow conditions
209  Whole AsWhole(Whole def = 0) const;
210 
211  /// @brief Attempts to convert the value of the attribute to a Integer and returns the results.
212  /// @return If the value of this attribute can be converted to a Integer by reading the character
213  /// and interpretting them a number, that number is returned. Returns 0 (def defaults to 0) on failure.
214  /// @param def Is returned if the attribute is empty
215  /// @exception This can throw exception in certain overflow conditions
216  Integer AsInteger(Integer def = 0) const;
217 
218  /// @brief Attempts to convert the value of the attribute to a float and returns the results.
219  /// @return Value as bool (returns true if first character is in '1tTyY' set), or def if attribute is empty
220  /// @param def Is returned if the attribute is empty
221  bool AsBool(bool def = false) const;
222 
223  #ifndef SWIG
224  /// @brief Set the name of .
225  /// @param rhs The desired name.
226  /// @return True if successful, returns false if the name cannot be stored or there is not enough memory.
227  /// @note Not available in scripting languages the Overload which accepts a Mezzanine::String is used instead, for enhanced robustness in the event corrupt String are created.
228  bool SetName(const Char8* rhs);
229  #endif
230 
231  /// @brief Set the name of this object
232  /// @param rhs The desired name .
233  /// @return True if successful, returns false if the name cannot be stored or there is not enough memory.
234  /// @note This overload is used in scripting languages.
235  bool SetName(const String& rhs)
236  { return SetName(rhs.c_str()); }
237 
238  #ifndef SWIG
239  /// @brief Set the value of this.
240  /// @param rhs The new Value.
241  /// @return True if successful, returns false if this is empty or there is not enough memory.
242  /// @todo update this to make the error return code redundant and use an exception instead.
243  /// @todo Review for possiblity of buffer overflow.
244  /// @note Not available in scripting languages the Overload which accepts a Mezzanine::String is used instead, for enhanced robustness in the event corrupt String are created.
245  bool SetValue(const Char8* rhs);
246 
247  // Set GetAttribute Value with Type conversion (numbers are converted to strings, boolean is converted to "true"/"false")
248  /// @brief Convert rhs to a character array that contains rhs, then use that as the new value.
249  /// @param rhs The new value as an int.
250  /// @return True if successful, returns false if Attribute is empty or there is not enough memory.
251  /// @todo update this to make the error return code redundant and use an exception instead.
252  /// @todo Review for possiblity of buffer overflow.
253  /// @note Not available in scripting languages the Overload which accepts a Mezzanine::String is used instead.
254  bool SetValue(int rhs);
255 
256  /// @brief Convert rhs to a character array that contains rhs, then use that as the new value.
257  /// @param rhs The new value as an unsigned int.
258  /// @return True if successful, returns false if Attribute is empty or there is not enough memory.
259  /// @todo update this to make the error return code redundant and use an exception instead.
260  /// @todo Review for possiblity of buffer overflow.
261  /// @note Not available in scripting languages the Overload which accepts a Mezzanine::String is used instead.
262  bool SetValue(unsigned int rhs);
263 
264  /// @brief Convert rhs to a character array that contains rhs, then use that as the new value.
265  /// @param rhs The new value as a double.
266  /// @return True if successful, returns false if Attribute is empty or there is not enough memory.
267  /// @todo update this to make the error return code redundant and use an exception instead.
268  /// @todo Review for possiblity of buffer overflow.
269  /// @note Not available in scripting languages the Overload which accepts a Mezzanine::String.
270  bool SetValue(double rhs);
271 
272  /// @brief Convert rhs to a character array that contains the meaning of rhs, then use that as the new value.
273  /// @param rhs This with be interpretted, then converted to "true" or "false" and used as the new value.
274  /// @return True if successful, returns false if Attribute is empty or there is not enough memory.
275  /// @todo update this to make the error return code redundant and use an exception instead.
276  /// @todo Review for possiblity of buffer overflow.
277  /// @note Not available in scripting languages the Overload which accepts a Mezzanine::String is used instead.
278  bool SetValue(bool rhs);
279 
280  /// @brief Convert rhs to a character array that contains the meaning of rhs, then use that as the new value.
281  /// @param rhs This with be converted to a character array using the appropriate streaming operator <<, then used as the new value.
282  /// @return True if successful, returns false if Attribute is empty or there is not enough memory.
283  /// @warning You should not pass classes that stream/serialize to xml into this function, the result will be invalid XML. If you must, find a way to strip out the ">" character, then you can reinsert it later
284  /// @todo Strip ">" automatically and provide a method to reconsitute it.
285  template <class T> bool SetValue(T rhs)
286  { return SetValue(ToString(rhs).c_str()); }
287  #endif
288 
289  /// @brief Set the value of this.
290  /// @param rhs The new Value.
291  /// @return True if successful, returns false if this is empty or there is not enough memory.
292  /// @todo update this to make the error return code redundant and use an exception instead.
293  /// @note This overload is used in scripting languages.
294  bool SetValue(const String& rhs)
295  { return SetValue(rhs.c_str()); }
296 
297  /// @param rhs The new value as an c-style string.
298  /// @brief The same as @ref Attribute::SetValue(); without the error return
299  /// @return An reference to this attribute.
300  Attribute& operator=(const Char8* rhs);
301 
302  /// @param rhs The new value as an int.
303  /// @brief The same as @ref Attribute::SetValue(); without the error return.
304  /// @return An reference to this attribute.
305  Attribute& operator=(int rhs);
306 
307  /// @param rhs The new value as an unsigned int.
308  /// @brief The same as @ref Attribute::SetValue(); without the error return.
309  /// @return An reference to this attribute.
310  Attribute& operator=(unsigned int rhs);
311 
312  /// @brief The same as @ref Attribute::SetValue(); without the error return.
313  /// @param rhs The new value as a double.
314  /// @return An reference to this attribute.
315  Attribute& operator=(double rhs);
316 
317  /// @brief The same as @ref Attribute::SetValue(); without the error return.
318  /// @param rhs This with be interpretted, then converted to "true" or "false" and used as the new value.
319  /// @return An reference to this attribute.
320  Attribute& operator=(bool rhs);
321 
322  /// @brief Get the next attribute.
323  /// @details This will get the next sibling attribute. That is, another Attribute on the same node as this attribute. This is internally a circular linked list, so once you reach the end, you simply be given the first node. If this attribute is empty this will return a empty attribute.
324  /// @return Either the next attribute or if this attribute is empty an empty attribute.
325  Attribute GetNextAttribute() const;
326 
327  /// @brief Get the previous attribute.
328  /// @details This will get the previous sibling attribute. That is, another Attribute on the same node as this attribute. This is internally a circular linked list, so once you reach the beginning, you simply be given the last node. If this attribute is empty this will return a empty attribute.
329  /// @return Either the previous attribute or if this attribute is empty an empty attribute.
330  Attribute GetPreviousAttribute() const;
331 
332  /// @brief Get a unique identifying value for the Attribute this represents
333  /// @return A size_t that is unique per Attribute that an attribute could represent.
334  size_t HashValue() const;
335 
336  /// @brief Retrieve a pointer to the internal data.
337  /// @return A void pointer to the internal data.
338  /// @internal
339  AttributeStruct* InternalObject() const;
340  };
341 
342 
343  }
344 }// /namespace Mezzanine
345 
346 
347 
348 #endif // Include guard
349 
350 
351 /*
352  *
353  * Software, Files, Libraries and all other items referenced in this clause refers only
354  * to the contents of this file and associated documentation.
355  *
356  * Copyright © 2006-2012 Arseny Kapoulkine
357  *
358  * Permission is hereby granted, free of charge, to any person
359  * obtaining a copy of this software and associated documentation
360  * files (the "Software"), to deal in the Software without
361  * restriction, including without limitation the rights to use,
362  * copy, modify, merge, publish, distribute, sublicense, and/or sell
363  * copies of the Software, and to permit persons to whom the
364  * Software is furnished to do so, subject to the following
365  * conditions:
366  *
367  * The above copyright notice and this permission notice shall be
368  * included in all copies or substantial portions of the Software.
369  *
370  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
371  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
372  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
373  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
374  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
375  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
376  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
377  * OTHER DEALINGS IN THE SOFTWARE.
378  */
A light-weight handle for manipulating attributes in DOM tree.
Definition: attribute.h:74
String ToString(const T &Datum)
Converts whatever to a String as long as a streaming operator is available for it.
Definition: datatypes.h:242
All the definitions for datatypes as well as some basic conversion functions are defined here...
int Integer
A datatype used to represent any integer close to.
Definition: datatypes.h:154
bool SetValue(const String &rhs)
Set the value of this.
Definition: attribute.h:294
float Real
A Datatype used to represent a real floating point number.
Definition: datatypes.h:141
char Char8
A datatype to represent one character.
Definition: datatypes.h:169
A light-weight handle for manipulating nodes in DOM tree.
Definition: node.h:89
Attribute iterator (a bidirectional iterator over a collection of Attribute).
#define MEZZ_LIB
Some platforms require special decorations to denote what is exported/imported in a share library...
bool SetValue(T rhs)
Convert rhs to a character array that contains the meaning of rhs, then use that as the new value...
Definition: attribute.h:285
The bulk of the engine components go in this namspace.
Definition: actor.cpp:56
bool SetName(const String &rhs)
Set the name of this object.
Definition: attribute.h:235
unsigned long Whole
Whole is an unsigned integer, it will be at least 32bits in size.
Definition: datatypes.h:151
std::string String
A datatype used to a series of characters.
Definition: datatypes.h:159