/* $Id$
This file is part of libmspcore
-Copyright © 2006-2007 Mikko Rasa, Mikkosoft Productions
+Copyright © 2008 Mikko Rasa, Mikkosoft Productions
Distributed under the LGPL
*/
#define MSP_CORE_VARIANT_H_
#include "except.h"
+#include "meta.h"
namespace Msp {
struct StoreBase
{
virtual ~StoreBase() { }
+ virtual StoreBase *clone() const =0;
};
template<typename T>
T data;
Store(T d): data(d) { }
+ virtual StoreBase *clone() const { return new Store<T>(data); }
};
StoreBase *store;
public:
Variant(): store(0) { }
template<typename T>
- Variant(T v): store(new Store<T>(v)) { }
+ Variant(const T &v): store(new Store<typename RemoveConst<T>::Type>(v)) { }
+ Variant(const Variant &v): store(v.store ? v.store->clone() : 0) { }
~Variant() { delete store; }
template<typename T>
- Variant &operator=(T v)
+ Variant &operator=(const T &v)
{
delete store;
- store=new Store<T>(v);
+ store=new Store<typename RemoveConst<T>::Type>(v);
+ return *this;
+ }
+
+ Variant &operator=(const Variant &v)
+ {
+ delete store;
+ store=(v.store ? v.store->clone() : 0);
return *this;
}
template<typename T>
- T value() const
+ T &value() const
{
- Store<T> *s=dynamic_cast<Store<T> *>(store);
+ typedef typename RemoveConst<T>::Type NCT;
+ Store<NCT> *s=dynamic_cast<Store<NCT> *>(store);
if(!s)
throw InvalidState("Type mismatch");
return s->data;
template<typename T>
bool check_type() const
{
- return dynamic_cast<Store<T> *>(store)!=0;
+ return dynamic_cast<Store<typename RemoveConst<T>::Type> *>(store)!=0;
}
template<typename T>