/* ========================================================================
** Extended Template and Library
** Abstraction for a Generic Value Type
-** $Id: _value.h,v 1.1.1.1 2005/01/04 01:31:48 darco Exp $
+** $Id$
**
** Copyright (c) 2002 Adrian Bentley
**
/* === S T A R T =========================================================== */
-#ifndef __ETL_VALUE_H
-#define __ETL_VALUE_H
+#ifndef __ETL__VALUE_H
+#define __ETL__VALUE_H
/* === H E A D E R S ======================================================= */
#include <algorithm>
virtual contentholder *clone() const = 0;
virtual const std::type_info &type() const = 0;
};
-
- contentholder *content;
-
-public: //structor interface
+
+ contentholder *content;
+
+public: //constructor interface
value()
- :content(0)
+ :content(0)
{
}
-
+
value(const value &v)
:content( v.content ? v.content->clone() : 0 )
{
}
-
+
/* Copies the object passed to it
*/
template < typename T >
(reinterpret_cast<const typename value_store_type<T>::value_type &>(v)) )
{
}
-
+
public: //modifier interface
-
+
value & swap(value & rhs)
{
std::swap(content, rhs.content);
value(rhs).swap(*this);
return *this;
}
-
+
public: //query interface
-
+
bool empty() const
{
return content == 0;
}
-
+
const std::type_info & type() const
{
return content ? content->type() : typeid(void);
}
-
+
private: //implementation interface
-
+
template < typename T >
class holder : public contentholder
{
public: //representation
T obj;
- public: //structor interface
-
+ public: //constructor interface
+
holder(const T &o)
:obj(o)
{
}
-
+
holder(const holder<T> &h)
:obj(h.obj)
{
{
return new holder(*this);
}
-
+
virtual const std::type_info &type() const
{
return typeid(T);
}
-
+
public: //allocation interface
void *operator new(unsigned int size)
{
assert(size == sizeof(holder<T>));
-
+
//use pool allocation at some point
return malloc(size);
}
-
+
void operator delete(void *p)
{
assert(p);
return free(p);
}
};
-
+
template < typename ValueType >
friend ValueType *value_cast(value *v);
};
ValueType *value_cast(value *v)
{
assert(v);
-
- return ( typeid(typename value_store_type<ValueType>::value_type) == v->type() )
+
+ return ( typeid(typename value_store_type<ValueType>::value_type) == v->type() )
? &static_cast<value::holder<ValueType> *>(v->content)->obj
: 0;
}
return value_cast<ValueType>(const_cast<value *>(v));
}
-/*! Extract a copy of the internal object and will throw a bad_value_cast exception
+/*! Extract a copy of the internal object and will throw a bad_value_cast exception
if the types do not agree.
\note I'm not sure why boost::any didn't use a reference here... there must be a reason...