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_ void senf::console::ParsedCommandOverloadBase::addParameter()
72 parameters_.push_back(detail::ArgumentInfo<Type>::create());
75 ///////////////////////////////////////////////////////////////////////////
76 // senf::console::ParsedCommandOverload<FunctionTraits,n>
78 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
79 SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
81 #include BOOST_PP_ITERATE()
83 ///////////////////////////////////////////////////////////////////////////
84 // senf::console::ParsedCommandAttributor<Overload>
86 template <class Overload>
87 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
90 return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
93 template <class Overload>
95 senf::console::ParsedCommandAttributor<Overload>::ParsedCommandAttributor(Overload & overload,
97 : ParsedCommandAttributorBase (overload, index)
100 ///////////////////////////////////////////////////////////////////////////
101 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
103 template <class Overload, class Self, class ReturnType>
105 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
108 this->ParsedCommandAttributorBase::nodeDoc(doc);
109 return static_cast<Self const &>(*this);
112 template <class Overload, class Self, class ReturnType>
113 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
114 overloadDoc(std::string const & doc)
117 this->ParsedCommandAttributorBase::overloadDoc(doc);
118 return static_cast<Self const &>(*this);
121 template <class Overload, class Self, class ReturnType>
122 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
123 formatter(typename Overload::Formatter f)
126 this->overload().formatter(f);
127 return static_cast<Self const &>(*this);
130 template <class Overload, class Self, class ReturnType>
132 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
133 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
134 : ParsedCommandAttributor<Overload> (overload, index)
137 template <class Overload, class Self>
139 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
142 this->ParsedCommandAttributorBase::nodeDoc(doc);
143 return static_cast<Self const &>(*this);
146 template <class Overload, class Self>
147 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
148 overloadDoc(std::string const & doc)
151 this->ParsedCommandAttributorBase::overloadDoc(doc);
152 return static_cast<Self const &>(*this);
155 template <class Overload, class Self>
157 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
158 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
159 : ParsedCommandAttributor<Overload> (overload, index)
162 ///////////////////////////////////////////////////////////////////////////
163 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
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()
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)
180 # define ProcessArg(tag) \
181 argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
184 ProcessArg(description);
185 ProcessArg(default_value);
186 ProcessArg(type_name);
187 ProcessArg(default_doc);
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_)
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,
209 this->argName(args[kw::name]);
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,
219 this->argDoc(args[kw::description]);
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,
229 this->defaultValue(args[kw::default_value]);
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,
239 this->typeName(args[kw::type_name]);
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,
249 BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
250 this->defaultDoc(args[kw::default_doc]);
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,
260 this->parser(args[kw::parser]);
263 template <class Overload, unsigned index, bool flag>
265 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
266 ParsedArgumentAttributor(Overload & overload)
267 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
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()
275 return ParsedArgumentAttributor<Overload, index+1>(this->overload());
278 template <class Overload, unsigned index, bool flag>
279 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
280 defaultValue(value_type const & value)
283 this->overload().arg<index>().defaultValue = value;
284 this->overload().arg(index).hasDefault = true;
287 template <class Overload, unsigned index, bool flag>
289 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
292 this->overload().arg<index>().parser = fn;
295 ///////////////////////////////////////////////////////////////////////////
296 // senf::console::ParsedArgumentAttributor<Overload, index, false>
298 template <class Overload, unsigned index>
300 senf::console::ParsedArgumentAttributor<Overload, index, false>::
301 ParsedArgumentAttributor(Overload & overload)
302 : ParsedArgumentAttributorBase<
303 Overload, ParsedArgumentAttributor<Overload, index, false> > (overload, index)
306 ///////////////////////////////////////////////////////////////////////////
315 struct ParsedCommandAddNodeAccess
317 template <class Attributor, class Node>
318 static Attributor attributor(Node & node)
319 { return Attributor(node); }
322 // What is THIS about ??
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.
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.
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.
336 template <class Traits,
337 bool ignoreOneArg=! Traits::has_ostream_arg,
338 unsigned arity=Traits::traits::arity>
339 struct CreateParsedCommandOverload
342 template <class Traits, unsigned arity>
343 struct CreateParsedCommandOverload<Traits, false, arity>
345 typedef typename Traits::traits traits;
347 template <class Function>
348 static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
349 { return senf::console::ParsedCommandOverload<traits>::create(fn); };
352 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
353 SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
355 # include BOOST_PP_ITERATE()
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)
361 senf::console::OverloadedCommandNode & cmdNode (
363 ? dynamic_cast<senf::console::OverloadedCommandNode &>(node(name))
364 : node.add(name, senf::console::OverloadedCommandNode::create()) );
366 typedef senf::console::detail::ParsedCommandTraits<Signature> CmdTraits;
367 typedef senf::console::ParsedCommandOverload<typename CmdTraits::traits> Overload;
368 typedef senf::console::ParsedArgumentAttributor<Overload> Attributor;
370 return senf::console::detail::ParsedCommandAddNodeAccess::attributor<Attributor>(
371 cmdNode.add( CreateParsedCommandOverload<CmdTraits>::create(fn) ) );
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,
385 return senf::console::detail::addOverloadedCommandNode<Function>(node, name, fn);
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)
393 return senf::console::detail::addOverloadedCommandNode<Signature>(node, name, fn);
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,
400 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
402 return senf::console::detail::addOverloadedCommandNode<Function>(
403 node, name, senf::membind(fn,&owner));
408 ///////////////////////////////cti.e///////////////////////////////////////
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"