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() ),
52 template <class ParameterType>
53 prefix_ std::string senf::console::detail::ArgumentInfo<ParameterType>::defaultValueStr()
56 return hasDefault ? ArgumentTraits<ParameterType>::str(defaultValue) : "";
59 ///////////////////////////////////////////////////////////////////////////
60 // senf::console::ParsedCommandOverloadBase
63 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
65 parameters_.push_back(detail::ArgumentInfo<Type>::create());
68 ///////////////////////////////////////////////////////////////////////////
69 // senf::console::ParsedCommandOverload<FunctionTraits,n>
71 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
72 SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
74 #include BOOST_PP_ITERATE()
76 ///////////////////////////////////////////////////////////////////////////
77 // senf::console::ParsedCommandAttributor<Overload>
79 template <class Overload>
80 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
83 return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
86 template <class Overload>
88 senf::console::ParsedCommandAttributor<Overload>::ParsedCommandAttributor(Overload & overload,
90 : ParsedCommandAttributorBase (overload, index)
93 ///////////////////////////////////////////////////////////////////////////
94 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
96 template <class Overload, class Self, class ReturnType>
98 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::doc(std::string const & doc)
101 this->ParsedCommandAttributorBase::nodeDoc(doc);
102 return static_cast<Self const &>(*this);
105 template <class Overload, class Self, class ReturnType>
106 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
107 overloadDoc(std::string const & doc)
110 this->ParsedCommandAttributorBase::overloadDoc(doc);
111 return static_cast<Self const &>(*this);
114 template <class Overload, class Self, class ReturnType>
115 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
116 formatter(typename Overload::Formatter f)
119 this->overload().formatter(f);
120 return static_cast<Self const &>(*this);
123 template <class Overload, class Self, class ReturnType>
125 senf::console::ParsedArgumentAttributorBase<Overload,Self,ReturnType>::
126 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
127 : ParsedCommandAttributor<Overload> (overload, index)
130 template <class Overload, class Self>
132 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::doc(std::string const & doc)
135 this->ParsedCommandAttributorBase::nodeDoc(doc);
136 return static_cast<Self const &>(*this);
139 template <class Overload, class Self>
140 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
141 overloadDoc(std::string const & doc)
144 this->ParsedCommandAttributorBase::overloadDoc(doc);
145 return static_cast<Self const &>(*this);
148 template <class Overload, class Self>
150 senf::console::ParsedArgumentAttributorBase<Overload,Self,void>::
151 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
152 : ParsedCommandAttributor<Overload> (overload, index)
155 ///////////////////////////////////////////////////////////////////////////
156 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
158 template <class Overload, unsigned index, bool flag>
159 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
160 senf::console::ParsedArgumentAttributor<Overload,index,flag>::arg()
166 template <class Overload, unsigned index, bool flag>
167 template <class ArgumentPack>
168 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
169 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
170 argInfo(ArgumentPack const & args)
173 # define ProcessArg(tag) \
174 argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
177 ProcessArg(description);
178 ProcessArg(default_value);
179 ProcessArg(type_name);
180 ProcessArg(default_doc);
188 template <class Overload, unsigned index, bool flag>
189 template <class Kw, class ArgumentPack>
190 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
191 argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_)
195 template <class Overload, unsigned index, bool flag>
196 template <class ArgumentPack>
197 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
198 argInfo(boost::parameter::keyword<kw::type::name> const &, ArgumentPack const & args,
202 this->argName(args[kw::name]);
205 template <class Overload, unsigned index, bool flag>
206 template <class ArgumentPack>
207 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
208 argInfo(boost::parameter::keyword<kw::type::description> const &, ArgumentPack const & args,
212 this->argDoc(args[kw::description]);
215 template <class Overload, unsigned index, bool flag>
216 template <class ArgumentPack>
217 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
218 argInfo(boost::parameter::keyword<kw::type::default_value> const &, ArgumentPack const & args,
222 this->defaultValue(args[kw::default_value]);
225 template <class Overload, unsigned index, bool flag>
226 template <class ArgumentPack>
227 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
228 argInfo(boost::parameter::keyword<kw::type::type_name> const &, ArgumentPack const & args,
232 this->typeName(args[kw::type_name]);
235 template <class Overload, unsigned index, bool flag>
236 template <class ArgumentPack>
237 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
238 argInfo(boost::parameter::keyword<kw::type::default_doc> const &, ArgumentPack const & args,
242 BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
243 this->defaultDoc(args[kw::default_doc]);
246 template <class Overload, unsigned index, bool flag>
247 template <class ArgumentPack>
248 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
249 argInfo(boost::parameter::keyword<kw::type::parser> const &, ArgumentPack const & args,
253 this->parser(args[kw::parser]);
256 template <class Overload, unsigned index, bool flag>
258 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
259 ParsedArgumentAttributor(Overload & overload)
260 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
263 template <class Overload, unsigned index, bool flag>
264 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
265 senf::console::ParsedArgumentAttributor<Overload,index,flag>::next()
268 return ParsedArgumentAttributor<Overload, index+1>(this->overload());
271 template <class Overload, unsigned index, bool flag>
272 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
273 defaultValue(value_type const & value)
276 this->overload().arg<index>().defaultValue = value;
277 this->overload().arg(index).hasDefault = true;
280 template <class Overload, unsigned index, bool flag>
282 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
285 this->overload().arg<index>().parser = fn;
288 ///////////////////////////////////////////////////////////////////////////
289 // senf::console::ParsedArgumentAttributor<Overload, index, false>
291 template <class Overload, unsigned index>
293 senf::console::ParsedArgumentAttributor<Overload, index, false>::
294 ParsedArgumentAttributor(Overload & overload)
295 : ParsedArgumentAttributorBase<
296 Overload, ParsedArgumentAttributor<Overload, index, false> > (overload, index)
299 ///////////////////////////////////////////////////////////////////////////
308 struct ParsedCommandAddNodeAccess
310 template <class Attributor, class Node>
311 static Attributor attributor(Node & node)
312 { return Attributor(node); }
315 // What is THIS about ??
317 // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
318 // this argument is given, then the function will be called with the console output stream as
319 // it's first argument.
321 // This is implemented in the following way: ParsedCommandOverload (the class responsible for
322 // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
323 // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
324 // function will be directly passed to ParsedCommandOverload.
326 // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
327 // the create member will use boost::bind to DROP the first argument.
329 template <class Traits,
330 bool ignoreOneArg=! Traits::has_ostream_arg,
331 unsigned arity=Traits::traits::arity>
332 struct CreateParsedCommandOverload
335 template <class Traits, unsigned arity>
336 struct CreateParsedCommandOverload<Traits, false, arity>
338 typedef typename Traits::traits traits;
340 template <class Function>
341 static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
342 { return senf::console::ParsedCommandOverload<traits>::create(fn); };
345 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
346 SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
348 # include BOOST_PP_ITERATE()
350 template <class Signature, class Fn>
351 typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
352 addOverloadedCommandNode(senf::console::DirectoryNode & node, std::string const & name, Fn fn)
354 senf::console::OverloadedCommandNode & cmdNode (
356 ? dynamic_cast<senf::console::OverloadedCommandNode &>(node(name))
357 : node.add(name, senf::console::OverloadedCommandNode::create()) );
359 typedef senf::console::detail::ParsedCommandTraits<Signature> CmdTraits;
360 typedef senf::console::ParsedCommandOverload<typename CmdTraits::traits> Overload;
361 typedef senf::console::ParsedArgumentAttributor<Overload> Attributor;
363 return senf::console::detail::ParsedCommandAddNodeAccess::attributor<Attributor>(
364 cmdNode.add( CreateParsedCommandOverload<CmdTraits>::create(fn) ) );
373 template <class Function>
374 typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
375 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
377 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_callable>::type *)
379 return senf::console::detail::addOverloadedCommandNode<Function>(node, name, fn);
382 template <class Signature>
383 typename senf::console::detail::ParsedCommandTraits<Signature>::Attributor
384 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
385 boost::function<Signature> fn, int)
387 return senf::console::detail::addOverloadedCommandNode<Signature>(node, name, fn);
390 template <class Owner, class Function>
391 typename senf::console::detail::ParsedCommandTraits<Function>::Attributor
392 senf::console::senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
394 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
396 return senf::console::detail::addOverloadedCommandNode<Function>(
397 node, name, senf::membind(fn,&owner));
402 ///////////////////////////////cti.e///////////////////////////////////////
409 // comment-column: 40
410 // c-file-style: "senf"
411 // indent-tabs-mode: nil
412 // ispell-local-dictionary: "american"
413 // compile-command: "scons -u test"