00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "smart/varobj.h"
00028 #include "smart/varfactory.h"
00029
00030 #include "smart/varmap.hpp"
00031 #include "smart/varvalue.hpp"
00032 #include "smart/byterep.hpp"
00033
00034 #include "base/memory.hpp"
00035 #include "base/common.hpp"
00036 #include "base/map.hpp"
00037 #include "base/list.hpp"
00038 #include "base/string.hpp"
00039 #include "base/numeric.h"
00040
00041 BEGIN_TERIMBER_NAMESPACE
00042 #pragma pack(4)
00043
00044 variant_factory::variant_factory() :
00045 _double_allocator(1024*128),
00046 _long_double_allocator(1024*128),
00047 _guid_allocator(1024*128)
00048 {
00049 }
00050
00051 variant_factory::~variant_factory()
00052 {
00053 }
00054
00055 bool
00056 variant_factory::create(vt_types type, var_value& val, size_t len)
00057 {
00058 switch (type)
00059 {
00060 case vt_unknown:
00061 case vt_enum:
00062 case vt_empty:
00063
00064 assert(false);
00065 return false;
00066 break;
00067 case vt_null:
00068
00069 memset(&val._value, 0, sizeof(terimber_xml_value));
00070 break;
00071 case vt_bool:
00072 case vt_sb1:
00073 case vt_ub1:
00074 case vt_sb2:
00075 case vt_ub2:
00076 case vt_sb4:
00077 case vt_ub4:
00078 case vt_float:
00079
00080 memset(&val._value, 0, sizeof(terimber_xml_value));
00081 break;
00082 #ifdef OS_64BIT
00083 case vt_double:
00084 case vt_sb8:
00085 case vt_ub8:
00086 case vt_date:
00087
00088 memset(&val._value, 0, sizeof(terimber_xml_value));
00089 break;
00090 #else
00091 case vt_double:
00092 val._value.dblVal = _double_allocator.allocate();
00093 break;
00094 case vt_date:
00095 case vt_sb8:
00096 val._value.intVal = (sb8_t*)_double_allocator.allocate();
00097 break;
00098 case vt_ub8:
00099 val._value.uintVal = (ub8_t*)_double_allocator.allocate();
00100 break;
00101 #endif
00102 case vt_string:
00103 {
00104 sb1_t* p = (sb1_t*)_byte_allocator->allocate(len + 1);
00105 p[len] = 0;
00106 val._value.strVal = (const char*)p;
00107 }
00108 break;
00109 case vt_wstring:
00110 {
00111 wchar_t* p = (wchar_t*)_byte_allocator->allocate(2*(len + 1));
00112 p[len] = 0;
00113 val._value.wstrVal = p;
00114 }
00115 break;
00116 case vt_decimal:
00117 case vt_numeric:
00118 val._value.bufVal = (const ub1_t*)_byte_allocator->allocate(len);
00119 break;
00120 case vt_binary:
00121 {
00122 ub1_t* p = (ub1_t*)_byte_allocator->allocate(len + sizeof(size_t));
00123 *(size_t*)p = len;
00124 val._value.bufVal = p;
00125 }
00126 break;
00127 case vt_guid:
00128 val._value.guidVal = _guid_allocator.allocate();
00129 break;
00130 }
00131
00132 return true;
00133 }
00134
00135 bool
00136 variant_factory::clone(vt_types type, var_value& out, const var_value& in)
00137 {
00138 out._not_null = in._not_null;
00139 memset(&out._value, 0, sizeof(terimber_xml_value));
00140
00141 switch (type)
00142 {
00143 case vt_unknown:
00144 case vt_enum:
00145 case vt_empty:
00146
00147 assert(false);
00148 return false;
00149 break;
00150 case vt_null:
00151
00152 break;
00153 case vt_bool:
00154 case vt_sb1:
00155 case vt_ub1:
00156 case vt_sb2:
00157 case vt_ub2:
00158 case vt_sb4:
00159 case vt_ub4:
00160 case vt_float:
00161
00162 memcpy(&out._value, &in._value, sizeof(terimber_xml_value));
00163 break;
00164 #ifdef OS_64BIT
00165 case vt_double:
00166 case vt_sb8:
00167 case vt_ub8:
00168 case vt_date:
00169
00170 memcpy(&out._value, &in._value, sizeof(terimber_xml_value));
00171 break;
00172 #else
00173 case vt_double:
00174 if (in._not_null)
00175 {
00176 double* p = _double_allocator.allocate();
00177 if (!p)
00178 return false;
00179
00180 *p = *in._value.dblVal;
00181 out._value.dblVal = p;
00182 }
00183 break;
00184 case vt_date:
00185 case vt_sb8:
00186 if (in._not_null)
00187 {
00188 sb8_t* p = (sb8_t*)_double_allocator.allocate();
00189 if (!p)
00190 return false;
00191
00192 *p = *in._value.intVal;
00193 out._value.intVal = p;
00194 }
00195 break;
00196 case vt_ub8:
00197 if (in._not_null)
00198 {
00199 ub8_t* p = (ub8_t*)_double_allocator.allocate();
00200 if (!p)
00201 return false;
00202
00203 *p = *in._value.uintVal;
00204 out._value.uintVal = p;
00205 }
00206 break;
00207 #endif
00208 case vt_string:
00209 if (in._not_null)
00210 {
00211 size_t len = in._value.strVal ? str_template::strlen(in._value.strVal) : 0;
00212 if (len)
00213 {
00214 char* p = (char*)_byte_allocator->allocate(len + 1);
00215 if (!p)
00216 return false;
00217
00218 p[len] = 0;
00219 memcpy(p, in._value.strVal, len);
00220 out._value.strVal = p;
00221 }
00222 }
00223 break;
00224 case vt_wstring:
00225 if (in._not_null)
00226 {
00227 size_t len = in._value.wstrVal ? str_template::strlen(in._value.wstrVal) : 0;
00228 if (len)
00229 {
00230 wchar_t* p = (wchar_t*)_byte_allocator->allocate(2*(len + 1));
00231 if (!p)
00232 return false;
00233
00234 p[len] = 0;
00235 memcpy(p, in._value.wstrVal, 2*len);
00236 out._value.wstrVal = p;
00237 }
00238 }
00239 break;
00240 case vt_decimal:
00241 case vt_numeric:
00242 if (in._not_null && in._value.bufVal)
00243 {
00244 _tmp_allocator.reset();
00245 numeric num(&_tmp_allocator);
00246 if (!num.parse_orcl(in._value.bufVal))
00247 return false;
00248
00249 size_t len = num.orcl_len();
00250
00251 if (len)
00252 {
00253 ub1_t* p = (ub1_t*)_byte_allocator->allocate(len);
00254 if (!p)
00255 return false;
00256
00257 if (!num.persist_orcl(p))
00258 return false;
00259
00260 out._value.bufVal = p;
00261 }
00262 }
00263 break;
00264 case vt_binary:
00265 if (in._not_null)
00266 {
00267 size_t len = in._value.bufVal ? *(size_t*)in._value.bufVal : 0;
00268 if (len)
00269 {
00270 ub1_t* p = (ub1_t*)_byte_allocator->allocate(len + sizeof(size_t));
00271 if (!p)
00272 return false;
00273
00274 *(size_t*)p = len;
00275 memcpy(p + sizeof(size_t), in._value.bufVal + sizeof(size_t), len);
00276 out._value.bufVal = p;
00277 }
00278 }
00279 break;
00280 case vt_guid:
00281 if (in._not_null && in._value.guidVal)
00282 {
00283 guid_t* p = _guid_allocator.allocate();
00284 if (!p)
00285 return false;
00286
00287 memcpy(p, in._value.guidVal, sizeof(guid_t));
00288 out._value.guidVal = p;
00289 }
00290 break;
00291 }
00292
00293 return true;
00294 }
00295
00296 void
00297 variant_factory::destroy(vt_types type, var_value& val)
00298 {
00299 switch (type)
00300 {
00301 case vt_unknown:
00302 case vt_enum:
00303 case vt_empty:
00304
00305 assert(false);
00306 break;
00307 case vt_null:
00308 break;
00309 case vt_bool:
00310 case vt_sb1:
00311 case vt_ub1:
00312 case vt_sb2:
00313 case vt_ub2:
00314 case vt_sb4:
00315 case vt_ub4:
00316 case vt_float:
00317 break;
00318 #ifdef OS_64BIT
00319 case vt_double:
00320 case vt_sb8:
00321 case vt_ub8:
00322 case vt_date:
00323 break;
00324 #else
00325 case vt_double:
00326 if (val._value.dblVal)
00327 _double_allocator.deallocate((void*)val._value.dblVal);
00328 break;
00329 case vt_date:
00330 case vt_sb8:
00331 if (val._value.intVal)
00332 _double_allocator.deallocate((void*)val._value.intVal);
00333 break;
00334 case vt_ub8:
00335 if (val._value.uintVal)
00336 _double_allocator.deallocate((void*)val._value.uintVal);
00337 break;
00338 #endif
00339 case vt_string:
00340 if (val._value.strVal)
00341 _byte_allocator->deallocate((char*)val._value.strVal);
00342 break;
00343 case vt_wstring:
00344 if (val._value.wstrVal)
00345 _byte_allocator->deallocate((char*)val._value.wstrVal);
00346 break;
00347 case vt_decimal:
00348 case vt_numeric:
00349 case vt_binary:
00350 if (val._value.bufVal)
00351 _byte_allocator->deallocate((char*)val._value.bufVal);
00352 break;
00353 case vt_guid:
00354 if (val._value.guidVal)
00355 _guid_allocator.deallocate((void*)val._value.guidVal);
00356 break;
00357 }
00358 }
00359
00361 void
00362 variant_factory::reset()
00363 {
00364 _byte_allocator->reset();
00365 _double_allocator.reset();
00366 _long_double_allocator.reset();
00367 _guid_allocator.reset();
00368 _tmp_allocator.reset();
00369 }
00370
00371 #pragma pack()
00372 END_TERIMBER_NAMESPACE