TurtleBrains  0.2.1
High quality, portable, C++ API for native application and game development.
tb_dynamic_structure.h
1 
9 #ifndef _TurtleBrains_DynamicStructure_h_
10 #define _TurtleBrains_DynamicStructure_h_
11 
12 #include "tb_string.h"
13 
14 #include <cmath>
15 #include <vector>
16 #include <map>
17 #include <string>
18 
19 namespace TurtleBrains
20 {
21  namespace Core
22  {
23 
29  {
30  public:
34  typedef std::vector<DynamicStructure> ArrayContainer;
35 
39  typedef std::map<tbCore::tbString, DynamicStructure> StructureContainer;
40 
44  DynamicStructure(void);
45 
50  DynamicStructure(const int& integerValue);
51 
56  DynamicStructure(const float& floatValue);
57 
62  DynamicStructure(const bool& booleanValue);
63 
68  DynamicStructure(const tbCore::tbString& stringValue);
69 
74  DynamicStructure(const DynamicStructure& other);
75 
81 
87  ~DynamicStructure(void);
88 
89 
96  bool IsNil(void) const;
97 
101  bool IsArray(void) const;
102 
107  bool IsStructure(void) const;
108 
112  bool IsInteger(void) const;
113 
117  bool IsFloat(void) const;
118 
122  bool IsBoolean(void) const;
123 
127  bool IsString(void) const;
128 
129 
138  int AsInteger(bool implicitConversion = kImplicitConversions) const;
139 
148  float AsFloat(bool implicitConversion = kImplicitConversions) const;
149 
158  bool AsBoolean(bool implicitConversion = kImplicitConversions) const;
159 
168  tbCore::tbString AsString(bool implicitConversion = kImplicitConversions) const;
169 
170  //
171  // TODO: TIM: Planning: Do we want to support this? If so implement and document.
172  // @note Cannot implicitly convert to a string, will assert if type is not a string.
173  //
174  //const tbCore::tbString& AsStringRef(void) const;
175 
176 
187  int AsIntegerWithDefault(const int defaultValue) const;
188 
199  float AsFloatWithDefault(const float defaultValue) const;
200 
211  bool AsBooleanWithDefault(const bool defaultValue) const;
212 
223  tbCore::tbString AsStringWithDefault(const tbCore::tbString& defaultValue) const;
224 
225 
235  void SetValue(const int& integerValue, bool implicitTypeChange = kImplicitTypeChange);
236 
246  void SetValue(const float& floatValue, bool implicitTypeChange = kImplicitTypeChange);
247 
257  void SetValue(const bool& booleanValue, bool implicitTypeChange = kImplicitTypeChange);
258 
268  void SetValue(const tbCore::tbString& stringValue, bool implicitTypeChange = kImplicitTypeChange);
269 
270 
271  //Used ONLY for Array Values. Type must be kArrayType or kNilType to PushValue
272 
286 
297  const DynamicStructure& GetValue(const size_t& arrayIndex) const;
298 
309  DynamicStructure& GetValue(const size_t& arrayIndex);
310 
321  const DynamicStructure& operator[](const size_t& arrayIndex) const;
322 
333  DynamicStructure& operator[](const size_t& arrayIndex);
334 
335  //
336  // TODO: TIM: Planning: Do we need to support int too? Teach the user how to use this.
337  //
338  //const DynamicStructure& operator[](const int& arrayIndex) const;
339 
340  //
341  // TODO: TIM: Planning: Do wee need to support int too? Teach the user how to use this.
342  //
343  //DynamicStructure& operator[](const int& arrayIndex);
344 
345 
346 
347 
348  //Used ONLY for Structure Values. Type must be kStructureType or kNilType to AddMember
349 
360  DynamicStructure& AddMember(const tbCore::tbString& memberName, const DynamicStructure& memberValue);
361 
372  DynamicStructure& SetMember(const tbCore::tbString& memberName, const DynamicStructure& memberValue);
373 
383  const DynamicStructure& GetMember(const tbCore::tbString& memberName) const;
384 
388  DynamicStructure& GetMember(const tbCore::tbString& memberName);
389 
393  const DynamicStructure& operator[](const tbCore::tbString& memberName) const;
394 
398  DynamicStructure& operator[](const tbCore::tbString& memberName);
399 
403  const DynamicStructure& operator[](const char* const memberName) const;
404 
408  DynamicStructure& operator[](const char* const memberName);
409 
413  StructureContainer::const_iterator BeginStructure(void) const;
414 
419  StructureContainer::const_iterator EndStructure(void) const;
420 
424  StructureContainer::iterator BeginStructure(void);
425 
430  StructureContainer::iterator EndStructure(void);
431 
432  //TODO: TIM: Implementation: (Test) Add a way to iterate over all Members in a structure.
433 
434  //Used ONLY for Structure or Array Values.
435 
443  size_t Size(void) const;
444 
445  //Acceptable Inputs:
446  // "engine.pistons[0].isDamaged"
447  //const MagicValue& FromPath(const tbCore::tbString& path) const;
448 
452  operator int() const { return AsInteger(true); }
453 
457  operator float() const { return AsFloat(true); }
458 
462  operator bool() const { return AsBoolean(true); }
463 
467  operator const bool() const { return AsBoolean(true); }
468 
472  operator tbCore::tbString() const { return AsString(true); }
473 
481  bool operator==(const DynamicStructure& rightSide) const;
482 
483  private:
487  void SetToNil(void);
488 
492  int ConvertToInteger(void) const;
493 
497  float ConvertToFloat(void) const;
498 
502  bool ConvertToBoolean(void) const;
503 
507  tbCore::tbString ConvertToString(void) const;
508 
512  enum DynamicStructureValueType
513  {
514  kNilType,
515 
516  kIntegerType,
517  kFloatType,
518  kBooleanType,
519  kStringType,
520 
521  kArrayType,
522  kStructureType,
523  };
524 
525 // ///
526 // /// TODO: TIM: InternalDoc: This is a TurtleBrains implementation detail, and should be documented as such.
527 // ///
528 // typedef std::vector<DynamicStructure> ArrayContainer;
529 //
530 // ///
531 // /// TODO: TIM: InternalDoc: This is a TurtleBrains implementation detail, and should be documented as such.
532 // ///
533 // typedef std::map<tbCore::tbString, DynamicStructure> StructureContainer;
534 
538  DynamicStructureValueType mValueType;
539 
540  //Unnamed anonymous union so that DynamicStrucure always starts
541  union
542  {
543  char mRawBytes[8]; //Reserve 64-bits for whatever types follow.
544  int mInteger;
545  bool mBoolean;
546  float mFloat;
547 
548  tbCore::tbString* mString;
549  ArrayContainer* mArray;
550  StructureContainer* mStructure;
551  };
552 
553  public:
558  static const unsigned int kInvalidSize;
559  static const bool kTypeSafeArrays;
560  static const bool kImplicitConversions;
561  static const bool kImplicitTypeChange;
562  static const float kFloatElipson;
563  };
564 
566 
567  /* Overloads to make the DynamicStructure behave like a built in Integer */
568  inline bool operator==(const DynamicStructure& leftSide, const int& rightSide) { return (leftSide.AsInteger() == rightSide) ? true : false; }
569  inline bool operator==(const int& leftSide, const DynamicStructure& rightSide) { return (rightSide.AsInteger() == leftSide) ? true : false; }
570  inline bool operator!=(const DynamicStructure& leftSide, const int& rightSide) { return (leftSide.AsInteger() != rightSide) ? true : false; }
571  inline bool operator!=(const int& leftSide, const DynamicStructure& rightSide) { return (rightSide.AsInteger() != leftSide) ? true : false; }
572 
573  inline bool operator==(const DynamicStructure& leftSide, const float& rightSide) { return (fabs(leftSide.AsFloat() - rightSide) <= DynamicStructure::kFloatElipson) ? true : false; }
574  inline bool operator==(const float& leftSide, const DynamicStructure& rightSide) { return (fabs(rightSide.AsFloat() - leftSide) <= DynamicStructure::kFloatElipson) ? true : false; }
575  inline bool operator!=(const DynamicStructure& leftSide, const float& rightSide) { return (fabs(leftSide.AsFloat() - rightSide) > DynamicStructure::kFloatElipson) ? true : false; }
576  inline bool operator!=(const float& leftSide, const DynamicStructure& rightSide) { return (fabs(rightSide.AsFloat() - leftSide) > DynamicStructure::kFloatElipson) ? true : false; }
577 
578  inline bool operator==(const DynamicStructure& leftSide, const bool& rightSide) { return (leftSide.AsBoolean() == rightSide) ? true : false; }
579  inline bool operator==(const bool& leftSide, const DynamicStructure& rightSide) { return (rightSide.AsBoolean() == leftSide) ? true : false; }
580  inline bool operator!=(const DynamicStructure& leftSide, const bool& rightSide) { return (leftSide.AsBoolean() != rightSide) ? true : false; }
581  inline bool operator!=(const bool& leftSide, const DynamicStructure& rightSide) { return (rightSide.AsBoolean() != leftSide) ? true : false; }
582 
583  inline bool operator==(const DynamicStructure& leftSide, const tbCore::tbString& rightSide) { return (leftSide.AsString() == rightSide) ? true : false; }
584  inline bool operator==(const tbCore::tbString& leftSide, const DynamicStructure& rightSide) { return (rightSide.AsString() == leftSide) ? true : false; }
585  inline bool operator!=(const DynamicStructure& leftSide, const tbCore::tbString& rightSide) { return (leftSide.AsString() != rightSide) ? true : false; }
586  inline bool operator!=(const tbCore::tbString& leftSide, const DynamicStructure& rightSide) { return (rightSide.AsString() != leftSide) ? true : false; }
587 
589 
590  }; /* namespace Core */
591 }; /* namespace TurtleBrains */
592 
593 namespace tbCore = TurtleBrains::Core;
594 
595 #endif /* _TurtleBrains_DynamicStructure_h_ */
const DynamicStructure & GetMember(const tbCore::tbString &memberName) const
DynamicStructure & PushValue(const DynamicStructure &value)
static const bool kImplicitConversions
Definition: tb_dynamic_structure.h:560
static const DynamicStructure kNullValue
Definition: tb_dynamic_structure.h:554
tbCore::tbString AsString(bool implicitConversion=kImplicitConversions) const
tbCore::tbString AsStringWithDefault(const tbCore::tbString &defaultValue) const
StructureContainer::const_iterator EndStructure(void) const
static const float kFloatElipson
Definition: tb_dynamic_structure.h:562
Contains all functions, classes and helpers related to game/application development written by Tim "B...
Definition: tb_application_dialog.h:21
float AsFloat(bool implicitConversion=kImplicitConversions) const
static const tbCore::tbString kTrueAsString
Definition: tb_dynamic_structure.h:556
float AsFloatWithDefault(const float defaultValue) const
const DynamicStructure & operator[](const size_t &arrayIndex) const
static const unsigned int kInvalidSize
Definition: tb_dynamic_structure.h:558
const DynamicStructure & GetValue(const size_t &arrayIndex) const
int AsInteger(bool implicitConversion=kImplicitConversions) const
static const bool kTypeSafeArrays
Definition: tb_dynamic_structure.h:559
static const tbCore::tbString kNullAsString
Definition: tb_dynamic_structure.h:555
Definition: tb_dynamic_structure.h:28
std::map< tbCore::tbString, DynamicStructure > StructureContainer
Definition: tb_dynamic_structure.h:39
static const tbCore::tbString kFalseAsString
Definition: tb_dynamic_structure.h:557
bool operator==(const DynamicStructure &rightSide) const
bool AsBoolean(bool implicitConversion=kImplicitConversions) const
std::vector< DynamicStructure > ArrayContainer
Definition: tb_dynamic_structure.h:34
StructureContainer::const_iterator BeginStructure(void) const
DynamicStructure & AddMember(const tbCore::tbString &memberName, const DynamicStructure &memberValue)
void SetValue(const int &integerValue, bool implicitTypeChange=kImplicitTypeChange)
Contains core functionality for each component of the API.
Definition: tb_dynamic_structure.h:21
std::string tbString
Definition: tb_string.h:293
DynamicStructure & operator=(const DynamicStructure &other)
bool AsBooleanWithDefault(const bool defaultValue) const
int AsIntegerWithDefault(const int defaultValue) const
static const bool kImplicitTypeChange
Definition: tb_dynamic_structure.h:561
DynamicStructure & SetMember(const tbCore::tbString &memberName, const DynamicStructure &memberValue)