00001 // All-Type implementation -*- C++ -*- 00002 00003 // $Id: vdbl_alltype.h 631 2008-05-19 17:38:05Z mihaly $ 00004 // Copyright (C) 2003 Hermann Schichl 00005 // 00006 // This file is part of the Vienna Database Library. This library is free 00007 // software; you can redistribute it and/or modify it under the 00008 // terms of the Library GNU General Public License as published by the 00009 // Free Software Foundation; either version 2, or (at your option) 00010 // any later version. 00011 00012 // This library is distributed in the hope that it will be useful, 00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 // Library GNU General Public License for more details. 00016 00017 // As a special exception, you may use this file as part of a free software 00018 // library without restriction. Specifically, if other files instantiate 00019 // templates or use macros or inline functions from this file, or you compile 00020 // this file and link it with other files to produce an executable, this 00021 // file does not by itself cause the resulting executable to be covered by 00022 // the Library GNU General Public License. This exception does not however 00023 // invalidate any other reasons why the executable file might be covered by 00024 // the Library GNU General Public License. 00025 00031 #ifndef __VDBL_ALLTYPE_H 00032 #define __VDBL_ALLTYPE_H 00033 00034 #include <vdbl_config.h> 00035 #include <vdbl_types.h> 00036 #include <string> 00037 #include <vector> 00038 #include <typeinfo> 00039 00040 __VDBL_BEGIN_NAMESPACE 00041 00042 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) 00043 #pragma set woff 1209 00044 #endif 00045 00046 00048 00055 class _VDBL_alltype_base 00056 { 00057 public: 00061 _VDBL_alltype_base() {} 00065 virtual ~_VDBL_alltype_base() {} 00066 00070 virtual const std::type_info& get_type() const VDBL_PURE_VIRTUAL 00071 00073 00077 virtual bool operator==(const _VDBL_alltype_base &p) const VDBL_PURE_VIRTUAL 00078 virtual bool operator!=(const _VDBL_alltype_base &p) const VDBL_PURE_VIRTUAL 00080 }; 00081 00083 00090 template <class _TR> 00091 class _VDBL_alltype : public _VDBL_alltype_base 00092 { 00093 public: 00095 typedef _TR cont_type; 00096 00097 private: 00099 typedef _VDBL_alltype<_TR> _Self; 00100 00101 private: 00105 cont_type* _RC_p; 00106 00107 public: 00111 _VDBL_alltype() : _RC_p(NULL) {} 00112 00117 _VDBL_alltype(const cont_type& _p) : _RC_p(new cont_type(_p)) {} 00118 00124 _VDBL_alltype(cont_type* _p) : _RC_p(_p) {} 00125 00129 virtual ~_VDBL_alltype() { delete _RC_p; } 00130 00135 const std::type_info& get_type() const { return typeid(_TR); } 00136 00140 const cont_type& content() const { return *_RC_p; } 00141 00143 00149 void operator=(const void *p) 00150 { delete _RC_p; _RC_p = new cont_type((cont_type*) p); } 00151 00152 void operator=(const cont_type *p) 00153 { delete _RC_p; _RC_p = new cont_type((cont_type*) p); } 00155 00157 00161 bool operator==(const _VDBL_alltype_base &p) const 00162 { if(typeid(cont_type)==typeid(*&p)) return *this == *(_Self*)&p; 00163 else return false; } 00164 bool operator!=(const _VDBL_alltype_base &p) const 00165 { if(typeid(cont_type)==typeid(*&p)) return *this != *(_Self*)&p; 00166 else return false; } 00168 00170 00174 bool operator==(const _Self &p) const { return *_RC_p == *p._RC_p; } 00175 bool operator!=(const _Self &p) const { return *_RC_p != *p._RC_p; } 00177 }; 00178 00179 class _VDBL_dateinterval; 00180 00182 00186 class _VDBL_date 00187 { 00188 public: 00192 int timezone; 00194 00198 int year; 00199 signed char month, day; 00200 unsigned int seconds; 00201 unsigned int microseconds; 00203 00207 _VDBL_dateinterval operator-(const _VDBL_date& _v) const; 00208 00210 00213 _VDBL_date& operator-(const _VDBL_dateinterval& _v) const; 00214 _VDBL_date& operator-=(const _VDBL_dateinterval& _v); 00215 _VDBL_date& operator+(const _VDBL_dateinterval& _v) const; 00216 _VDBL_date& operator+=(const _VDBL_dateinterval& _v); 00218 00219 #if 0 00222 _VDBL_date& operator=(const _VDBL_date& _v) 00223 { 00224 timezone = _v.timezone; 00225 year = _v.year; 00226 month = _v.month; 00227 day = _v.day; 00228 seconds = _v.seconds; 00229 microseconds = _v.microseconds; 00230 return *this; 00231 } 00232 #endif 00233 }; 00234 00235 #if 0 00236 _VDBL_date& operator=(_VDBL_date& _a, const _VDBL_date& _v) 00237 { 00238 _a.timezone = _v.timezone; 00239 _a.year = _v.year; 00240 _a.month = _v.month; 00241 _a.day = _v.day; 00242 _a.seconds = _v.seconds; 00243 _a.microseconds = _v.microseconds; 00244 return _a; 00245 } 00246 #endif 00247 00249 00254 class _VDBL_dateinterval 00255 { 00256 public: 00258 00262 int years; 00263 short int days; 00264 int seconds; 00265 int microseconds; 00267 00269 00273 _VDBL_dateinterval operator*(double d) const; 00274 _VDBL_dateinterval operator/(double d) const; 00275 _VDBL_dateinterval& operator*=(double d); 00276 _VDBL_dateinterval& operator/=(double d); 00278 00280 00283 _VDBL_dateinterval& operator-(const _VDBL_dateinterval& _v) const; 00284 _VDBL_dateinterval& operator-=(const _VDBL_dateinterval& _v); 00285 _VDBL_dateinterval& operator+(const _VDBL_dateinterval& _v) const; 00286 _VDBL_dateinterval& operator+=(const _VDBL_dateinterval& _v); 00288 00292 _VDBL_date operator+(const _VDBL_date& _d) const; 00293 }; 00294 00295 #if 0 00296 _VDBL_dateinterval& operator=(_VDBL_dateinterval& _a, 00297 const _VDBL_dateinterval& _v) 00298 { 00299 _a.years = _v.years; 00300 _a.days = _v.days; 00301 _a.seconds = _v.seconds; 00302 _a.microseconds = _v.microseconds; 00303 return _a; 00304 } 00305 #endif 00306 00308 00327 class _VDBL_mixtype 00328 { 00329 public: 00331 00335 #define VDBL_MIXTYPE_EMPTY -1 00336 #define VDBL_MIXTYPE_ALLOCED_NONE 0 00337 #define VDBL_MIXTYPE_ALLOCED_B 1 00338 #define VDBL_MIXTYPE_ALLOCED_N 2 00339 #define VDBL_MIXTYPE_ALLOCED_D 3 00340 #define VDBL_MIXTYPE_ALLOCED_U 4 00341 #define VDBL_MIXTYPE_ALLOCED_S 5 00342 00343 00344 private: 00348 union 00349 { 00350 bool nb; 00351 int nn; 00352 unsigned int nu; 00353 double nd; 00354 _VDBL_date dt; 00355 _VDBL_dateinterval di; 00356 std::string* s; 00357 std::vector<bool>* b; 00358 std::vector<int>* n; 00359 std::vector<unsigned int>* u; 00360 std::vector<double>* d; 00361 } __c; 00366 int __is_allocated; 00367 00368 private: 00369 void _destroy() 00370 { 00371 switch(__is_allocated) 00372 { 00373 case VDBL_MIXTYPE_EMPTY: 00374 case VDBL_MIXTYPE_ALLOCED_NONE: 00375 break; 00376 case VDBL_MIXTYPE_ALLOCED_S: 00377 delete(__c.s); 00378 break; 00379 case VDBL_MIXTYPE_ALLOCED_B: 00380 delete(__c.b); 00381 break; 00382 case VDBL_MIXTYPE_ALLOCED_N: 00383 delete(__c.n); 00384 break; 00385 case VDBL_MIXTYPE_ALLOCED_D: 00386 delete(__c.d); 00387 break; 00388 case VDBL_MIXTYPE_ALLOCED_U: 00389 delete(__c.u); 00390 break; 00391 } 00392 } 00393 00394 void _copy(const _VDBL_mixtype& __a) 00395 { 00396 __is_allocated = __a.__is_allocated; 00397 switch(__is_allocated) 00398 { 00399 case VDBL_MIXTYPE_EMPTY: 00400 break; 00401 case VDBL_MIXTYPE_ALLOCED_NONE: 00402 __c = __a.__c; 00403 break; 00404 case VDBL_MIXTYPE_ALLOCED_S: 00405 __c.s = new std::string(*__a.__c.s); 00406 break; 00407 case VDBL_MIXTYPE_ALLOCED_B: 00408 __c.b = new std::vector<bool>(*__a.__c.b); 00409 break; 00410 case VDBL_MIXTYPE_ALLOCED_N: 00411 __c.n = new std::vector<int>(*__a.__c.n); 00412 break; 00413 case VDBL_MIXTYPE_ALLOCED_D: 00414 __c.d = new std::vector<double>(*__a.__c.d); 00415 break; 00416 case VDBL_MIXTYPE_ALLOCED_U: 00417 __c.u = new std::vector<unsigned int>(*__a.__c.u); 00418 break; 00419 } 00420 } 00421 00422 public: 00424 00429 _VDBL_mixtype() : __is_allocated(VDBL_MIXTYPE_EMPTY) {} 00430 _VDBL_mixtype(bool __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00431 { __c.nb = __x; } 00432 _VDBL_mixtype(int __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00433 { __c.nn = __x; } 00434 _VDBL_mixtype(double __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00435 { __c.nd = __x;} 00436 _VDBL_mixtype(unsigned int __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00437 { __c.nu = __x; } 00438 _VDBL_mixtype(_VDBL_date __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00439 { __c.dt = __x; } 00440 _VDBL_mixtype(_VDBL_dateinterval __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_NONE) 00441 { __c.di = __x; } 00442 _VDBL_mixtype(const char* __cp) : __is_allocated(VDBL_MIXTYPE_ALLOCED_S) 00443 { __c.s = new std::string(__cp); } 00444 _VDBL_mixtype(const std::string& __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_S) 00445 { __c.s = new std::string(__x); } 00446 _VDBL_mixtype(const std::vector<bool>& __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_B) 00447 { __c.b = new std::vector<bool>(__x); } 00448 _VDBL_mixtype(const std::vector<int>& __x) : __is_allocated(VDBL_MIXTYPE_ALLOCED_N) 00449 { __c.n = new std::vector<int>(__x); } 00450 _VDBL_mixtype(const std::vector<double>& __x) : 00451 __is_allocated(VDBL_MIXTYPE_ALLOCED_D) 00452 { __c.d = new std::vector<double>(__x); } 00453 _VDBL_mixtype(const std::vector<unsigned int>& __x) : 00454 __is_allocated(VDBL_MIXTYPE_ALLOCED_U) 00455 { __c.u = new std::vector<unsigned int>(__x); } 00457 00459 virtual ~_VDBL_mixtype() 00460 { _destroy(); } 00461 00463 _VDBL_mixtype(const _VDBL_mixtype& __a) 00464 { _copy(__a); } 00465 00467 00471 _VDBL_mixtype& operator=(bool __x) 00472 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.nb = __x; 00473 return *this; 00474 } 00475 00476 _VDBL_mixtype& operator=(int __x) 00477 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.nn = __x; 00478 return *this; 00479 } 00480 00481 _VDBL_mixtype& operator=(double __x) 00482 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.nd = __x; 00483 return *this; 00484 } 00485 00486 _VDBL_mixtype& operator=(unsigned int __x) 00487 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.nu = __x; 00488 return *this; 00489 } 00490 00491 _VDBL_mixtype& operator=(const _VDBL_date& __x) 00492 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.dt = __x; 00493 return *this; 00494 } 00495 00496 _VDBL_mixtype& operator=(const _VDBL_dateinterval& __x) 00497 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_NONE; __c.di = __x; 00498 return *this; 00499 } 00500 00501 _VDBL_mixtype& operator=(const std::string& __x) 00502 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_S; 00503 __c.s = new std::string(__x); 00504 return *this; 00505 } 00506 00507 _VDBL_mixtype& operator=(const char* __x) 00508 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_S; 00509 __c.s = new std::string(__x); 00510 return *this; 00511 } 00512 00513 _VDBL_mixtype& operator=(const std::vector<bool>& __x) 00514 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_B; 00515 __c.b = new std::vector<bool>(__x); 00516 return *this; 00517 } 00518 00519 _VDBL_mixtype& operator=(const std::vector<int>& __x) 00520 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_N; 00521 __c.n = new std::vector<int>(__x); 00522 return *this; 00523 } 00524 00525 _VDBL_mixtype& operator=(const std::vector<double>& __x) 00526 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_D; 00527 __c.d = new std::vector<double>(__x); 00528 return *this; 00529 } 00530 00531 _VDBL_mixtype& operator=(const std::vector<unsigned int>& __x) 00532 { _destroy(); __is_allocated = VDBL_MIXTYPE_ALLOCED_U; 00533 __c.u = new std::vector<unsigned int>(__x); 00534 return *this; 00535 } 00537 00539 _VDBL_mixtype& operator=(const _VDBL_mixtype& __a) 00540 { 00541 _destroy(); 00542 _copy(__a); 00543 return *this; 00544 } 00545 00547 _VDBL_mixtype& clear() 00548 { 00549 _destroy(); 00550 __is_allocated = VDBL_MIXTYPE_EMPTY; 00551 return *this; 00552 } 00553 00554 public: 00556 00562 bool nb() const { return __c.nb; } 00563 int nn() const { return __c.nn; } 00564 double nd() const { return __c.nd; } 00565 unsigned int nu() const { return __c.nu; } 00566 _VDBL_date dt() const { return __c.dt; } 00567 _VDBL_dateinterval di() const { return __c.di; } 00568 std::string& s() const { return *__c.s; } 00569 std::vector<bool>& b() const { return *__c.b; } 00570 std::vector<int>& n() const { return *__c.n; } 00571 std::vector<double>& d() const { return *__c.d; } 00572 std::vector<unsigned int>& u() const { return *__c.u; } 00574 00576 bool is_vector() const { return __is_allocated != VDBL_MIXTYPE_ALLOCED_NONE && 00577 __is_allocated != VDBL_MIXTYPE_EMPTY; } 00578 00580 bool empty() const { return __is_allocated == VDBL_MIXTYPE_EMPTY; } 00581 }; 00582 00584 00588 typedef _VDBL_date date; 00590 00594 typedef _VDBL_dateinterval dateinterval; 00596 00600 typedef _VDBL_mixtype mixtype; 00601 00603 00607 typedef _VDBL_alltype_base alltype_base; 00608 00610 00621 template <class _TT> 00622 class alltype : public _VDBL_alltype<_TT> 00623 { 00624 private: 00625 typedef _VDBL_alltype<_TT> _Base; 00626 00627 public: 00629 typedef typename _Base::cont_type cont_type; 00630 }; 00631 00632 #if defined(__sgi) && !defined(__GNUC__) && (_MIPS_SIM != _MIPS_SIM_ABI32) 00633 #pragma reset woff 1209 00634 #endif 00635 00636 __VDBL_END_NAMESPACE 00637 00638 #endif /* __VDBL_ALLTYPE_H */ 00639 00640 // Local Variables: 00641 // mode:C++ 00642 // End: