Hasty Badger
Small UI library (a branch of Turbo Badger)
 All Classes Namespaces Functions Variables Enumerations Enumerator Friends Groups Pages
tb_value.h
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 
6 #ifndef TB_VALUE_H
7 #define TB_VALUE_H
8 
9 #include "tb_core.h"
10 #include "tb_list.h"
11 #include "tb_str.h"
12 
13 namespace tb {
14 
15 class TBValue;
16 class TBTypedObject;
17 
20 bool is_start_of_number(const char *str);
21 
24 bool contains_non_trailing_space(const char *str);
25 
30 bool is_number_only(const char *str);
31 
34 bool is_number_float(const char *str);
35 
38 {
39 public:
40  TBValueArray();
41  ~TBValueArray();
42  TBValue *AddValue();
43  TBValue *GetValue(int index);
44  bool Equals(const TBValueArray & rhs) const;
45  static TBValueArray *Clone(TBValueArray *source);
46  int GetLength() const { return m_list.GetNumItems(); }
47 private:
49 };
50 
59 class TBValue
60 {
61 public:
64  enum TYPE {
65  TYPE_NULL,
66  TYPE_STRING,
67  TYPE_FLOAT,
68  TYPE_INT,
69  TYPE_OBJECT,
70  TYPE_ARRAY
71  };
72 
74  enum SET_MODE {
78  };
79 
80  TBValue();
81  TBValue(TBValue && val) = default;
82  TBValue(const TBValue &value);
83  TBValue(TYPE type);
84 
85  TBValue(int value) : TBValue((long)value) {}
86  TBValue(long value);
87  TBValue(double value);
88  TBValue(const char *value, SET_MODE set = SET_NEW_COPY);
89  TBValue(const TBStr & str) : TBValue((const char *)str) {}
90  TBValue(TBStr && str);
91  TBValue(TBTypedObject *object, SET_MODE set = SET_TAKE_OWNERSHIP);
92 
93  ~TBValue();
94 
98  void TakeOver(TBValue &source_value);
99 
102  void Copy(const TBValue &source_value);
103 
104  void SetNull();
105  void SetInt(long val);
106  void SetFloat(double val);
107 
109  void SetString(const char *val, SET_MODE set);
110 
112  void SetString(const TBStr & str) { SetString(str.CStr(), SET_NEW_COPY); }
113 
115  void SetObject(TBTypedObject *object, SET_MODE set);
116 
118  void SetArray(TBValueArray *arr, SET_MODE set);
119 
121  void SetFromStringAuto(const char *str, SET_MODE set);
122 
123  long GetInt() const;
124  double GetFloat() const;
125  TBStr GetString() const;
126  TBTypedObject *GetObject() const { return IsObject() ? val_obj : nullptr; }
127  TBValueArray *GetArray() const { return IsArray() ? val_arr : nullptr; }
128 
129  bool Equals(int value) const { return Equals((long)value); }
130  bool Equals(long value) const;
131  bool Equals(double value) const;
132  bool Equals(const char * value) const;
133  bool Equals(const TBStr & value) const { return Equals(value.CStr()); }
134  bool Equals(const TBValue & value) const;
135 
136  TYPE GetType() const { return (TYPE) m_packed.type; }
137  bool IsString() const { return m_packed.type == TYPE_STRING; }
138  bool IsFloat() const { return m_packed.type == TYPE_FLOAT; }
139  bool IsInt() const { return m_packed.type == TYPE_INT; }
140  bool IsObject() const { return m_packed.type == TYPE_OBJECT; }
141  bool IsArray() const { return m_packed.type == TYPE_ARRAY; }
142  int GetArrayLength() const { return IsArray() ? val_arr->GetLength() : 0; }
143 
144  const TBValue& operator = (const TBValue &val) { Copy(val); return *this; }
145  TBValue & operator = (TBValue && val) = default;
146  /*{
147  std::swap(val_float, val.val_float);
148  std::swap(m_packed_init, val.m_packed_init);
149  return *this;
150  }*/
151 
152 private:
153  union {
154  double val_float;
155  long val_int;
156  char *val_str;
157  TBTypedObject *val_obj;
158  TBValueArray *val_arr;
159  };
160  union {
161  struct {
162  uint32_t type : 8;
163  uint32_t allocated : 1;
164  } m_packed;
165  uint32_t m_packed_init;
166  };
167 };
168 
169 } // namespace tb
170 
171 #endif // TB_VALUE_H
bool is_number_only(const char *s)
Return true if the string can be represented as a number.
Definition: tb_value.cpp:52
void SetObject(TBTypedObject *object, SET_MODE set)
Set the passed in object.
Definition: tb_value.cpp:267
TYPE
The current type of the value.
Definition: tb_value.h:64
void SetArray(TBValueArray *arr, SET_MODE set)
Set the passed in array.
Definition: tb_value.cpp:278
SET_MODE
How to deal with the dynamic memory when setting string and array.
Definition: tb_value.h:74
void TakeOver(TBValue &source_value)
Take over ownership of content of source_value.
Definition: tb_value.cpp:194
void SetFromStringAuto(const char *str, SET_MODE set)
Set the value either as a string, number or array of numbers, depending of the string syntax...
Definition: tb_value.cpp:291
void SetString(const char *val, SET_MODE set)
Set the passed in string.
Definition: tb_value.cpp:254
The data passed in will be stored and freed.
Definition: tb_value.h:76
TBValueArray is an array of TBValue.
Definition: tb_value.h:37
bool is_start_of_number(const char *str)
Return true if the given string starts with a number.
Definition: tb_value.cpp:32
bool contains_non_trailing_space(const char *str)
Returns true if the given string contains space that is not at the end of the string.
Definition: tb_value.cpp:41
A new copy of the data will be made.
Definition: tb_value.h:75
TBListAutoDeleteOf is a list (array) of pointers to the specified object type.
Definition: tb_list.h:118
Definition: tb_object.h:21
TBStr is a simple string class.
Definition: tb_str.h:62
TBValue holds value of a specific type.
Definition: tb_value.h:59
bool is_number_float(const char *str)
Return true if the given number string is a float number.
Definition: tb_value.cpp:63
char * CStr() const
Get the c-string raw (char *)
Definition: tb_str.h:93
void Copy(const TBValue &source_value)
Copy the content of source_value to this value.
Definition: tb_value.cpp:205
void SetString(const TBStr &str)
Set the passed in string.
Definition: tb_value.h:112
The data passed in will be stored but never freed.
Definition: tb_value.h:77