commore  1.0.6-SNAPSHOT
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TypeManager.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2006-2007 Raphael David / CANTOR
3 //
4 
5 #ifndef CMR_TYPE_MANAGER_INCLUDED
6 #define CMR_TYPE_MANAGER_INCLUDED
7 
8 #include "commore/Commore.h"
9 #include "commore/Symbol.h"
10 #include "commore/Tuple.h"
11 #include "commore/CommBuffer.h"
12 
13 #include "ArrayImpl.h"
14 
15 //
16 // TypeManager is the base class for TypeManagerInfo
17 // It provides the static method get_type_info for getting
18 // informations about commore types.
19 //
21 {
22  TypeManager(commore::Types type, bool isList, bool complex, const char* typeName, bool isWord);
23  virtual ~TypeManager() {}
24 
25  virtual long write(commore::CommBuffer& b, void*) = 0;
26  virtual long read(const commore::CommBuffer& b, void*) = 0;
27  virtual long write_xml(commore::OBStream& o, void*) = 0;
28  virtual long read_xml(commore::XmlParser& i, void*) = 0;
29 
30  virtual bool equal(void* v1, void* v2) = 0;
31 
32  virtual void assign(void* v1, void* v2) = 0;
33 
34 
35  virtual size_t size()
36  {
37  return 0;
38  };
39  virtual void construct(commore::Byte*, const commore::Byte* v = 0) {};
40  virtual void destroy(void*) {};
41  virtual int compare(const commore::Dummy&, const commore::Dummy&)
42  {
43  return 0;
44  }
45  virtual void* get_default()
46  {
47  return 0;
48  }
49 
51  static TypeManager* get_type_info(const commore::AChar* type_name);
52 
53  //
54  // All T_List types
55  //
56  bool isList_;
57 
58  //
59  // Only Tuple is complex
60  //
61  bool complex_;
62 
63  //
64  // bool, int, long, float, double, timeperiod, timedate and all associated lists
65  //
66  bool isWord_;
67 
69 
70  //
71  // Short name for type name
72  //
74 
75  size_t hash_;
76 
77 };
79 
80 //
81 // TypeManagerInfo object represents meta-informations about a primitive type.
82 // The class maintains a cache of TypeManagerInfo object, one per type, accessible
83 // with the static get_type_info method.
84 //
85 template< class T > struct TypeManagerInfo : public TypeManager
86 {
87  TypeManagerInfo(commore::Types type, const char* typeName, bool isWord, bool isList = false, bool complex = false)
88  : TypeManager(type, isList, complex, typeName, isWord) {}
89 
90  long write_xml(commore::OBStream& o, void*);
91  long read_xml(commore::XmlParser& i, void*);
92  long write(commore::CommBuffer& b, void* value)
93  {
94  long r = 0;
95  T* avalue = (T*)value;
96  r += b.write(*avalue);
97  return r;
98  }
99  long read(const commore::CommBuffer& b, void* value)
100  {
101  long r = b.read(*(T*)value);
102  return r;
103  }
104  void assign(void* v1, void* v2)
105  {
106  *(T*)v1 = *(T*)v2;
107  }
108  bool equal(void* v1, void* v2)
109  {
110  return (bool)(*(T*)v1 == *(T*)v2);
111  }
112  size_t size()
113  {
114  size_t r = sizeof(T);
115  r = r + (sizeof(int) - r % sizeof(int));
116  return r;
117  }
118  void construct(commore::Byte* data, const commore::Byte* value)
119  {
120  if (value)
121  {
122  new(data) T(*(T*)value);
123  }
124  else
125  {
126  new(data) T();
127  }
128  }
129  void destroy(void* data)
130  {
131  ((T*)data)->T::~T();
132  }
133  int compare(const commore::Dummy& v1, const commore::Dummy& v2)
134  {
135  const T& ov1 = (const T&)v1;
136  const T& ov2 = (const T&)v2;
137  if (ov1 < ov2)
138  {
139  return -1;
140  }
141  else if (ov1 == ov2)
142  {
143  return 0;
144  }
145  else
146  {
147  return 1;
148  }
149  }
150  void* get_default()
151  {
152  static T default_;
153  return &default_;
154  }
155 };
156 
157 
158 
159 #endif
long write_xml(commore::OBStream &o, void *)
commore::Symbol symbType_
Definition: TypeManager.h:73
long write(commore::CommBuffer &b, void *value)
Definition: TypeManager.h:92
virtual long write(commore::CommBuffer &b, void *)=0
Definition: Parser.h:229
long read(const commore::CommBuffer &b, void *value)
Definition: TypeManager.h:99
commore::Types type_
Definition: TypeManager.h:68
bool isWord_
Definition: TypeManager.h:66
static TypeManager * get_type_info(commore::Types type)
Definition: TypeManager.cpp:92
void assign(void *v1, void *v2)
Definition: TypeManager.h:104
bool complex_
Definition: TypeManager.h:61
Definition: Symbol.h:18
char AChar
Definition: Type.h:65
bool equal(void *v1, void *v2)
Definition: TypeManager.h:108
virtual long read_xml(commore::XmlParser &i, void *)=0
long write(const AString &v)
Definition: CommBuffer.cpp:925
TypeManager * PTypeManager
Definition: TypeManager.h:78
void construct(commore::Byte *data, const commore::Byte *value)
Definition: TypeManager.h:118
void * get_default()
Definition: TypeManager.h:150
virtual ~TypeManager()
Definition: TypeManager.h:23
virtual long write_xml(commore::OBStream &o, void *)=0
void destroy(void *data)
Definition: TypeManager.h:129
virtual long read(const commore::CommBuffer &b, void *)=0
long read_xml(commore::XmlParser &i, void *)
TypeManager(commore::Types type, bool isList, bool complex, const char *typeName, bool isWord)
Definition: TypeManager.cpp:33
Definition: CommBuffer.h:28
int compare(const commore::Dummy &v1, const commore::Dummy &v2)
Definition: TypeManager.h:133
virtual int compare(const commore::Dummy &, const commore::Dummy &)
Definition: TypeManager.h:41
virtual void construct(commore::Byte *, const commore::Byte *v=0)
Definition: TypeManager.h:39
virtual bool equal(void *v1, void *v2)=0
long read(AString &v) const
Definition: CommBuffer.cpp:1277
unsigned char Byte
Definition: Type.h:64
virtual void destroy(void *)
Definition: TypeManager.h:40
virtual void assign(void *v1, void *v2)=0
virtual void * get_default()
Definition: TypeManager.h:45
Definition: IOBStream.h:166
bool isList_
Definition: TypeManager.h:56
Definition: TypeManager.h:20
virtual size_t size()
Definition: TypeManager.h:35
Definition: TypeManager.h:85
TypeManagerInfo(commore::Types type, const char *typeName, bool isWord, bool isList=false, bool complex=false)
Definition: TypeManager.h:87
size_t hash_
Definition: TypeManager.h:75
size_t size()
Definition: TypeManager.h:112
Types
Definition: Type.h:131