4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief ParsedCommand inline template implementation */
26 #include "ParsedCommand.ih"
29 #include "../Utils/membind.hh"
30 #include <boost/format.hpp>
31 #include "../Utils/parameter.hh"
33 #define prefix_ inline
34 ///////////////////////////////cti.p///////////////////////////////////////
36 ///////////////////////////////////////////////////////////////////////////
37 // senf::console::detail::ArgumentInfoBase
39 prefix_ senf::console::detail::ArgumentInfoBase::ArgumentInfoBase(std::string const & type_)
40 : type (type_), name (), hasDefault (false)
43 ///////////////////////////////////////////////////////////////////////////
44 // senf::console::detail::ArgumentInfo<ParameterType>
46 template <class ParameterType>
47 prefix_ typename senf::console::detail::ArgumentInfo<ParameterType>::ptr
48 senf::console::detail::ArgumentInfo<ParameterType>::create()
50 return ptr(new ArgumentInfo());
53 template <class ParameterType>
54 prefix_ senf::console::detail::ArgumentInfo<ParameterType>::ArgumentInfo()
55 : ArgumentInfoBase ( ArgumentTraits<ParameterType>::description() ),
59 template <class ParameterType>
60 prefix_ std::string senf::console::detail::ArgumentInfo<ParameterType>::defaultValueStr()
63 return hasDefault ? ArgumentTraits<ParameterType>::str(defaultValue) : "";
66 ///////////////////////////////////////////////////////////////////////////
67 // senf::console::ParsedCommandOverloadBase
70 prefix_ senf::console::detail::ArgumentInfo<Type> &
71 senf::console::ParsedCommandOverloadBase::arg(unsigned n)
74 return dynamic_cast<detail::ArgumentInfo<Type> &>(arg(n));
78 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
80 parameters_.push_back(detail::ArgumentInfo<Type>::create());
83 ///////////////////////////////////////////////////////////////////////////
84 // senf::console::ParsedCommandOverload<FunctionTraits,n>
86 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
87 SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
89 #include BOOST_PP_ITERATE()
91 ///////////////////////////////////////////////////////////////////////////
92 // senf::console::ParsedCommandAttributorBase
95 prefix_ void senf::console::ParsedCommandAttributorBase::defaultValue(Type const & value)
98 overload().arg<Type>(index_).defaultValue = value;
99 overload().arg(index_).hasDefault = true;
102 template <class Type, class Fn>
103 prefix_ void senf::console::ParsedCommandAttributorBase::parser(Fn fn)
106 overload().arg<Type>(index_).parser = fn;
109 ///////////////////////////////////////////////////////////////////////////
110 // senf::console::ParsedCommandAttributor<Overload>
112 template <class Overload>
113 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
116 return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
119 template <class Overload>
121 senf::console::ParsedCommandAttributor<Overload>::ParsedCommandAttributor(Overload & overload,
123 : ParsedCommandAttributorBase (overload, index)
126 ///////////////////////////////////////////////////////////////////////////
127 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
129 template <class Overload, class Self, class ReturnType>
131 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
134 this->ParsedCommandAttributorBase::nodeDoc(doc);
135 return static_cast<Self const &>(*this);
138 template <class Overload, class Self, class ReturnType>
139 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
140 overloadDoc(std::string const & doc)
143 this->ParsedCommandAttributorBase::overloadDoc(doc);
144 return static_cast<Self const &>(*this);
147 template <class Overload, class Self, class ReturnType>
148 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
149 formatter(typename Overload::Formatter f)
152 this->overload().formatter(f);
153 return static_cast<Self const &>(*this);
156 template <class Overload, class Self, class ReturnType>
158 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
159 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
160 : ParsedCommandAttributor<Overload> (overload, index)
163 template <class Overload, class Self>
165 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
168 this->ParsedCommandAttributorBase::nodeDoc(doc);
169 return static_cast<Self const &>(*this);
172 template <class Overload, class Self>
173 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
174 overloadDoc(std::string const & doc)
177 this->ParsedCommandAttributorBase::overloadDoc(doc);
178 return static_cast<Self const &>(*this);
181 template <class Overload, class Self>
183 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
184 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
185 : ParsedCommandAttributor<Overload> (overload, index)
188 ///////////////////////////////////////////////////////////////////////////
189 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
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()
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)
206 # define ProcessArg(tag) \
207 argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
210 ProcessArg(description);
211 ProcessArg(default_value);
212 ProcessArg(type_name);
213 ProcessArg(default_doc);
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_)
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,
235 this->argName(args[kw::name]);
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,
245 this->argDoc(args[kw::description]);
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,
255 this->defaultValue(args[kw::default_value]);
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,
265 this->typeName(args[kw::type_name]);
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,
275 BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
276 this->defaultDoc(args[kw::default_doc]);
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,
286 this->parser(args[kw::parser]);
289 template <class Overload, unsigned index, bool flag>
291 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
292 ParsedArgumentAttributor(Overload & overload)
293 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
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()
301 return ParsedArgumentAttributor<Overload, index+1>(this->overload());
304 template <class Overload, unsigned index, bool flag>
305 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
306 defaultValue(value_type const & value)
309 ParsedCommandAttributorBase::defaultValue<value_type>(value);
312 template <class Overload, unsigned index, bool flag>
314 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
317 ParsedCommandAttributorBase::parser<value_type>(fn);
320 ///////////////////////////////////////////////////////////////////////////
321 // senf::console::ParsedArgumentAttributor<Overload, index, false>
323 template <class Overload, unsigned index>
325 senf::console::ParsedArgumentAttributor<Overload, index, false>::
326 ParsedArgumentAttributor(Overload & overload)
327 : ParsedArgumentAttributorBase< Overload,
328 ParsedArgumentAttributor<Overload, index, false> > (overload, index)
331 ///////////////////////////////////////////////////////////////////////////
338 struct ParsedCommandAddNodeAccess
340 template <class Attributor, class Node>
341 static Attributor attributor(Node & node)
342 { return Attributor(node); }
345 // What is THIS about ??
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.
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.
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.
359 template <class Traits,
360 bool ignoreOneArg=! Traits::has_ostream_arg,
361 unsigned arity=Traits::traits::arity>
362 struct CreateParsedCommandOverload
365 template <class Traits, unsigned arity>
366 struct CreateParsedCommandOverload<Traits, false, arity>
368 typedef typename Traits::traits traits;
370 template <class Function>
371 static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
372 { return senf::console::ParsedCommandOverload<traits>::create(fn); };
375 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
376 SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
378 # include BOOST_PP_ITERATE()
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)
384 senf::console::OverloadedCommandNode & cmdNode (
386 ? dynamic_cast<senf::console::OverloadedCommandNode &>(node(name))
387 : node.add(name, senf::console::OverloadedCommandNode::create()) );
389 typedef senf::console::detail::ParsedCommandTraits<Signature> CmdTraits;
390 typedef senf::console::ParsedCommandOverload<typename CmdTraits::traits> Overload;
391 typedef senf::console::ParsedArgumentAttributor<Overload> Attributor;
393 return senf::console::detail::ParsedCommandAddNodeAccess::attributor<Attributor>(
394 cmdNode.add( CreateParsedCommandOverload<CmdTraits>::create(fn) ) );
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,
404 return senf::console::detail::addOverloadedCommandNode<Function>(node, name, fn);
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)
412 return senf::console::detail::addOverloadedCommandNode<Signature>(node, name, fn);
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,
419 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
421 return senf::console::detail::addOverloadedCommandNode<Function>(
422 node, name, senf::membind(fn,&owner));
425 ///////////////////////////////cti.e///////////////////////////////////////
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"