tb_value.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // ================================================================================
  2. // == This file is a part of Turbo Badger. (C) 2011-2014, Emil Segerås ==
  3. // == See tb_core.h for more information. ==
  4. // ================================================================================
  5. #ifndef TB_VALUE_H
  6. #define TB_VALUE_H
  7. #include "tb_core.h"
  8. #include "tb_list.h"
  9. namespace tb {
  10. class TBValue;
  11. class TBTypedObject;
  12. /** Return true if the given string starts with a number.
  13. Ex: 100, -.2, 1.0E-8, 5px will all return true. */
  14. bool is_start_of_number(const char *str);
  15. /** Returns true if the given string contains space that
  16. is not at the end of the string. */
  17. bool contains_non_trailing_space(const char *str);
  18. /** Return true if the string can be represented as a number.
  19. It ignores trailing white space.
  20. Ex: 100, -.2 will return true.
  21. Ex: 1.0E-8, 5px will return false. */
  22. bool is_number_only(const char *str);
  23. /** Return true if the given number string is a float number.
  24. Should only be called when you've verified it's a number with is_number(). */
  25. bool is_number_float(const char *str);
  26. /** TBValueArray is a array of TBValue */
  27. class TBValueArray
  28. {
  29. public:
  30. TBValueArray();
  31. ~TBValueArray();
  32. TBValue *AddValue();
  33. TBValue *GetValue(int index);
  34. static TBValueArray *Clone(TBValueArray *source);
  35. int GetLength() const { return m_list.GetNumItems(); }
  36. private:
  37. TBListAutoDeleteOf<TBValue> m_list;
  38. };
  39. /** TBValue holds value of a specific type.
  40. In addition to NULL, string, float, integer, it may also contain an array
  41. of attributes (TBValueArray), or an object (derived from TBTypedObject).
  42. When getting the value as a different type from what it is, it may convert
  43. its internal representation to that type. Exceptions are for array and
  44. object, which will return 0 when getting as numbers, or "" or object name
  45. when getting as string. */
  46. class TBValue
  47. {
  48. public:
  49. /** The current type of the value.
  50. It may change when using a getter of a different type. */
  51. enum TYPE {
  52. TYPE_NULL,
  53. TYPE_STRING,
  54. TYPE_FLOAT,
  55. TYPE_INT,
  56. TYPE_OBJECT,
  57. TYPE_ARRAY
  58. };
  59. /** How to deal with the dynamic memory when setting string and array. */
  60. enum SET {
  61. SET_NEW_COPY, ///< A new copy of the data will be made.
  62. SET_TAKE_OWNERSHIP, ///< The data passed in will be stored and freed.
  63. SET_AS_STATIC ///< The data passed in will be stored but never freed.
  64. };
  65. TBValue();
  66. TBValue(const TBValue &value);
  67. TBValue(TYPE type);
  68. TBValue(int value);
  69. TBValue(float value);
  70. TBValue(const char *value, SET set = SET_NEW_COPY);
  71. TBValue(TBTypedObject *object);
  72. ~TBValue();
  73. /** Take over ownership of content of source_value.
  74. Note: -If source_value has string or array that are set with SET_AS_STATIC, it will make new copies of those.
  75. -value will be nulled on source_value after this call. */
  76. void TakeOver(TBValue &source_value);
  77. /** Copy the content of source_value to this value.
  78. Note: This value will become TYPE_NULL if source_value holds an object. We can't copy objects. */
  79. void Copy(const TBValue &source_value);
  80. void SetNull();
  81. void SetInt(int val);
  82. void SetFloat(float val);
  83. /** Set the passed in string */
  84. void SetString(const char *val, SET set);
  85. /** Set the passed in object. Takes the ownership of the object! */
  86. void SetObject(TBTypedObject *object);
  87. /** Set the passed in array */
  88. void SetArray(TBValueArray *arr, SET set);
  89. /** Set the value either as a string, number or array of numbers, depending of the string syntax. */
  90. void SetFromStringAuto(const char *str, SET set);
  91. int GetInt() const;
  92. float GetFloat() const;
  93. const char *GetString();
  94. TBTypedObject *GetObject() const { return IsObject() ? val_obj : nullptr; }
  95. TBValueArray *GetArray() const { return IsArray() ? val_arr : nullptr; }
  96. TYPE GetType() const { return (TYPE) m_packed.type; }
  97. bool IsString() const { return m_packed.type == TYPE_STRING; }
  98. bool IsFloat() const { return m_packed.type == TYPE_FLOAT; }
  99. bool IsInt() const { return m_packed.type == TYPE_INT; }
  100. bool IsObject() const { return m_packed.type == TYPE_OBJECT; }
  101. bool IsArray() const { return m_packed.type == TYPE_ARRAY; }
  102. int GetArrayLength() const { return IsArray() ? val_arr->GetLength() : 0; }
  103. const TBValue& operator = (const TBValue &val) { Copy(val); return *this; }
  104. private:
  105. union {
  106. float val_float;
  107. int val_int;
  108. char *val_str;
  109. TBTypedObject *val_obj;
  110. TBValueArray *val_arr;
  111. };
  112. union {
  113. struct {
  114. uint32 type : 8;
  115. uint32 allocated : 1;
  116. } m_packed;
  117. uint32 m_packed_init;
  118. };
  119. };
  120. }; // namespace tb
  121. #endif // TB_VALUE_H