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>
131 senf::console::ParsedArgumentAttributorBase<Overload,Self>::doc(std::string const & doc)
134 this->ParsedCommandAttributorBase::nodeDoc(doc);
135 return static_cast<Self const &>(*this);
138 template <class Overload, class Self>
139 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self>::
140 overloadDoc(std::string const & doc)
143 this->ParsedCommandAttributorBase::overloadDoc(doc);
144 return static_cast<Self const &>(*this);
147 template <class Overload, class Self>
149 senf::console::ParsedArgumentAttributorBase<Overload,Self>::
150 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
151 : ParsedCommandAttributor<Overload> (overload, index)
154 ///////////////////////////////////////////////////////////////////////////
155 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
157 template <class Overload, unsigned index, bool flag>
158 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
159 senf::console::ParsedArgumentAttributor<Overload,index,flag>::arg()
165 template <class Overload, unsigned index, bool flag>
166 template <class ArgumentPack>
167 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
168 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
169 argInfo(ArgumentPack const & args)
172 # define ProcessArg(tag) \
173 argInfo( kw:: tag, args, senf::has_parameter< ArgumentPack, kw::type:: tag >() )
176 ProcessArg(description);
177 ProcessArg(default_value);
178 ProcessArg(type_name);
179 ProcessArg(default_doc);
187 template <class Overload, unsigned index, bool flag>
188 template <class Kw, class ArgumentPack>
189 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
190 argInfo(Kw const &, ArgumentPack const &, boost::mpl::false_)
194 template <class Overload, unsigned index, bool flag>
195 template <class ArgumentPack>
196 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
197 argInfo(boost::parameter::keyword<kw::type::name> const &, ArgumentPack const & args,
201 this->argName(args[kw::name]);
204 template <class Overload, unsigned index, bool flag>
205 template <class ArgumentPack>
206 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
207 argInfo(boost::parameter::keyword<kw::type::description> const &, ArgumentPack const & args,
211 this->argDoc(args[kw::description]);
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::default_value> const &, ArgumentPack const & args,
221 this->defaultValue(args[kw::default_value]);
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::type_name> const &, ArgumentPack const & args,
231 this->typeName(args[kw::type_name]);
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_doc> const &, ArgumentPack const & args,
241 BOOST_STATIC_ASSERT(( senf::has_parameter<ArgumentPack, kw::type::default_value>::value ));
242 this->defaultDoc(args[kw::default_doc]);
245 template <class Overload, unsigned index, bool flag>
246 template <class ArgumentPack>
247 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
248 argInfo(boost::parameter::keyword<kw::type::parser> const &, ArgumentPack const & args,
252 this->parser(args[kw::parser]);
255 template <class Overload, unsigned index, bool flag>
257 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
258 ParsedArgumentAttributor(Overload & overload)
259 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
262 template <class Overload, unsigned index, bool flag>
263 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
264 senf::console::ParsedArgumentAttributor<Overload,index,flag>::next()
267 return ParsedArgumentAttributor<Overload, index+1>(this->overload());
270 template <class Overload, unsigned index, bool flag>
271 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
272 defaultValue(value_type const & value)
275 ParsedCommandAttributorBase::defaultValue<value_type>(value);
278 template <class Overload, unsigned index, bool flag>
280 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::parser(Fn fn)
283 ParsedCommandAttributorBase::parser<value_type>(fn);
286 ///////////////////////////////////////////////////////////////////////////
287 // senf::console::ParsedArgumentAttributor<Overload, index, false>
289 template <class Overload, unsigned index>
291 senf::console::ParsedArgumentAttributor<Overload, index, false>::
292 ParsedArgumentAttributor(Overload & overload)
293 : ParsedArgumentAttributorBase< Overload,
294 ParsedArgumentAttributor<Overload, index, false> > (overload, index)
297 ///////////////////////////////////////////////////////////////////////////
302 // What is THIS about ??
304 // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
305 // this argument is given, then the function will be called with the console output stream as
306 // it's first argument.
308 // This is implemented in the following way: ParsedCommandOverload (the class responsible for
309 // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
310 // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
311 // function will be directly passed to ParsedCommandOverload.
313 // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
314 // the create member will use boost::bind to DROP the first argument.
316 template <class Traits, bool ignoreOneArg, unsigned arity=Traits::arity>
317 struct CreateParsedCommandOverload
320 template <class Traits, unsigned arity>
321 struct CreateParsedCommandOverload<Traits, false, arity>
323 typedef Traits traits;
325 template <class Function>
326 static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
327 { return senf::console::ParsedCommandOverload<traits>::create(fn); };
330 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
331 SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
333 # include BOOST_PP_ITERATE()
337 template <class Function>
338 prefix_ senf::console::ParsedArgumentAttributor<
339 senf::console::ParsedCommandOverload<
340 typename senf::console::detail::ParsedCommandTraits<Function>::traits> >
341 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
344 OverloadedCommandNode & cmdNode (
346 ? dynamic_cast<OverloadedCommandNode &>(node(name))
347 : node.add(name, OverloadedCommandNode::create()) );
349 typedef detail::ParsedCommandTraits<Function> CmdTraits;
350 typedef ParsedCommandOverload<typename CmdTraits::traits> Overload;
351 typedef ParsedArgumentAttributor<Overload> Attributor;
354 cmdNode.add( CreateParsedCommandOverload<
355 typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(fn) ) );
358 template <class Owner, class Function>
359 prefix_ senf::console::ParsedArgumentAttributor<
360 senf::console::ParsedCommandOverload<
361 typename senf::console::detail::ParsedCommandTraits<Function>::traits> >
363 senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
365 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
367 OverloadedCommandNode & cmdNode (
369 ? dynamic_cast<OverloadedCommandNode &>(node(name))
370 : node.add(name, OverloadedCommandNode::create()) );
372 typedef detail::ParsedCommandTraits<Function> CmdTraits;
373 typedef ParsedCommandOverload<typename CmdTraits::traits> Overload;
374 typedef ParsedArgumentAttributor<Overload> Attributor;
377 cmdNode.add( CreateParsedCommandOverload<
378 typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(
379 senf::membind(fn,&owner)) ) );
382 ///////////////////////////////////////////////////////////////////////////
383 // senf::console::detail::CheckVoidReturn<Type>
385 template <class Type>
387 prefix_ void senf::console::detail::CheckVoidReturn<Type>::call(Fn fn, std::ostream & os)
389 ReturnValueTraits<Type>::format(fn(),os);
394 prefix_ void senf::console::detail::CheckVoidReturn<void>::call(Fn fn, std::ostream & os)
399 ///////////////////////////////cti.e///////////////////////////////////////
406 // comment-column: 40
407 // c-file-style: "senf"
408 // indent-tabs-mode: nil
409 // ispell-local-dictionary: "american"
410 // compile-command: "scons -u test"