-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertors.h
47 lines (39 loc) · 908 Bytes
/
Convertors.h
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
#ifndef CONVERTORS_H
#define CONVERTORS_H
#include "Exceptions.h"
#include <boost/any.hpp>
class Convertor {
public:
Convertor(boost::any& a) : data_(a) {
}
template <typename T>
operator T&() {
T* v = boost::any_cast<T>(&data_);
if (v) {
return *v;
} else {
throw PTreeBadType("cast error");
}
}
private:
boost::any& data_;
};
class ConstConvertor {
public:
ConstConvertor(const boost::any& a) : data_(a) {
}
template <typename T>
operator T&() const {
const T* cv = boost::any_cast<T>(&data_);
// http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#976
T* v = const_cast<T*>(cv);
if (v) {
return *v;
} else {
throw PTreeBadType("const cast error");
}
}
private:
const boost::any& data_;
};
#endif // CONVERTORS_H