commore  1.0.6-SNAPSHOT
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Table.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2014 Raphael David / CANTOR
3 //
4 
5 #ifndef CMR_TABLE_INCLUDED
6 #define CMR_TABLE_INCLUDED
7 
8 
9 #include "Commore.h"
10 #include "Tuple.h"
11 
12 namespace commore
13 {
14  enum
15  {
17  };
18 }
19 
20 namespace commore
21 {
26  class CMREXD Table
27  {
28  public:
29  struct Header;
30  struct LineItem;
31  //
32  // Handle for line iterator
33  //
34  class CMREXD Line
35  {
36  friend class Table;
37 
38  public:
39  Line();
40  ~Line();
41 
42  public:
43  bool get_bool(const Symbol& att)
44  {
45  const Bool* r = (const Bool*)get_value(att, T_BOOL);
46  return *r;
47  }
48  bool& set_bool(const Symbol& att)
49  {
50  Bool* r = (Bool*)get_value(att, T_BOOL);
51  return *r;
52  }
53  bool& set_bool(const Symbol& att, bool v)
54  {
55  Bool* r = (Bool*)get_value(att, T_BOOL);
56  *r = v;
57  return *r;
58  }
59 
60  Int get_int(const Symbol& att)
61  {
62  Int* r = (Int*)get_value(att, T_INT);
63  return *r;
64  }
65  Int& set_int(const Symbol& att)
66  {
67  Int* r = (Int*)get_value(att, T_INT);
68  return *r;
69  }
70  Int& set_int(const Symbol& att, Int v)
71  {
72  Int* r = (Int*)get_value(att, T_INT);
73  *r = v;
74  return *r;
75  }
76 
77  const ListInt& get_list_int(const Symbol& att) const
78  {
79  const ListInt* r = (const ListInt*)get_value(att, T_LIST_INT);
80  return *r;
81  }
83  {
84  ListInt* r = (ListInt*)get_value(att, T_LIST_INT);
85  return *r;
86  }
87  ListInt& set_list_int(const Symbol& att, const ListInt& v)
88  {
89  ListInt* r = (ListInt*)get_value(att, T_LIST_INT);
90  return *r = v;
91  }
92  Int& add_int(const Symbol& att, Int v)
93  {
94  ListInt* r = (ListInt*)get_value(att, T_LIST_INT);
95  return r->add(v);
96  }
97  Int& add_int(const Symbol& att)
98  {
99  ListInt* r = (ListInt*)get_value(att, T_LIST_INT);
100  return r->add();
101  }
102 
103  const AString& get_astring(const Symbol& att)
104  {
105  const AString* r = (const AString*)get_value(att, T_ASTRING);
106  return *r;
107  }
109  {
110  AString* r = (AString*)get_value(att, T_ASTRING);
111  return *r;
112  }
113  AString& set_astring(const Symbol& att, const AChar* v)
114  {
115  AString* r = (AString*)get_value(att, T_ASTRING);
116  *r = v;
117  return *r;
118  }
119 
120  const Tuple& get_tuple(const Symbol& att)
121  {
122  const Tuple* r = (const Tuple*)get_value(att, T_TUPLE);
123  return *r;
124  }
125  Tuple& set_tuple(const Symbol& att)
126  {
127  Tuple* r = (Tuple*)get_value(att, T_TUPLE);
128  return *r;
129  }
130  Tuple& set_tuple(const Symbol& att, const Tuple& v)
131  {
132  Tuple* r = (Tuple*)get_value(att, T_TUPLE);
133  *r = v;
134  return *r;
135  }
136 
137  bool next();
138  bool prev();
139  bool more() const;
140  bool erase();
141 
142  private:
143  const void* get_value(const Symbol& att, Types type) const;
144  void* get_value(const Symbol& att, Types type);
145  void* get_value(int index, Types type);
146 
147  private:
148  Table* table_;
149  LineItem* line_;
150 
151  };
152  friend class Line;
153 
154 
155  //
156  // Const line iterator
157  //
159  {
160  friend class Table;
161 
162  public:
163  ConstLine();
164  ~ConstLine();
165 
166  public:
167  bool get_bool(const Symbol& att) const
168  {
169  const Bool* r = (const Bool*)get_value(att, T_BOOL);
170  return *r;
171  }
172  Int get_int(const Symbol& att) const
173  {
174  Int* r = (Int*)get_value(att, T_INT);
175  return *r;
176  }
177  const AString& get_astring(const Symbol& att) const
178  {
179  const AString* r = (const AString*)get_value(att, T_ASTRING);
180  return *r;
181  }
182  const Tuple& get_tuple(const Symbol& att) const
183  {
184  Tuple* r = (Tuple*)get_value(att, T_TUPLE);
185  return *r;
186  }
187 
188  bool next();
189  bool prev();
190  bool more() const;
191 
192  private:
193  const void* get_value(const Symbol& att, Types type) const;
194  const void* get_value(int index, Types type) const;
195 
196  private:
197  const Table* table_;
198  const LineItem* line_;
199 
200  };
201  friend class ConstLine;
202 
203 
204  public:
205  Table();
206  Table(const Symbol& name);
207  Table(const Table&);
208  Table& operator = (const Table& t)
209  {
210  return assign(t);
211  }
212  ~Table();
213 
214  public:
215  Table& assign(const Table& t);
216 
218  // Remove lines content
219  // if all : remove column specifications
220  //
221  void clear(bool all = false);
222  //
223  // Manage table column
224  //
225  int add_column(const Symbol& name, Types type);
226  int add_int_col(const Symbol& name)
227  {
228  return add_column(name, T_INT);
229  }
230  int add_list_int_col(const Symbol& name)
231  {
232  return add_column(name, T_LIST_INT);
233  }
234  int add_bool_col(const Symbol& name)
235  {
236  return add_column(name, T_BOOL);
237  }
238  int add_astring_col(const Symbol& name)
239  {
240  return add_column(name, T_ASTRING);
241  }
242  int add_tuple_col(const Symbol& name)
243  {
244  return add_column(name, T_TUPLE);
245  }
246  int remove_column(const Symbol& name);
247  int compare(const Table& t) const;
248  Line begin();
249  Line end();
250  ConstLine begin() const;
251  ConstLine end() const;
252  Line add_line();
253  const Symbol& get_name() const;
254  size_t size() const;
255  size_t line_size() const;
256  int write_xml(OBStream& o) const;
257  int read_xml(IBStream& i);
258  int write(CommBuffer& buffer) const;
259  int read(const CommBuffer& buffer);
260 
261  private:
262  Line add_line(const LineItem* aline);
263 
264  private:
265  Header* header_;
266  };
267 }
268 
270 
271 #endif
int add_bool_col(const Symbol &name)
Definition: Table.h:234
Tuple & set_tuple(const Symbol &att)
Definition: Table.h:125
const Tuple & get_tuple(const Symbol &att) const
Definition: Table.h:182
Definition: Tuple.h:29
Definition: AString.h:39
bool Bool
Definition: Type.h:35
long CMREXD write_xml(const AString &sIn, OBStream &o)
Definition: AString.cpp:27
AString & set_astring(const Symbol &att)
Definition: Table.h:108
ListInt & set_list_int(const Symbol &att, const ListInt &v)
Definition: Table.h:87
AString & set_astring(const Symbol &att, const AChar *v)
Definition: Table.h:113
Definition: List.h:23
CMREXD commore::OBStream & operator<<(commore::OBStream &o, const commore::Table &b)
Definition: Table.cpp:727
bool get_bool(const Symbol &att)
Definition: Table.h:43
Tuple & set_tuple(const Symbol &att, const Tuple &v)
Definition: Table.h:130
int add_int_col(const Symbol &name)
Definition: Table.h:226
Definition: Table.cpp:24
bool read(IBStream &str, int &num)
Definition: Time.cpp:200
Definition: Symbol.h:18
Definition: Table.h:16
Int & set_int(const Symbol &att, Int v)
Definition: Table.h:70
char AChar
Definition: Type.h:65
Int get_int(const Symbol &att)
Definition: Table.h:60
Int get_int(const Symbol &att) const
Definition: Table.h:172
int add_tuple_col(const Symbol &name)
Definition: Table.h:242
#define CMREXD
Definition: Compiler.h:22
Definition: Type.h:137
ListInt & set_list_int(const Symbol &att)
Definition: Table.h:82
Definition: Table.h:158
Definition: Type.h:155
Int & set_int(const Symbol &att)
Definition: Table.h:65
Definition: Type.h:136
bool & set_bool(const Symbol &att)
Definition: Table.h:48
T & add()
Definition: List.h:319
const Tuple & get_tuple(const Symbol &att)
Definition: Table.h:120
Definition: CommBuffer.h:28
int Int
Definition: Type.h:37
Definition: Table.h:34
const AString & get_astring(const Symbol &att) const
Definition: Table.h:177
long CMREXD read_xml(AString &sOut, IBStream &i)
Definition: AString.cpp:65
Definition: Type.h:142
bool get_bool(const Symbol &att) const
Definition: Table.h:167
Definition: Type.h:152
Definition: Table.cpp:100
const AString & get_astring(const Symbol &att)
Definition: Table.h:103
Definition: IOBStream.h:166
Definition: Table.h:26
int add_astring_col(const Symbol &name)
Definition: Table.h:238
Definition: IOBStream.h:29
const ListInt & get_list_int(const Symbol &att) const
Definition: Table.h:77
Int & add_int(const Symbol &att, Int v)
Definition: Table.h:92
Int & add_int(const Symbol &att)
Definition: Table.h:97
bool & set_bool(const Symbol &att, bool v)
Definition: Table.h:53
int add_list_int_col(const Symbol &name)
Definition: Table.h:230
Types
Definition: Type.h:131