-
Notifications
You must be signed in to change notification settings - Fork 0
/
Any.hh
103 lines (84 loc) · 1.95 KB
/
Any.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#ifndef _ANY_H_
#define _ANY_H_
# include <typeinfo>
# include <exception>
# include <cassert>
namespace navi {
class BadAnyCast : public std::bad_cast {
public:
virtual char const* what() const throw() { return "Bad any cast"; }
};
class Any {
class HolderBase {
public:
virtual ~HolderBase() {}
virtual HolderBase* clone() const = 0;
virtual std::type_info const& getType() const = 0;
};
template <typename ValueType>
class Holder : public HolderBase {
friend class Any;
public:
Holder(ValueType const& value)
: _value(value) {}
HolderBase* clone() const { return new Holder(_value); }
std::type_info const& getType() const { return typeid(ValueType); }
private:
ValueType _value;
};
public:
template <typename ValueType>
Any(ValueType const& value)
: _holder(new Holder<ValueType>(value))
{
}
Any(Any const& any)
: _holder(any._holder->clone())
{
}
~Any()
{
delete _holder;
}
inline Any& operator=(Any const& any)
{
delete _holder;
_holder = any._holder->clone();
return *this;
}
template <typename ValueType>
inline Any& operator=(ValueType const& value)
{
get<ValueType>() = value;
return *this;
}
template <typename ValueType>
inline ValueType const* getPtr() const
{
# ifndef NDEBUG
if (_holder->getType() != typeid(ValueType))
throw BadAnyCast();
# endif
return &static_cast<Holder<ValueType>*>(_holder)->_value;
}
template <typename ValueType>
inline ValueType* getPtr()
{
return const_cast<ValueType*>(static_cast<Any const&>(*this).getPtr<ValueType>());
}
template <typename ValueType>
inline ValueType const& get() const
{
ValueType const* value = getPtr<ValueType>();
return *value;
}
template <typename ValueType>
inline ValueType& get()
{
return const_cast<ValueType&>(static_cast<Any const&>(*this).get<ValueType>());
}
private:
HolderBase* _holder;
};
} // !navi
#endif /* !_ANY_H_ */