-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathadvanced.cpp
More file actions
70 lines (57 loc) · 1.63 KB
/
advanced.cpp
File metadata and controls
70 lines (57 loc) · 1.63 KB
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
#include <cstddef>
#include <iostream>
#include <boost/variant.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/contains.hpp>
#include <boost/utility/enable_if.hpp>
// Generic visitor that does magical dispatching of
// types and delegates passes down to your visitor only
// those types specified in a type list.
using namespace boost;
using namespace boost::mpl;
template <typename Visitor, typename TypeList>
struct picky_visitor: static_visitor<void>, Visitor
{
template <typename T>
void operator() (T v,
typename enable_if<typename contains<TypeList, T>::type>::type* dummy=NULL) const
{
Visitor::operator() (v);
}
template <typename T>
void operator() (T v,
typename disable_if<typename contains<TypeList, T>::type>::type* dummy=NULL) const
{}
};
struct nil {};
typedef variant<nil, char, int, double> sql_field;
struct example_visitor
{
typedef picky_visitor<example_visitor,
mpl::vector<char, int/*, double, Usage*/> > value_type;
void operator() (char v) const
{
std::cout << "character detected" << std::endl;
}
void operator() (int v) const
{
std::cout << "integer detected" << std::endl;
}
void operator() (double v) const
{
std::cout << "double detected" << std::endl;
}
};
int main(int argc, char* argv[])
{
example_visitor::value_type visitor;
sql_field nilField;
sql_field charField ('X');
sql_field intField (1986);
sql_field doubleField (19.86);
boost::apply_visitor (visitor, nilField);
boost::apply_visitor (visitor, charField);
boost::apply_visitor (visitor, intField);
boost::apply_visitor (visitor, doubleField);
return 0;
}