3 This file is part of libmspcore
4 Copyright © 2008 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #ifndef MSP_CORE_VARIANT_H_
9 #define MSP_CORE_VARIANT_H_
21 virtual ~StoreBase() { }
22 virtual StoreBase *clone() const =0;
26 struct Store: public StoreBase
30 Store(T d): data(d) { }
31 virtual StoreBase *clone() const { return new Store<T>(data); }
37 Variant(): store(0) { }
39 Variant(const T &v): store(new Store<typename RemoveConst<T>::Type>(v)) { }
40 Variant(const Variant &v): store(v.store ? v.store->clone() : 0) { }
41 ~Variant() { delete store; }
44 Variant &operator=(const T &v)
47 store=new Store<typename RemoveConst<T>::Type>(v);
51 Variant &operator=(const Variant &v)
54 store=(v.store ? v.store->clone() : 0);
61 typedef typename RemoveConst<T>::Type NCT;
62 Store<NCT> *s=dynamic_cast<Store<NCT> *>(store);
64 throw InvalidState("Type mismatch");
69 bool check_type() const
71 return dynamic_cast<Store<typename RemoveConst<T>::Type> *>(store)!=0;
76 { return value<T>(); }