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 
414  StructureContainer::const_iterator BeginStructure(void) const;
415 
420  StructureContainer::const_iterator EndStructure(void) const;
421 
426  StructureContainer::iterator BeginStructure(void);
427 
432  StructureContainer::iterator EndStructure(void);
433 
434  //TODO: TIM: Implementation: (Test) Add a way to iterate over all Members in a structure.
435 
436  //Used ONLY for Structure or Array Values.
437 
445  size_t Size(void) const;
446 
447  //Acceptable Inputs:
448  // "engine.pistons[0].isDamaged"
449  //const MagicValue& FromPath(const tbCore::tbString& path) const;
450 
454  operator int() const { return AsInteger(true); }
455 
459  operator float() const { return AsFloat(true); }
460 
464  operator bool() const { return AsBoolean(true); }
465 
469  operator const bool() const { return AsBoolean(true); }
470 
474  operator tbCore::tbString() const { return AsString(true); }
475 
483  bool operator==(const DynamicStructure& rightSide) const;
484 
485  private:
489  void SetToNil(void);
490 
494  int ConvertToInteger(void) const;
495 
499  float ConvertToFloat(void) const;
500 
504  bool ConvertToBoolean(void) const;
505 
509  tbCore::tbString ConvertToString(void) const;
510 
514  enum DynamicStructureValueType
515  {
516  kNilType,
517 
518  kIntegerType,
519  kFloatType,
520  kBooleanType,
521  kStringType,
522 
523  kArrayType,
524  kStructureType,
525  };
526 
527 // ///
528 // /// TODO: TIM: InternalDoc: This is a TurtleBrains implementation detail, and should be documented as such.
529 // ///
530 // typedef std::vector<DynamicStructure> ArrayContainer;
531 //
532 // ///
533 // /// TODO: TIM: InternalDoc: This is a TurtleBrains implementation detail, and should be documented as such.
534 // ///
535 // typedef std::map<tbCore::tbString, DynamicStructure> StructureContainer;
536 
540  DynamicStructureValueType mValueType;
541 
542  //Unnamed anonymous union so that DynamicStrucure always starts
543  union
544  {
545  char mRawBytes[8]; //Reserve 64-bits for whatever types follow.
546  int mInteger;
547  bool mBoolean;
548  float mFloat;
549 
550  tbCore::tbString* mString;
551  ArrayContainer* mArray;
552  StructureContainer* mStructure;
553  };
554 
555  public:
560  static const unsigned int kInvalidSize;
561  static const bool kTypeSafeArrays;
562  static const bool kImplicitConversions;
563  static const bool kImplicitTypeChange;
564  static const float kFloatElipson;
565  };
566 
568 
569  /* Overloads to make the DynamicStructure behave like a built in Integer */
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  inline bool operator!=(const DynamicStructure& leftSide, const int& rightSide) { return (leftSide.AsInteger() != rightSide) ? true : false; }
573  inline bool operator!=(const int& leftSide, const DynamicStructure& rightSide) { return (rightSide.AsInteger() != leftSide) ? true : false; }
574 
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  inline bool operator!=(const DynamicStructure& leftSide, const float& rightSide) { return (fabs(leftSide.AsFloat() - rightSide) > DynamicStructure::kFloatElipson) ? true : false; }
578  inline bool operator!=(const float& leftSide, const DynamicStructure& rightSide) { return (fabs(rightSide.AsFloat() - leftSide) > DynamicStructure::kFloatElipson) ? true : false; }
579 
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  inline bool operator!=(const DynamicStructure& leftSide, const bool& rightSide) { return (leftSide.AsBoolean() != rightSide) ? true : false; }
583  inline bool operator!=(const bool& leftSide, const DynamicStructure& rightSide) { return (rightSide.AsBoolean() != leftSide) ? true : false; }
584 
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  inline bool operator!=(const DynamicStructure& leftSide, const tbCore::tbString& rightSide) { return (leftSide.AsString() != rightSide) ? true : false; }
588  inline bool operator!=(const tbCore::tbString& leftSide, const DynamicStructure& rightSide) { return (rightSide.AsString() != leftSide) ? true : false; }
589 
591 
592  }; /* namespace Core */
593 }; /* namespace TurtleBrains */
594 
595 namespace tbCore = TurtleBrains::Core;
596 
597 #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:562
static const DynamicStructure kNullValue
Definition: tb_dynamic_structure.h:556
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:564
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:558
float AsFloatWithDefault(const float defaultValue) const
const DynamicStructure & operator[](const size_t &arrayIndex) const
static const unsigned int kInvalidSize
Definition: tb_dynamic_structure.h:560
const DynamicStructure & GetValue(const size_t &arrayIndex) const
int AsInteger(bool implicitConversion=kImplicitConversions) const
static const bool kTypeSafeArrays
Definition: tb_dynamic_structure.h:561
static const tbCore::tbString kNullAsString
Definition: tb_dynamic_structure.h:557
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:559
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_debug_logger.h:91
std::string tbString
Definition: tb_string.h:335
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:563
DynamicStructure & SetMember(const tbCore::tbString &memberName, const DynamicStructure &memberValue)