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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
|
//////////////////////////////////////////////////////////////////////////
//
// fun_impl
//
//////////////////////////////////////////////////////////////////////////
struct base_fun_impl
{
typedef NullType parm1;
typedef NullType parm2;
typedef NullType parm3;
virtual ~base_fun_impl() = 0;
template< typename R >
R call()
{
return ( *reinterpret_cast< fun_impl< R, Sequence<> >* >( this ) )();
}
template< typename R, typename P1 >
R call( P1 p1 )
{
return ( *reinterpret_cast< fun_impl< R, Sequence< P1 > >* >( this ) )( p1 );
}
template< typename R, typename P1, typename P2 >
R call( P1 p1, P2 p2 )
{
return ( *reinterpret_cast< fun_impl< R, Sequence< P1, P2 > >* >( this ) )( p1, p2 );
}
template< typename R, typename P1, typename P2, typename P3 >
R call( P1 p1, P2 p2, P3 p3 )
{
return ( *reinterpret_cast< fun_impl< R, Sequence< P1, P2, P3 > >* >( this ) )( p1, p2, p3 );
}
};
base_fun_impl::~base_fun_impl()
{}
template< typename R, typename Parms >
struct fun_impl;
template< typename R >
struct fun_impl< R, Sequence<> > : public base_fun_impl
{
typedef R result_type;
virtual R operator()() const = 0;
};
template< typename R, typename P1 >
struct fun_impl< R, Sequence< P1 > > : public base_fun_impl
{
typedef R result_type;
typedef P1 parm1;
virtual R operator()( P1 p1 ) const = 0;
};
template< typename R, typename P1, typename P2 >
struct fun_impl< R, Sequence< P1, P2 > > : public base_fun_impl
{
typedef R result_type;
typedef P1 parm1;
typedef P2 parm2;
virtual R operator()( P1 p1, P2 p2 ) const = 0;
};
template< typename R, typename P1, typename P2, typename P3 >
struct fun_impl< R, Sequence< P1, P2, P3 > > : public base_fun_impl
{
typedef R result_type;
typedef P1 parm1;
typedef P2 parm2;
typedef P3 parm3;
virtual R operator()( P1 p1, P2 p2, P3 p3 ) const = 0;
};
//////////////////////////////////////////////////////////////////////////
//
// function_holder
//
//////////////////////////////////////////////////////////////////////////
template< typename R, typename Parms, typename FunPtr >
struct fun_holder
: public fun_impl< R, Parms >
{
typedef typename fun_impl< R, Parms > impl;
typedef typename impl::result_type result_type;
typedef typename impl::parm1 parm1;
typedef typename impl::parm2 parm2;
typedef typename impl::parm3 parm3;
fun_holder( const FunPtr& ptr )
: fun( ptr )
{}
result_type operator()() const
{
return fun();
}
result_type operator()( parm1 p1 ) const
{
return fun( p1 );
}
result_type operator()( parm1 p1, parm2 p2 ) const
{
return fun( p1, p2 );
}
result_type operator()( parm1 p1, parm2 p2, parm3 p3 ) const
{
return fun( p1, p2, p3 );
}
private:
FunPtr fun;
};
namespace function
{
template< typename R, typename Parms, typename FunPtr >
base_fun_impl* create( const FunPtr& ptr )
{
return new fun_holder< R, Parms, FunPtr >( ptr );
}
}
//////////////////////////////////////////////////////////////////////////
//
// command_list
//
//////////////////////////////////////////////////////////////////////////
class command_list
{
typedef boost::shared_ptr< base_fun_impl > ptr_type;
typedef std::map< std::string, ptr_type > list_type;
public:
template< typename R, typename Parms, typename FunPtr >
bool register_( const std::string& name, FunPtr fun )
{
return list.insert(
list_type::value_type( name, ptr_type( new fun_holder< R, Parms, FunPtr >( fun ) ) ) ).second;
}
bool unregister_( const std::string& name )
{
return list.erase( name ) != 0;
}
template< typename R >
R call( const std::string& name )
{
return find( name )->call< R >();
}
template< typename R, typename P1 >
R call( const std::string& name, P1 p1 )
{
return find( name )->call< R >( p1 );
}
template< typename R, typename P1, typename P2 >
R call( const std::string& name, P1 p1, P2 p2 )
{
return find( name )->call< R >( p1, p2 );
}
template< typename R, typename P1, typename P2, typename P3 >
R call( const std::string& name, P1 p1, P2 p2, P3 p3 )
{
return find( name )->call< R >( p1, p2, p3 );
}
private:
ptr_type& find( const std::string& name )
{
list_type::iterator it = list.find( name );
if ( it == list.end() )
{
throw std::logic_error( "unregistered type" );
}
return it->second;
}
private:
list_type list;
};
struct foo
{
static void bar( int i )
{
std::cout << "passed: " << i << std::endl;
}
static int add( int a, int b )
{
return a + b;
}
};
int main()
{
command_list list;
list.register_< void, Sequence< int > >( "bar", &foo::bar );
list.register_< int, Sequence< int, int > >( "add", &foo::add );
try
{
list.call< void >( "bar", 10 );
std::cout << list.call< int >( "add", 10, 20 );
}
catch ( std::logic_error& e )
{
std::cout << "Error: " << e.what() << std::endl;
}
std::cin.get();
}
|