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::ArgumentInfo<ParameterType>
39 template <class ParameterType>
40 prefix_ typename senf::console::detail::ArgumentInfo<ParameterType>::ptr
41 senf::console::detail::ArgumentInfo<ParameterType>::create()
43 return ptr(new ArgumentInfo());
46 template <class ParameterType>
47 prefix_ senf::console::detail::ArgumentInfo<ParameterType>::ArgumentInfo()
48 : ArgumentInfoBase ( ArgumentTraits<ParameterType>::description(),
49 ArgumentTraits<ParameterType>::singleToken ),
53 template <class ParameterType>
54 prefix_ std::string senf::console::detail::ArgumentInfo<ParameterType>::defaultValueStr()
57 return hasDefault ? ArgumentTraits<ParameterType>::str(defaultValue) : "";
60 ///////////////////////////////////////////////////////////////////////////
61 // senf::console::ParsedCommandOverloadBase
64 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
66 parameters_.push_back(detail::ArgumentInfo<Type>::create());
69 ///////////////////////////////////////////////////////////////////////////
70 // senf::console::ParsedCommandOverload<FunctionTraits,n>
72 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
73 SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
75 #include BOOST_PP_ITERATE()
77 ///////////////////////////////////////////////////////////////////////////
78 // senf::console::ParsedCommandAttributor<Overload>
80 template <class Overload>
81 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
84 return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
87 template <class Overload>
89 senf::console::ParsedCommandAttributor<Overload>::ParsedCommandAttributor(Overload & overload,
91 : ParsedCommandAttributorBase (overload, index)
94 ///////////////////////////////////////////////////////////////////////////
95 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
97 template <class Overload, class Self, class ReturnType>
99 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
102 this->ParsedCommandAttributorBase::nodeDoc(doc);
103 return static_cast<Self const &>(*this);
106 template <class Overload, class Self, class ReturnType>
107 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
108 shortdoc(std::string const & doc)
111 this->ParsedCommandAttributorBase::shortDoc(doc);
112 return static_cast<Self const &>(*this);
115 template <class Overload, class Self, class ReturnType>
116 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
117 overloadDoc(std::string const & doc)
120 this->ParsedCommandAttributorBase::overloadDoc(doc);
121 return static_cast<Self const &>(*this);
124 template <class Overload, class Self, class ReturnType>
125 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
126 formatter(typename Overload::Formatter f)
129 this->overload().formatter(f);
130 return static_cast<Self const &>(*this);
133 template <class Overload, class Self, class ReturnType>
135 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
136 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
137 : ParsedCommandAttributor<Overload> (overload, index)
140 template <class Overload, class Self>
142 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
145 this->ParsedCommandAttributorBase::nodeDoc(doc);
146 return static_cast<Self const &>(*this);
149 template <class Overload, class Self>
150 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload, Self, void>::
151 shortdoc(std::string const & doc)
154 this->ParsedCommandAttributorBase::shortDoc(doc);
155 return static_cast<Self const &>(*this);
158 template <class Overload, class Self>
159 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
160 overloadDoc(std::string const & doc)
163 this->ParsedCommandAttributorBase::overloadDoc(doc);
164 return static_cast<Self const &>(*this);
167 template <class Overload, class Self>
169 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
170 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
171 : ParsedCommandAttributor<Overload> (overload, index)
174 ///////////////////////////////////////////////////////////////////////////
175 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
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()
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)
192 # define ProcessArg(tag) \
193 argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
196 ProcessArg(description);
197 ProcessArg(default_value);
198 ProcessArg(type_name);
199 ProcessArg(default_doc);
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_)
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,
221 this->argName(args[kw::name]);
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,
231 this->argDoc(args[kw::description]);
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,
241 this->defaultValue(args[kw::default_value]);
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,
251 this->typeName(args[kw::type_name]);
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,
261 BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
262 this->defaultDoc(args[kw::default_doc]);
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,
272 this->parser(args[kw::parser]);
275 template <class Overload, unsigned index, bool flag>
277 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
278 ParsedArgumentAttributor(Overload & overload)
279 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
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()
287 return ParsedArgumentAttributor<Overload, index+1>(this->overload());
290 template <class Overload, unsigned index, bool flag>
291 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
292 defaultValue(value_type const & value)
295 this->overload().arg<index>().defaultValue = value;
296 this->overload().arg(index).hasDefault = true;
299 template <class Overload, unsigned index, bool flag>
301 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
304 this->overload().arg<index>().parser = fn;
307 ///////////////////////////////////////////////////////////////////////////
308 // senf::console::ParsedArgumentAttributor<Overload, index, false>
310 template <class Overload, unsigned index>
312 senf::console::ParsedArgumentAttributor<Overload, index, false>::
313 ParsedArgumentAttributor(Overload & overload)
314 : ParsedArgumentAttributorBase<
315 Overload, ParsedArgumentAttributor<Overload, index, false> > (overload, index)
318 ///////////////////////////////////////////////////////////////////////////
327 struct ParsedCommandAddNodeAccess
329 template <class Attributor, class Node>
330 static Attributor attributor(Node & node)
331 { return Attributor(node); }
334 // What is THIS about ??
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.
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.
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.
348 template <class Traits,
349 bool ignoreOneArg=! Traits::has_ostream_arg,
350 unsigned arity=Traits::traits::arity>
351 struct CreateParsedCommandOverload
354 template <class Traits, unsigned arity>
355 struct CreateParsedCommandOverload<Traits, false, arity>
357 typedef typename Traits::traits traits;
359 template <class Function>
360 static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
361 { return senf::console::ParsedCommandOverload<traits>::create(fn); };
364 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
365 SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
367 # include BOOST_PP_ITERATE()
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)
373 senf::console::OverloadedCommandNode & cmdNode (
375 ? dynamic_cast<senf::console::OverloadedCommandNode &>(node(name))
376 : node.add(name, senf::console::OverloadedCommandNode::create()) );
378 typedef senf::console::detail::ParsedCommandTraits<Signature> CmdTraits;
379 typedef senf::console::ParsedCommandOverload<typename CmdTraits::traits> Overload;
380 typedef senf::console::ParsedArgumentAttributor<Overload> Attributor;
382 return senf::console::detail::ParsedCommandAddNodeAccess::attributor<Attributor>(
383 cmdNode.add( CreateParsedCommandOverload<CmdTraits>::create(fn) ) );
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,
396 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *)
398 return senf::console::detail::addOverloadedCommandNode<Function>(node, name, fn);
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)
406 return senf::console::detail::addOverloadedCommandNode<Signature>(node, name, fn);
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,
413 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
415 return senf::console::detail::addOverloadedCommandNode<Function>(
416 node, name, senf::membind(fn,&owner));
421 ///////////////////////////////cti.e///////////////////////////////////////
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"