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