eeedd3a303f0d7fcad3a163a51ef962ce0558679
[senf.git] / Scheduler / 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::ArgumentInfo<ParameterType>
38
39 template <class ParameterType>
40 prefix_ typename senf::console::detail::ArgumentInfo<ParameterType>::ptr
41 senf::console::detail::ArgumentInfo<ParameterType>::create()
42 {
43     return ptr(new ArgumentInfo());
44 }
45
46 template <class ParameterType>
47 prefix_ senf::console::detail::ArgumentInfo<ParameterType>::ArgumentInfo()
48     : ArgumentInfoBase ( ArgumentTraits<ParameterType>::description() ),
49       defaultValue ()
50 {}
51
52 template <class ParameterType>
53 prefix_ std::string senf::console::detail::ArgumentInfo<ParameterType>::defaultValueStr()
54     const
55 {
56     return hasDefault ? ArgumentTraits<ParameterType>::str(defaultValue) : "";
57 }
58
59 ///////////////////////////////////////////////////////////////////////////
60 // senf::console::ParsedCommandOverloadBase
61
62 template <class Type>
63 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
64 {
65     parameters_.push_back(detail::ArgumentInfo<Type>::create());
66 }
67
68 ///////////////////////////////////////////////////////////////////////////
69 // senf::console::ParsedCommandOverload<FunctionTraits,n>
70
71 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                       \
72                                          SENF_ABSOLUTE_INCLUDE_PATH(Scheduler/Console/ParsedCommand.mpp),   \
73                                          2))
74 #include BOOST_PP_ITERATE()
75
76 ///////////////////////////////////////////////////////////////////////////
77 // senf::console::ParsedCommandAttributor<Overload>
78
79 template <class Overload>
80 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
81     const
82 {
83     return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
84 }
85
86 template <class Overload>
87 prefix_
88 senf::console::ParsedCommandAttributor<Overload>::ParsedCommandAttributor(Overload & overload,
89                                                                           unsigned index)
90     : ParsedCommandAttributorBase (overload, index)
91 {}
92
93 ///////////////////////////////////////////////////////////////////////////
94 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
95
96 template <class Overload, class Self, class ReturnType>
97 prefix_ Self
98 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
99     const
100 {
101     this->ParsedCommandAttributorBase::nodeDoc(doc);
102     return static_cast<Self const &>(*this);
103 }
104
105 template <class Overload, class Self, class ReturnType>
106 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
107 overloadDoc(std::string const & doc)
108     const
109 {
110     this->ParsedCommandAttributorBase::overloadDoc(doc);
111     return static_cast<Self const &>(*this);
112 }
113
114 template <class Overload, class Self, class ReturnType>
115 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
116 formatter(typename Overload::Formatter f)
117     const
118 {
119     this->overload().formatter(f);
120     return static_cast<Self const &>(*this);
121 }
122
123 template <class Overload, class Self, class ReturnType>
124 prefix_
125 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
126 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
127     : ParsedCommandAttributor<Overload> (overload, index)
128 {}
129
130 template <class Overload, class Self>
131 prefix_ Self
132 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
133     const
134 {
135     this->ParsedCommandAttributorBase::nodeDoc(doc);
136     return static_cast<Self const &>(*this);
137 }
138
139 template <class Overload, class Self>
140 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
141 overloadDoc(std::string const & doc)
142     const
143 {
144     this->ParsedCommandAttributorBase::overloadDoc(doc);
145     return static_cast<Self const &>(*this);
146 }
147
148 template <class Overload, class Self>
149 prefix_
150 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
151 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
152     : ParsedCommandAttributor<Overload> (overload, index)
153 {}
154
155 ///////////////////////////////////////////////////////////////////////////
156 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
157
158 template <class Overload, unsigned index, bool flag>
159 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
160 senf::console::ParsedArgumentAttributor<Overload,index,flag>::arg()
161     const
162 {
163     return next();
164 }
165
166 template <class Overload, unsigned index, bool flag>
167 template <class ArgumentPack>
168 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
169 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
170 argInfo(ArgumentPack const & args)
171     const
172 {
173 #   define ProcessArg(tag) \
174         argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
175
176     ProcessArg(name);
177     ProcessArg(description);
178     ProcessArg(default_value);
179     ProcessArg(type_name);
180     ProcessArg(default_doc);
181     ProcessArg(parser);
182
183     return next();
184
185 #   undef HaveArg
186 }
187
188 template <class Overload, unsigned index, bool flag>
189 template <class Kw, class ArgumentPack>
190 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
191 argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_)
192     const
193 {}
194
195 template <class Overload, unsigned index, bool flag>
196 template <class ArgumentPack>
197 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
198 argInfo(boost::parameter::keyword<kw::type::name> const &, ArgumentPack const & args,
199         boost::mpl::true_)
200     const
201 {
202     this->argName(args[kw::name]);
203 }
204
205 template <class Overload, unsigned index, bool flag>
206 template <class ArgumentPack>
207 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
208 argInfo(boost::parameter::keyword<kw::type::description> const &, ArgumentPack const & args,
209         boost::mpl::true_)
210     const
211 {
212     this->argDoc(args[kw::description]);
213 }
214
215 template <class Overload, unsigned index, bool flag>
216 template <class ArgumentPack>
217 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
218 argInfo(boost::parameter::keyword<kw::type::default_value> const &, ArgumentPack const & args,
219         boost::mpl::true_)
220     const
221 {
222     this->defaultValue(args[kw::default_value]);
223 }
224
225 template <class Overload, unsigned index, bool flag>
226 template <class ArgumentPack>
227 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
228 argInfo(boost::parameter::keyword<kw::type::type_name> const &, ArgumentPack const & args,
229         boost::mpl::true_)
230     const
231 {
232     this->typeName(args[kw::type_name]);
233 }
234
235 template <class Overload, unsigned index, bool flag>
236 template <class ArgumentPack>
237 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
238 argInfo(boost::parameter::keyword<kw::type::default_doc> const &, ArgumentPack const & args,
239         boost::mpl::true_)
240     const
241 {
242     BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
243     this->defaultDoc(args[kw::default_doc]);
244 }
245
246 template <class Overload, unsigned index, bool flag>
247 template <class ArgumentPack>
248 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
249 argInfo(boost::parameter::keyword<kw::type::parser> const &, ArgumentPack const & args,
250         boost::mpl::true_)
251     const
252 {
253     this->parser(args[kw::parser]);
254 }
255
256 template <class Overload, unsigned index, bool flag>
257 prefix_
258 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
259 ParsedArgumentAttributor(Overload & overload)
260     : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
261 {}
262
263 template <class Overload, unsigned index, bool flag>
264 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
265 senf::console::ParsedArgumentAttributor<Overload,index,flag>::next()
266     const
267 {
268     return ParsedArgumentAttributor<Overload, index+1>(this->overload());
269 }
270
271 template <class Overload, unsigned index, bool flag>
272 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
273 defaultValue(value_type const & value)
274     const
275 {
276     this->overload().arg<index>().defaultValue = value;
277     this->overload().arg(index).hasDefault = true;
278 }
279
280 template <class Overload, unsigned index, bool flag>
281 template <class Fn>
282 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
283     const
284 {
285     this->overload().arg<index>().parser = fn;
286 }
287
288 ///////////////////////////////////////////////////////////////////////////
289 // senf::console::ParsedArgumentAttributor<Overload, index, false>
290
291 template <class Overload, unsigned index>
292 prefix_
293 senf::console::ParsedArgumentAttributor<Overload, index, false>::
294 ParsedArgumentAttributor(Overload & overload)
295     : ParsedArgumentAttributorBase< 
296           Overload, ParsedArgumentAttributor<Overload, index, false> > (overload, index)
297 {}
298
299 ///////////////////////////////////////////////////////////////////////////
300 // namespace members
301
302 namespace senf {
303 namespace console {
304 namespace detail {
305
306 #ifndef DOXYGEN
307
308     struct ParsedCommandAddNodeAccess
309     {
310         template <class Attributor, class Node>
311         static Attributor attributor(Node & node)
312             { return Attributor(node); }
313     };
314
315     // What is THIS about ??
316
317     // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
318     // this argument is given, then the function will be called with the console output stream as
319     // it's first argument.
320     //
321     // This is implemented in the following way: ParsedCommandOverload (the class responsible for
322     // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
323     // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
324     // function will be directly passed to ParsedCommandOverload.
325     //
326     // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
327     // the create member will use boost::bind to DROP the first argument.
328     
329     template <class Traits, 
330               bool ignoreOneArg=! Traits::has_ostream_arg, 
331               unsigned arity=Traits::traits::arity>
332     struct CreateParsedCommandOverload
333     {};
334
335     template <class Traits, unsigned arity>
336     struct CreateParsedCommandOverload<Traits, false, arity>
337     {
338         typedef typename Traits::traits traits;
339         
340         template <class Function>
341         static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn) 
342             { return senf::console::ParsedCommandOverload<traits>::create(fn); };
343     };
344
345 #   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                     \
346                                             SENF_ABSOLUTE_INCLUDE_PATH(Scheduler/Console/ParsedCommand.mpp), \
347                                             4))
348 #   include BOOST_PP_ITERATE()
349
350     template <class Signature, class Fn>
351     typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
352     addOverloadedCommandNode(senf::console::DirectoryNode & node,  std::string const & name, Fn fn)
353     {
354         senf::console::OverloadedCommandNode & cmdNode (
355             node.hasChild(name) 
356             ? dynamic_cast<senf::console::OverloadedCommandNode &>(node(name))
357             : node.add(name, senf::console::OverloadedCommandNode::create()) );
358
359         typedef senf::console::detail::ParsedCommandTraits<Signature> CmdTraits;
360         typedef senf::console::ParsedCommandOverload<typename CmdTraits::traits> Overload;
361         typedef senf::console::ParsedArgumentAttributor<Overload> Attributor;
362
363         return senf::console::detail::ParsedCommandAddNodeAccess::attributor<Attributor>(
364             cmdNode.add( CreateParsedCommandOverload<CmdTraits>::create(fn) ) );
365     }
366
367 #endif
368
369 }}}
370
371 #ifndef DOXYGEN
372
373 template <class Function>
374 typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
375 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
376                                      Function fn, int,
377                                      typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *)
378 {
379     return senf::console::detail::addOverloadedCommandNode<Function>(node, name, fn);
380 }
381
382 template <class Signature>
383 typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
384 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
385                                      boost::function<Signature> fn, int)
386 {
387     return senf::console::detail::addOverloadedCommandNode<Signature>(node, name, fn);
388 }
389
390 template <class Owner, class Function>
391 typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
392 senf::console::senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
393                                      Function fn, int,
394                                      typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
395 {
396     return senf::console::detail::addOverloadedCommandNode<Function>(
397         node, name, senf::membind(fn,&owner));
398 }
399
400 #endif
401
402 ///////////////////////////////cti.e///////////////////////////////////////
403 #undef prefix_
404
405 \f
406 // Local Variables:
407 // mode: c++
408 // fill-column: 100
409 // comment-column: 40
410 // c-file-style: "senf"
411 // indent-tabs-mode: nil
412 // ispell-local-dictionary: "american"
413 // compile-command: "scons -u test"
414 // End: