Console; Support non-function-pointer parsed commands via boost::function
[senf.git] / Console / ParsedCommand.cti
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief ParsedCommand inline template implementation */
25
26 #include "ParsedCommand.ih"
27
28 // Custom includes
29 #include "../Utils/membind.hh"
30 #include <boost/format.hpp>
31 #include "../Utils/parameter.hh"
32
33 #define prefix_ inline
34 ///////////////////////////////cti.p///////////////////////////////////////
35
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::console::detail::ArgumentInfoBase
38
39 prefix_ senf::console::detail::ArgumentInfoBase::ArgumentInfoBase(std::string const & type_)
40     : type (type_), name (), hasDefault (false)
41 {}
42
43 ///////////////////////////////////////////////////////////////////////////
44 // senf::console::detail::ArgumentInfo<ParameterType>
45
46 template <class ParameterType>
47 prefix_ typename senf::console::detail::ArgumentInfo<ParameterType>::ptr
48 senf::console::detail::ArgumentInfo<ParameterType>::create()
49 {
50     return ptr(new ArgumentInfo());
51 }
52
53 template <class ParameterType>
54 prefix_ senf::console::detail::ArgumentInfo<ParameterType>::ArgumentInfo()
55     : ArgumentInfoBase ( ArgumentTraits<ParameterType>::description() ),
56       defaultValue ()
57 {}
58
59 template <class ParameterType>
60 prefix_ std::string senf::console::detail::ArgumentInfo<ParameterType>::defaultValueStr()
61     const
62 {
63     return hasDefault ? ArgumentTraits<ParameterType>::str(defaultValue) : "";
64 }
65
66 ///////////////////////////////////////////////////////////////////////////
67 // senf::console::ParsedCommandOverloadBase
68
69 template <class Type>
70 prefix_ senf::console::detail::ArgumentInfo<Type> &
71 senf::console::ParsedCommandOverloadBase::arg(unsigned n)
72     const
73 {
74     return dynamic_cast<detail::ArgumentInfo<Type> &>(arg(n));
75 }
76
77 template <class Type>
78 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
79 {
80     parameters_.push_back(detail::ArgumentInfo<Type>::create());
81 }
82
83 ///////////////////////////////////////////////////////////////////////////
84 // senf::console::ParsedCommandOverload<FunctionTraits,n>
85
86 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                       \
87                                          SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp),   \
88                                          2))
89 #include BOOST_PP_ITERATE()
90
91 ///////////////////////////////////////////////////////////////////////////
92 // senf::console::ParsedCommandAttributorBase
93
94 template <class Type>
95 prefix_ void senf::console::ParsedCommandAttributorBase::defaultValue(Type const & value)
96     const
97 {
98     overload().arg<Type>(index_).defaultValue = value;
99     overload().arg(index_).hasDefault = true;
100 }
101
102 template <class Type, class Fn>
103 prefix_ void senf::console::ParsedCommandAttributorBase::parser(Fn fn)
104     const
105 {
106     overload().arg<Type>(index_).parser = fn;
107 }
108
109 ///////////////////////////////////////////////////////////////////////////
110 // senf::console::ParsedCommandAttributor<Overload>
111
112 template <class Overload>
113 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
114     const
115 {
116     return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
117 }
118
119 template <class Overload>
120 prefix_
121 senf::console::ParsedCommandAttributor<Overload>::ParsedCommandAttributor(Overload & overload,
122                                                                           unsigned index)
123     : ParsedCommandAttributorBase (overload, index)
124 {}
125
126 ///////////////////////////////////////////////////////////////////////////
127 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
128
129 template <class Overload, class Self, class ReturnType>
130 prefix_ Self
131 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
132     const
133 {
134     this->ParsedCommandAttributorBase::nodeDoc(doc);
135     return static_cast<Self const &>(*this);
136 }
137
138 template <class Overload, class Self, class ReturnType>
139 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
140 overloadDoc(std::string const & doc)
141     const
142 {
143     this->ParsedCommandAttributorBase::overloadDoc(doc);
144     return static_cast<Self const &>(*this);
145 }
146
147 template <class Overload, class Self, class ReturnType>
148 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
149 formatter(typename Overload::Formatter f)
150     const
151 {
152     this->overload().formatter(f);
153     return static_cast<Self const &>(*this);
154 }
155
156 template <class Overload, class Self, class ReturnType>
157 prefix_
158 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
159 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
160     : ParsedCommandAttributor<Overload> (overload, index)
161 {}
162
163 template <class Overload, class Self>
164 prefix_ Self
165 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
166     const
167 {
168     this->ParsedCommandAttributorBase::nodeDoc(doc);
169     return static_cast<Self const &>(*this);
170 }
171
172 template <class Overload, class Self>
173 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
174 overloadDoc(std::string const & doc)
175     const
176 {
177     this->ParsedCommandAttributorBase::overloadDoc(doc);
178     return static_cast<Self const &>(*this);
179 }
180
181 template <class Overload, class Self>
182 prefix_
183 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
184 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
185     : ParsedCommandAttributor<Overload> (overload, index)
186 {}
187
188 ///////////////////////////////////////////////////////////////////////////
189 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
190
191 template <class Overload, unsigned index, bool flag>
192 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
193 senf::console::ParsedArgumentAttributor<Overload,index,flag>::arg()
194     const
195 {
196     return next();
197 }
198
199 template <class Overload, unsigned index, bool flag>
200 template <class ArgumentPack>
201 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
202 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
203 argInfo(ArgumentPack const & args)
204     const
205 {
206 #   define ProcessArg(tag) \
207         argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
208
209     ProcessArg(name);
210     ProcessArg(description);
211     ProcessArg(default_value);
212     ProcessArg(type_name);
213     ProcessArg(default_doc);
214     ProcessArg(parser);
215
216     return next();
217
218 #   undef HaveArg
219 }
220
221 template <class Overload, unsigned index, bool flag>
222 template <class Kw, class ArgumentPack>
223 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
224 argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_)
225     const
226 {}
227
228 template <class Overload, unsigned index, bool flag>
229 template <class ArgumentPack>
230 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
231 argInfo(boost::parameter::keyword<kw::type::name> const &, ArgumentPack const & args,
232         boost::mpl::true_)
233     const
234 {
235     this->argName(args[kw::name]);
236 }
237
238 template <class Overload, unsigned index, bool flag>
239 template <class ArgumentPack>
240 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
241 argInfo(boost::parameter::keyword<kw::type::description> const &, ArgumentPack const & args,
242         boost::mpl::true_)
243     const
244 {
245     this->argDoc(args[kw::description]);
246 }
247
248 template <class Overload, unsigned index, bool flag>
249 template <class ArgumentPack>
250 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
251 argInfo(boost::parameter::keyword<kw::type::default_value> const &, ArgumentPack const & args,
252         boost::mpl::true_)
253     const
254 {
255     this->defaultValue(args[kw::default_value]);
256 }
257
258 template <class Overload, unsigned index, bool flag>
259 template <class ArgumentPack>
260 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
261 argInfo(boost::parameter::keyword<kw::type::type_name> const &, ArgumentPack const & args,
262         boost::mpl::true_)
263     const
264 {
265     this->typeName(args[kw::type_name]);
266 }
267
268 template <class Overload, unsigned index, bool flag>
269 template <class ArgumentPack>
270 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
271 argInfo(boost::parameter::keyword<kw::type::default_doc> const &, ArgumentPack const & args,
272         boost::mpl::true_)
273     const
274 {
275     BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
276     this->defaultDoc(args[kw::default_doc]);
277 }
278
279 template <class Overload, unsigned index, bool flag>
280 template <class ArgumentPack>
281 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
282 argInfo(boost::parameter::keyword<kw::type::parser> const &, ArgumentPack const & args,
283         boost::mpl::true_)
284     const
285 {
286     this->parser(args[kw::parser]);
287 }
288
289 template <class Overload, unsigned index, bool flag>
290 prefix_
291 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
292 ParsedArgumentAttributor(Overload & overload)
293     : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
294 {}
295
296 template <class Overload, unsigned index, bool flag>
297 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
298 senf::console::ParsedArgumentAttributor<Overload,index,flag>::next()
299     const
300 {
301     return ParsedArgumentAttributor<Overload, index+1>(this->overload());
302 }
303
304 template <class Overload, unsigned index, bool flag>
305 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
306 defaultValue(value_type const & value)
307     const
308 {
309     ParsedCommandAttributorBase::defaultValue<value_type>(value);
310 }
311
312 template <class Overload, unsigned index, bool flag>
313 template <class Fn>
314 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
315     const
316 {
317     ParsedCommandAttributorBase::parser<value_type>(fn);
318 }
319
320 ///////////////////////////////////////////////////////////////////////////
321 // senf::console::ParsedArgumentAttributor<Overload, index, false>
322
323 template <class Overload, unsigned index>
324 prefix_
325 senf::console::ParsedArgumentAttributor<Overload, index, false>::
326 ParsedArgumentAttributor(Overload & overload)
327     : ParsedArgumentAttributorBase< Overload, 
328                                      ParsedArgumentAttributor<Overload, index, false> > (overload, index)
329 {}
330
331 ///////////////////////////////////////////////////////////////////////////
332 // namespace members
333
334 namespace senf {
335 namespace console {
336 namespace detail {
337
338     struct ParsedCommandAddNodeAccess
339     {
340         template <class Attributor, class Node>
341         static Attributor attributor(Node & node)
342             { return Attributor(node); }
343     };
344
345     // What is THIS about ??
346
347     // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
348     // this argument is given, then the function will be called with the console output stream as
349     // it's first argument.
350     //
351     // This is implemented in the following way: ParsedCommandOverload (the class responsible for
352     // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
353     // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
354     // function will be directly passed to ParsedCommandOverload.
355     //
356     // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
357     // the create member will use boost::bind to DROP the first argument.
358     
359     template <class Traits, 
360               bool ignoreOneArg=! Traits::has_ostream_arg, 
361               unsigned arity=Traits::traits::arity>
362     struct CreateParsedCommandOverload
363     {};
364
365     template <class Traits, unsigned arity>
366     struct CreateParsedCommandOverload<Traits, false, arity>
367     {
368         typedef typename Traits::traits traits;
369         
370         template <class Function>
371         static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn) 
372             { return senf::console::ParsedCommandOverload<traits>::create(fn); };
373     };
374
375 #   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                     \
376                                             SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
377                                             4))
378 #   include BOOST_PP_ITERATE()
379
380     template <class Signature, class Fn>
381     typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
382     addOverloadedCommandNode(senf::console::DirectoryNode & node,  std::string const & name, Fn fn)
383     {
384         senf::console::OverloadedCommandNode & cmdNode (
385             node.hasChild(name) 
386             ? dynamic_cast<senf::console::OverloadedCommandNode &>(node(name))
387             : node.add(name, senf::console::OverloadedCommandNode::create()) );
388
389         typedef senf::console::detail::ParsedCommandTraits<Signature> CmdTraits;
390         typedef senf::console::ParsedCommandOverload<typename CmdTraits::traits> Overload;
391         typedef senf::console::ParsedArgumentAttributor<Overload> Attributor;
392
393         return senf::console::detail::ParsedCommandAddNodeAccess::attributor<Attributor>(
394             cmdNode.add( CreateParsedCommandOverload<CmdTraits>::create(fn) ) );
395     }
396
397 }}}
398
399 template <class Function>
400 typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
401 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
402                                      Function fn, int)
403 {
404     return senf::console::detail::addOverloadedCommandNode<Function>(node, name, fn);
405 }
406
407 template <class Signature>
408 typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
409 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
410                                      boost::function<Signature> fn, int)
411 {
412     return senf::console::detail::addOverloadedCommandNode<Signature>(node, name, fn);
413 }
414
415 template <class Owner, class Function>
416 typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
417 senf::console::senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
418                                      Function fn, int,
419                                      typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
420 {
421     return senf::console::detail::addOverloadedCommandNode<Function>(
422         node, name, senf::membind(fn,&owner));
423 }
424
425 ///////////////////////////////cti.e///////////////////////////////////////
426 #undef prefix_
427
428 \f
429 // Local Variables:
430 // mode: c++
431 // fill-column: 100
432 // comment-column: 40
433 // c-file-style: "senf"
434 // indent-tabs-mode: nil
435 // ispell-local-dictionary: "american"
436 // compile-command: "scons -u test"
437 // End: