-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAny.h
More file actions
49 lines (37 loc) · 834 Bytes
/
Copy pathAny.h
File metadata and controls
49 lines (37 loc) · 834 Bytes
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
#ifndef __ANY_H__
#define __ANY_H__
#include <memory>
#include "TypeHolder.h"
class Any
{
public:
Any() {}
template <typename T>
Any(const T& value) : _holder(new Holder<T>(value)) {}
Any(const Any& other) : _holder(other._holder->Clone()) {}
Any& operator=(const Any& other)
{
_holder.reset(other._holder->Clone());
return *this;
}
const std::type_info& Type() const
{
if (_holder)
{
return _holder->Type();
}
return typeid(void);
}
template <typename T>
T& Get()
{
if (typeid(T) != _holder->Type())
{
throw std::bad_cast();
}
return static_cast<Holder<T>*>(_holder.get())->GetValue();
}
private:
std::unique_ptr<HolderBase> _holder;
};
#endif // __ANY_H__