bbvariant.h 668 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #ifndef BB_VARIANT_H
  2. #define BB_VARIANT_H
  3. class bbVariant{
  4. struct RepBase{
  5. virtual ~RepBase(){
  6. }
  7. };
  8. template<class T> struct Rep : public RepBase{
  9. T t;
  10. };
  11. RepBase *_rep;
  12. void retain()const{
  13. ++_rep->refs;
  14. }
  15. void release(){
  16. if( !--_rep->refs ) {}//delete rep;
  17. }
  18. public:
  19. template<class T> bbVariant( const T &t ):_rep( new Rep<T>( t ) ){
  20. retain();
  21. }
  22. bbVariant( const bbVariant &t ):_rep( t._rep ){
  23. retain();
  24. }
  25. bbVariant &operator=( const bbVariant &t ){
  26. t.retain();
  27. release();
  28. _rep=t._rep;
  29. return *this;
  30. }
  31. template<class T> T get()const{
  32. Rep<T> *p=dynamic_cast<Rep<T>*>( _rep );
  33. return p->t;
  34. }
  35. };
  36. #endif