My Project
NPLTable.h
1 #pragma once
2 #include "util/intrusive_ptr.h"
3 #include <string>
4 #include <map>
5 
6 #ifdef _MSC_VER
7 #pragma warning( push )
8 #pragma warning( disable:4275 )
9 #pragma warning( disable:4251 )
10 #endif
11 
12 namespace NPL
13 {
14  class NPLObjectBase;
15  class NPLNumberObject;
16  class NPLBoolObject;
17  class NPLTable;
18  class NPLStringObject;
19  class NPLObjectProxy;
20  typedef ParaIntrusivePtr<NPLObjectBase> NPLObjectBase_ptr;
21  typedef ParaIntrusivePtr<NPLTable> NPLTable_ptr;
22  typedef ParaIntrusivePtr<NPLNumberObject> NPLNumberObject_ptr;
23  typedef ParaIntrusivePtr<NPLBoolObject> NPLBoolObject_ptr;
24  typedef ParaIntrusivePtr<NPLStringObject> NPLStringObject_ptr;
25 
28  {
29  public:
30  typedef std::map<std::string, NPLObjectProxy> TableFieldMap_Type;
31  typedef std::map<int, NPLObjectProxy> TableIntFieldMap_Type;
32  typedef TableFieldMap_Type::iterator Iterator_Type;
33  typedef TableIntFieldMap_Type::iterator IndexIterator_Type;
34 
35  enum NPLObjectType
36  {
37  NPLObjectType_Nil,
38  NPLObjectType_Table,
39  NPLObjectType_Number,
40  NPLObjectType_String,
41  NPLObjectType_Bool,
42  };
43  NPLObjectBase():m_type(NPLObjectType_Nil){};
44  virtual ~NPLObjectBase(){};
45 
46  inline NPLObjectType GetType(){return m_type;}
47 
48  bool isTable() { return GetType() == NPLObjectType_Table; }
49  bool isNumber() { return GetType() == NPLObjectType_Number; }
50  bool isString() { return GetType() == NPLObjectType_String; }
51  bool isNil() { return GetType() == NPLObjectType_Nil; }
52  bool isBool() { return GetType() == NPLObjectType_Bool; }
53 
54  protected:
55  NPLObjectType m_type;
56  };
57 
73  class PE_CORE_DECL NPLObjectProxy : public NPLObjectBase_ptr
74  {
75  public:
76  NPLObjectProxy() {};
77  NPLObjectProxy( NPLObjectBase* pObject);
78 
79  operator double ();
80 
81  void operator = (double value);
82 
83  operator bool ();
84 
85  void operator = (bool value);
86 
87  operator const string& ();
88  const char* c_str();
89  int toInt();
90 
91  bool operator == (const string& value);
92  bool operator == (const char* value);
93 
94  void operator = (const std::string& value);
95  void operator = (const char* value);
96 
98  NPLObjectProxy& operator [] (const string& sName);
99 
100  NPLObjectProxy& operator [] (const char* sName);
101 
102  NPLObjectProxy& operator [](int nIndex);
103 
105  NPLObjectProxy GetField(const string& sName);
106 
108  NPLObjectProxy GetField(const char* sName);
109 
110 
115  void SetField(const string& sName, const NPLObjectProxy& pObject);
116 
117  void SetField(int nIndex, const NPLObjectProxy& pObject);
118 
120  void MakeNil();
121 
122  NPLObjectBase::Iterator_Type begin();
123  NPLObjectBase::Iterator_Type end();
124 
125  NPLObjectBase::IndexIterator_Type index_begin();
126  NPLObjectBase::IndexIterator_Type index_end();
127 
129  NPLObjectBase::NPLObjectType GetType();
130  };
131 
132 
134  class PE_CORE_DECL NPLNumberObject : public NPLObjectBase
135  {
136  public:
137  NPLNumberObject():m_value(0){m_type = NPLObjectType_Number;};
139  NPLNumberObject(double value):m_value(value){m_type = NPLObjectType_Number;};
140 
141  virtual ~NPLNumberObject(){};
142 
143  void SetValue(double value){m_value = value;}
144  double GetValue() {return m_value;}
145 
146  NPLNumberObject& operator = (double value) { SetValue(value); return *this;}
147 
148  private:
149  double m_value;
150  };
151 
152 
154  class PE_CORE_DECL NPLBoolObject : public NPLObjectBase
155  {
156  public:
157  NPLBoolObject():m_value(false){m_type = NPLObjectType_Bool;};
159  NPLBoolObject(bool value):m_value(value){m_type = NPLObjectType_Bool;};
160 
161  virtual ~NPLBoolObject(){};
162 
163  inline void SetValue(bool value){m_value = value;}
164  bool GetValue() {return m_value;}
165 
166  NPLBoolObject& operator = (bool value) { SetValue(value); return *this;}
167 
168  private:
169  bool m_value;
170  };
171 
172 
174  class PE_CORE_DECL NPLStringObject : public NPLObjectBase
175  {
176  public:
177  NPLStringObject(){m_type = NPLObjectType_String;};
179  NPLStringObject(const std::string& value):m_value(value){m_type = NPLObjectType_String;};
180 
181  virtual ~NPLStringObject(){};
182 
183  void SetValue(const std::string& value){m_value = value;}
184  std::string& GetValue(){return m_value;}
185 
186  NPLStringObject& operator = (const std::string& value) { SetValue(value); return *this;}
187 
188  private:
189  std::string m_value;
190  };
191 
192 
213  class PE_CORE_DECL NPLTable : public NPLObjectBase
214  {
215  public:
217  NPLTable(){m_type = NPLObjectType_Table;};
218 
219  virtual ~NPLTable();
220  public:
222  void ToString(std::string& str);
223 
225  void Clear();
226 
231  void SetField(const string& sName, const NPLObjectProxy& pObject);
232 
233  void SetField(int nIndex, const NPLObjectProxy& pObject);
234 
236  NPLObjectProxy GetField(const string& sName);
237  NPLObjectProxy GetField(int nIndex);
238 
240  NPLObjectProxy& CreateGetField(const string& sName);
241  NPLObjectProxy& CreateGetField(int nIndex);
242 
243  Iterator_Type begin() {return m_fields.begin();};
244  Iterator_Type end() {return m_fields.end();};
245 
246  IndexIterator_Type index_begin() { return m_index_fields.begin(); };
247  IndexIterator_Type index_end() { return m_index_fields.end(); };
248 
250  NPLObjectProxy& operator [](const string& sName) {return CreateGetField(sName);};
251  NPLObjectProxy& operator [](const char* sName) {return CreateGetField(sName);};
252  NPLObjectProxy& operator [](int nIndex) {return CreateGetField(nIndex);};
253  private:
254  TableFieldMap_Type m_fields;
255  TableIntFieldMap_Type m_index_fields;
256  };
257 }
258 
259 // required by DLL interface
260 //EXPIMP_TEMPLATE template class PE_CORE_DECL ParaIntrusivePtr<NPL::NPLObjectBase>;
261 //EXPIMP_TEMPLATE template class PE_CORE_DECL ParaIntrusivePtr<NPL::NPLTable>;
262 //EXPIMP_TEMPLATE template class PE_CORE_DECL ParaIntrusivePtr<NPL::NPLNumberObject>;
263 //EXPIMP_TEMPLATE template class PE_CORE_DECL ParaIntrusivePtr<NPL::NPLBoolObject>;
264 //EXPIMP_TEMPLATE template class PE_CORE_DECL ParaIntrusivePtr<NPL::NPLStringObject>;
265 
266 #ifdef _MSC_VER
267 #pragma warning( pop )
268 #endif
NPLNumberObject(double value)
create the table from a serialized string.
Definition: NPLTable.h:139
define this to enable debugging of NPL code in visual studio
Definition: INPL.h:9
NPL object proxy.
Definition: NPLTable.h:73
base class for all NPL date members.
Definition: NPLTable.h:27
this is a pure c++ implementation of lua table.
Definition: NPLTable.h:213
a floating point number
Definition: NPLTable.h:174
a floating point number
Definition: NPLTable.h:134
NPLBoolObject(bool value)
create the table from a serialized string.
Definition: NPLTable.h:159
Definition: enum_maker.hpp:46
NPLTable()
this is an empty table
Definition: NPLTable.h:217
single-threaded reference counted base class for boost::intrusive_ptr all boost::intrusive_ptr<T>, should derive from this class.
Definition: intrusive_ptr.h:75
NPLStringObject(const std::string &value)
create the table from a serialized string.
Definition: NPLTable.h:179
a boolean
Definition: NPLTable.h:154