12 #include <KrisLibrary/errors.h> 16 template<
typename ValueType>
18 template<
typename ValueType>
32 template<
typename ValueType>
34 : content(
new holder<ValueType>(value))
37 : content(other.content ? other.content->clone() : 0)
40 : content(other.content)
48 std::swap(content, rhs.content);
51 template<
typename ValueType>
52 AnyValue & operator=(
const ValueType & rhs) {
60 bool empty()
const {
return !content; }
61 const std::type_info & type()
const {
62 return content ? content->type() :
typeid(void);
65 bool hastype()
const {
66 return &type() == &
typeid(T);
68 template <
class ValueType>
69 bool operator == (
const ValueType &rhs)
const {
70 if(&type() != &
typeid(ValueType))
return false;
71 return *AnyCast<ValueType>(
this) == rhs;
74 template <
class ValueType>
75 bool operator != (
const ValueType &rhs)
const {
76 return !operator == (rhs);
82 virtual ~placeholder() { }
83 virtual const std::type_info & type()
const = 0;
84 virtual placeholder * clone()
const = 0;
87 template<
typename ValueType>
88 struct holder :
public placeholder
90 holder(
const ValueType & value) : held(value) {}
91 virtual const std::type_info & type()
const {
return typeid(ValueType); }
92 virtual placeholder * clone()
const {
return new holder(held); }
101 placeholder * content;
105 template<
typename ValueType>
108 return operand && operand->type() ==
typeid(ValueType)
109 ? &
static_cast<AnyValue::holder<ValueType> *
>(operand->content)->held
114 template<
typename ValueType>
117 return AnyCast<ValueType>(
const_cast<AnyValue *
>(operand));
125 template<
typename ValueType>
129 ? &
static_cast<AnyValue::holder<ValueType> *
>(operand->content)->held
138 template<
typename ValueType>
141 return AnyCast_Raw<ValueType>(
const_cast<AnyValue *
>(operand));
149 template<
typename ValueType>
152 return operand && (&operand->type() == &
typeid(ValueType))
153 ? &
static_cast<AnyValue::holder<ValueType> *
>(operand->content)->held
161 template<
typename ValueType>
164 return AnyCast_Quick<ValueType>(
const_cast<AnyValue *
>(operand));
173 bool CoerceCast(
const AnyValue& value,T& result)
175 const T* val = AnyCast<T>(&value);
183 template <>
bool CoerceCast<bool>(
const AnyValue& value,
bool& result);
184 template <>
bool CoerceCast<char>(
const AnyValue& value,
char& result);
185 template <>
bool CoerceCast<unsigned char>(
const AnyValue& value,
unsigned char& result);
186 template <>
bool CoerceCast<int>(
const AnyValue& value,
int& result);
187 template <>
bool CoerceCast<unsigned int>(
const AnyValue& value,
unsigned int& result);
188 template <>
bool CoerceCast<float>(
const AnyValue& value,
float& result);
189 template <>
bool CoerceCast<double>(
const AnyValue& value,
double& result);
190 template <
class T>
bool LexicalCast(
const T& value,std::string& result);
191 template <>
bool LexicalCast(
const AnyValue& value,std::string& result);
192 template <
class T> std::string LexicalCast(
const T& value);
193 template <
class T>
bool LexicalCast(
const std::string& value,T& result);
194 template <>
bool LexicalCast(
const std::string& value,
AnyValue& result);
197 template <
class T>
bool LexicalCast(
const T& value,std::string& result)
199 std::stringstream ss;
208 template <
class T> std::string LexicalCast(
const T& value)
211 if(!LexicalCast(value,result)) {
return ""; }
215 template <
class T>
bool LexicalCast(
const std::string& value,T& result)
217 std::stringstream ss(value);
A polymorphic container class that can contain data of any type.
Definition: AnyValue.h:25
friend ValueType * AnyCast(AnyValue *)
Retreive the data within the operand, or NULL if not of the correct type.
Definition: AnyValue.h:106
friend ValueType * AnyCast_Raw(AnyValue *)
Definition: AnyValue.h:126
friend ValueType * AnyCast_Quick(AnyValue *)
Definition: AnyValue.h:150