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