00001 // All-Type implementation -*- C++ -*- 00002 00003 // Copyright (C) 2003 Hermann Schichl 00004 // 00005 // This file is part of the Vienna Database Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the Library GNU General Public License as published by the 00008 // Free Software Foundation; either version 2, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // Library GNU General Public License for more details. 00015 00016 // As a special exception, you may use this file as part of a free software 00017 // library without restriction. Specifically, if other files instantiate 00018 // templates or use macros or inline functions from this file, or you compile 00019 // this file and link it with other files to produce an executable, this 00020 // file does not by itself cause the resulting executable to be covered by 00021 // the Library GNU General Public License. This exception does not however 00022 // invalidate any other reasons why the executable file might be covered by 00023 // the Library GNU General Public License. 00024 00030 #ifndef __VDBL_ALLTYPE_H 00031 #define __VDBL_ALLTYPE_H 00032 00033 #include <vdbl_config.h> 00034 #include <vdbl_types.h> 00035 #include <string> 00036 #include <vector> 00037 #include <typeinfo> 00038 00039 __VDBL_BEGIN_NAMESPACE 00040 00041 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) 00042 #pragma set woff 1209 00043 #endif 00044 00045 00047 00054 class _VDBL_alltype_base 00055 { 00056 public: 00060 _VDBL_alltype_base() {} 00064 virtual ~_VDBL_alltype_base() {} 00065 00069 virtual const std::type_info& get_type() const VDBL_PURE_VIRTUAL 00070 00072 00076 virtual bool operator==(const _VDBL_alltype_base &p) VDBL_PURE_VIRTUAL 00077 virtual bool operator!=(const _VDBL_alltype_base &p) VDBL_PURE_VIRTUAL 00079 }; 00080 00082 00089 template <class _R> 00090 class _VDBL_alltype : public _VDBL_alltype_base 00091 { 00092 public: 00094 typedef _R cont_type; 00095 00096 private: 00098 typedef _VDBL_alltype<_R> _Self; 00099 00100 private: 00104 cont_type* _RC_p; 00105 00106 public: 00110 _VDBL_alltype() : _RC_p(NULL) {} 00111 00116 _VDBL_alltype(const cont_type& _p) : _RC_p(new cont_type(_p)) {} 00117 00123 _VDBL_alltype(cont_type* _p) : _RC_p(_p) {} 00124 00128 virtual ~_VDBL_alltype() { delete _RC_p; } 00129 00134 const std::type_info& get_type() const { return typeid(_R); } 00135 00139 const cont_type& content() const { return *_RC_p; } 00140 00142 00148 void operator=(const void *p) 00149 { delete _RC_p; _RC_p = new cont_type((cont_type*) p); } 00150 00151 void operator=(const cont_type *p) 00152 { delete _RC_p; _RC_p = new cont_type((cont_type*) p); } 00154 00156 00160 bool operator==(const _Self &p) 00161 { return *_RC_p == p; } 00162 00163 bool operator!=(const _Self &p) 00164 { return *_RC_p != p; } 00166 }; 00167 00168 class _VDBL_dateinterval; 00169 00171 00175 class _VDBL_date 00176 { 00177 public: 00181 int timezone; 00183 00187 int year; 00188 signed char month, day; 00189 unsigned int seconds; 00190 unsigned int microseconds; 00192 00196 _VDBL_dateinterval operator-(const _VDBL_date& _v) const; 00197 00199 00202 _VDBL_date& operator-(const _VDBL_dateinterval& _v) const; 00203 _VDBL_date& operator-=(const _VDBL_dateinterval& _v); 00204 _VDBL_date& operator+(const _VDBL_dateinterval& _v) const; 00205 _VDBL_date& operator+=(const _VDBL_dateinterval& _v); 00207 00208 #if 0 00211 _VDBL_date& operator=(const _VDBL_date& _v) 00212 { 00213 timezone = _v.timezone; 00214 year = _v.year; 00215 month = _v.month; 00216 day = _v.day; 00217 seconds = _v.seconds; 00218 microseconds = _v.microseconds; 00219 return *this; 00220 } 00221 #endif 00222 }; 00223 00224 #if 0 00225 _VDBL_date& operator=(_VDBL_date& _a, const _VDBL_date& _v) 00226 { 00227 _a.timezone = _v.timezone; 00228 _a.year = _v.year; 00229 _a.month = _v.month; 00230 _a.day = _v.day; 00231 _a.seconds = _v.seconds; 00232 _a.microseconds = _v.microseconds; 00233 return _a; 00234 } 00235 #endif 00236 00238 00243 class _VDBL_dateinterval 00244 { 00245 public: 00247 00251 int years; 00252 short int days; 00253 int seconds; 00254 int microseconds; 00256 00258 00262 _VDBL_dateinterval operator*(double d) const; 00263 _VDBL_dateinterval operator/(double d) const; 00264 _VDBL_dateinterval& operator*=(double d); 00265 _VDBL_dateinterval& operator/=(double d); 00267 00269 00272 _VDBL_dateinterval& operator-(const _VDBL_dateinterval& _v) const; 00273 _VDBL_dateinterval& operator-=(const _VDBL_dateinterval& _v); 00274 _VDBL_dateinterval& operator+(const _VDBL_dateinterval& _v) const; 00275 _VDBL_dateinterval& operator+=(const _VDBL_dateinterval& _v); 00277 00281 _VDBL_date operator+(const _VDBL_date& _d) const; 00282 }; 00283 00284 #if 0 00285 _VDBL_dateinterval& operator=(_VDBL_dateinterval& _a, 00286 const _VDBL_dateinterval& _v) 00287 { 00288 _a.years = _v.years; 00289 _a.days = _v.days; 00290 _a.seconds = _v.seconds; 00291 _a.microseconds = _v.microseconds; 00292 return _a; 00293 } 00294 #endif 00295 00297 00316 class _VDBL_mixtype 00317 { 00318 public: 00320 00324 #define VDBL_MIXTYPE_EMPTY -1 00325 #define VDBL_MIXTYPE_ALLOCED_NONE 0 00326 #define VDBL_MIXTYPE_ALLOCED_B 1 00327 #define VDBL_MIXTYPE_ALLOCED_N 2 00328 #define VDBL_MIXTYPE_ALLOCED_D 3 00329 #define VDBL_MIXTYPE_ALLOCED_U 4 00330 #define VDBL_MIXTYPE_ALLOCED_S 5 00331 00332 00333 private: 00337 union 00338 { 00339 bool nb; 00340 int nn; 00341 unsigned int nu; 00342 double nd; 00343 _VDBL_date dt; 00344 _VDBL_dateinterval di; 00345 std::string* s; 00346 std::vector<bool>* b; 00347 std::vector<int>* n; 00348 std::vector<unsigned int>* u; 00349 std::vector<double>* d; 00350 } __c; 00355 int __is_allocated; 00356 00357 private: 00358 void _destroy() 00359 { 00360 switch(__is_allocated) 00361 { 00362 case VDBL_MIXTYPE_EMPTY: 00363 case VDBL_MIXTYPE_ALLOCED_NONE: 00364 break; 00365 case VDBL_MIXTYPE_ALLOCED_S: 00366 delete(__c.s); 00367 break; 00368 case VDBL_MIXTYPE_ALLOCED_B: 00369 delete(__c.b); 00370 break; 00371 case VDBL_MIXTYPE_ALLOCED_N: 00372 delete(__c.n); 00373 break; 00374 case VDBL_MIXTYPE_ALLOCED_D: 00375 delete(__c.d); 00376 break; 00377 case VDBL_MIXTYPE_ALLOCED_U: 00378 delete(__c.u); 00379 break; 00380 } 00381 } 00382 00383 void _copy(const _VDBL_mixtype& __a) 00384 { 00385 __is_allocated = __a.__is_allocated; 00386 switch(__is_allocated) 00387 { 00388 case VDBL_MIXTYPE_EMPTY: 00389 break; 00390 case VDBL_MIXTYPE_ALLOCED_NONE: 00391 __c = __a.__c; 00392 break; 00393 case VDBL_MIXTYPE_ALLOCED_S: 00394 __c.s = new std::string(*__a.__c.s); 00395 break; 00396 case VDBL_MIXTYPE_ALLOCED_B: 00397 __c.b = new std::vector<bool>(*__a.__c.b); 00398 break; 00399 case VDBL_MIXTYPE_ALLOCED_N: 00400 __c.n = new std::vector<int>(*__a.__c.n); 00401 break; 00402 case VDBL_MIXTYPE_ALLOCED_D: 00403 __c.d = new std::vector<double>(*__a.__c.d); 00404 break; 00405 case VDBL_MIXTYPE_ALLOCED_U: 00406 __c.u = new std::vector<unsigned int>(*__a.__c.u); 00407 break; 00408 } 00409 } 00410 00411 public: 00413 00418 _VDBL_mixtype() : __is_allocated(VDBL_MIXTYPE_EMPTY) {} 00419 _VDBL_mixtype(bool __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00420 { __c.nb = __x; } 00421 _VDBL_mixtype(int __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00422 { __c.nn = __x; } 00423 _VDBL_mixtype(double __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00424 { __c.nd = __x;} 00425 _VDBL_mixtype(unsigned int __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00426 { __c.nu = __x; } 00427 _VDBL_mixtype(_VDBL_date __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00428 { __c.dt = __x; } 00429 _VDBL_mixtype(_VDBL_dateinterval __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00430 { __c.di = __x; } 00431 _VDBL_mixtype(const char* __cp) : __is_allocated(VDBL_MIXTYPE_ALLOCED_S) 00432 { __c.s = new std::string(__cp); } 00433 _VDBL_mixtype(const std::string& __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_S) 00434 { __c.s = new std::string(__x); } 00435 _VDBL_mixtype(const std::vector<bool>& __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_B) 00436 { __c.b = new std::vector<bool>(__x); } 00437 _VDBL_mixtype(const std::vector<int>& __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_N) 00438 { __c.n = new std::vector<int>(__x); } 00439 _VDBL_mixtype(const std::vector<double>& __x) : 00440 __is_allocated(VDBL_MIXTYPE_ALLOCED_D) 00441 { __c.d = new std::vector<double>(__x); } 00442 _VDBL_mixtype(const std::vector<unsigned int>& __x) : 00443 __is_allocated(VDBL_MIXTYPE_ALLOCED_U) 00444 { __c.u = new std::vector<unsigned int>(__x); } 00446 00448 virtual ~_VDBL_mixtype() 00449 { _destroy(); } 00450 00452 _VDBL_mixtype(const _VDBL_mixtype& __a) 00453 { _copy(__a); } 00454 00456 00460 _VDBL_mixtype& operator=(bool __x) 00461 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.nb = __x; 00462 return *this; 00463 } 00464 00465 _VDBL_mixtype& operator=(int __x) 00466 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.nn = __x; 00467 return *this; 00468 } 00469 00470 _VDBL_mixtype& operator=(double __x) 00471 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.nd = __x; 00472 return *this; 00473 } 00474 00475 _VDBL_mixtype& operator=(unsigned int __x) 00476 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.nu = __x; 00477 return *this; 00478 } 00479 00480 _VDBL_mixtype& operator=(const _VDBL_date& __x) 00481 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.dt = __x; 00482 return *this; 00483 } 00484 00485 _VDBL_mixtype& operator=(const _VDBL_dateinterval& __x) 00486 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.di = __x; 00487 return *this; 00488 } 00489 00490 _VDBL_mixtype& operator=(const std::string& __x) 00491 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_S; 00492 __c.s = new std::string(__x); 00493 return *this; 00494 } 00495 00496 _VDBL_mixtype& operator=(const char* __x) 00497 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_S; 00498 __c.s = new std::string(__x); 00499 return *this; 00500 } 00501 00502 _VDBL_mixtype& operator=(const std::vector<bool>& __x) 00503 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_B; 00504 __c.b = new std::vector<bool>(__x); 00505 return *this; 00506 } 00507 00508 _VDBL_mixtype& operator=(const std::vector<int>& __x) 00509 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_N; 00510 __c.n = new std::vector<int>(__x); 00511 return *this; 00512 } 00513 00514 _VDBL_mixtype& operator=(const std::vector<double>& __x) 00515 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_D; 00516 __c.d = new std::vector<double>(__x); 00517 return *this; 00518 } 00519 00520 _VDBL_mixtype& operator=(const std::vector<unsigned int>& __x) 00521 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_U; 00522 __c.u = new std::vector<unsigned int>(__x); 00523 return *this; 00524 } 00526 00528 _VDBL_mixtype& operator=(const _VDBL_mixtype& __a) 00529 { 00530 _destroy(); 00531 _copy(__a); 00532 return *this; 00533 } 00534 00536 _VDBL_mixtype& clear() 00537 { 00538 _destroy(); 00539 __is_allocated = VDBL_MIXTYPE_EMPTY; 00540 return *this; 00541 } 00542 00543 public: 00545 00551 bool nb() const { return __c.nb; } 00552 int nn() const { return __c.nn; } 00553 double nd() const { return __c.nd; } 00554 unsigned int nu() const { return __c.nu; } 00555 _VDBL_date dt() const { return __c.dt; } 00556 _VDBL_dateinterval di() const { return __c.di; } 00557 std::string& s() const { return *__c.s; } 00558 std::vector<bool>& b() const { return *__c.b; } 00559 std::vector<int>& n() const { return *__c.n; } 00560 std::vector<double>& d() const { return *__c.d; } 00561 std::vector<unsigned int>& u() const { return *__c.u; } 00563 00565 bool is_vector() const { return __is_allocated != VDBL_MIXTYPE_ALLOCED_NONE && 00566 __is_allocated != VDBL_MIXTYPE_EMPTY; } 00567 00569 bool empty() const { return __is_allocated == VDBL_MIXTYPE_EMPTY; } 00570 }; 00571 00573 00577 typedef _VDBL_date date; 00579 00583 typedef _VDBL_dateinterval dateinterval; 00585 00589 typedef _VDBL_mixtype mixtype; 00590 00592 00596 typedef _VDBL_alltype_base alltype_base; 00597 00599 00610 template <class _T> 00611 class alltype : public _VDBL_alltype<_T> 00612 { 00613 private: 00614 typedef _VDBL_alltype<_T> _Base; 00615 00616 public: 00618 typedef typename _Base::cont_type cont_type; 00619 }; 00620 00621 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) 00622 #pragma reset woff 1209 00623 #endif 00624 00625 __VDBL_END_NAMESPACE 00626 00627 #endif /* __VDBL_ALLTYPE_H */ 00628 00629 // Local Variables: 00630 // mode:C++ 00631 // End: