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 <boost/parameter/binding.hpp>
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 ///////////////////////////////////////////////////////////////////////////
103 // senf::console::ParsedCommandAttributor<Overload>
105 template <class Overload>
106 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
109 return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
112 template <class Overload>
114 senf::console::ParsedCommandAttributor<Overload>::ParsedCommandAttributor(Overload & overload,
116 : ParsedCommandAttributorBase (overload, index)
119 ///////////////////////////////////////////////////////////////////////////
120 // senf::console::ParsedArgumentAttributorBase<Overload,Self>
122 template <class Overload, class Self>
124 senf::console::ParsedArgumentAttributorBase<Overload,Self>::doc(std::string const & doc)
127 this->ParsedCommandAttributorBase::nodeDoc(doc);
128 return static_cast<Self const &>(*this);
131 template <class Overload, class Self>
132 prefix_ Self senf::console::ParsedArgumentAttributorBase<Overload,Self>::
133 overloadDoc(std::string const & doc)
136 this->ParsedCommandAttributorBase::overloadDoc(doc);
137 return static_cast<Self const &>(*this);
140 template <class Overload, class Self>
142 senf::console::ParsedArgumentAttributorBase<Overload,Self>::
143 ParsedArgumentAttributorBase(Overload & overload, unsigned index)
144 : ParsedCommandAttributor<Overload> (overload, index)
147 ///////////////////////////////////////////////////////////////////////////
148 // senf::console::ParsedArgumentAttributor<Overload,index,flag>
150 template <class Overload, unsigned index, bool flag>
151 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
152 senf::console::ParsedArgumentAttributor<Overload,index,flag>::arg()
158 template <class Overload, unsigned index, bool flag>
159 template <class ArgumentPack>
160 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
161 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
162 argInfo(ArgumentPack const & args)
165 # define HaveArg(tag) boost::is_same< \
166 typename boost::parameter::binding<ArgumentPack, tag>::type, void >()
168 argInfo( kw::name, args, HaveArg(kw::type::name) );
169 argInfo( kw::description, args, HaveArg(kw::type::description) );
170 argInfo( kw::default_value, args, HaveArg(kw::type::default_value) );
177 template <class Overload, unsigned index, bool flag>
178 template <class Kw, class ArgumentPack>
179 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
180 argInfo(Kw const &, ArgumentPack const &, boost::mpl::true_)
184 template <class Overload, unsigned index, bool flag>
185 template <class ArgumentPack>
186 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
187 argInfo(boost::parameter::keyword<kw::type::name> const &, ArgumentPack const & args,
191 this->argName(args[kw::name]);
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::description> const &, ArgumentPack const & args,
201 this->argDoc(args[kw::description]);
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::default_value> const &, ArgumentPack const & args,
211 this->defaultValue(args[kw::default_value]);
214 template <class Overload, unsigned index, bool flag>
216 senf::console::ParsedArgumentAttributor<Overload,index,flag>::
217 ParsedArgumentAttributor(Overload & overload)
218 : ParsedArgumentAttributorBase<Overload, ParsedArgumentAttributor> (overload, index)
221 template <class Overload, unsigned index, bool flag>
222 prefix_ typename senf::console::ParsedArgumentAttributor<Overload,index,flag>::next_type
223 senf::console::ParsedArgumentAttributor<Overload,index,flag>::next()
226 return ParsedArgumentAttributor<Overload, index+1>(this->overload());
229 template <class Overload, unsigned index, bool flag>
230 prefix_ void senf::console::ParsedArgumentAttributor<Overload,index,flag>::
231 defaultValue(value_type const & value)
234 ParsedCommandAttributorBase::defaultValue<arg_type>(value);
237 ///////////////////////////////////////////////////////////////////////////
238 // senf::console::ParsedArgumentAttributor<Overload, index, false>
240 template <class Overload, unsigned index>
242 senf::console::ParsedArgumentAttributor<Overload, index, false>::
243 ParsedArgumentAttributor(Overload & overload)
244 : ParsedArgumentAttributorBase< Overload,
245 ParsedArgumentAttributor<Overload, index, false> > (overload, index)
248 ///////////////////////////////////////////////////////////////////////////
253 // What is THIS about ??
255 // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
256 // this argument is given, then the function will be called with the console output stream as
257 // it's first argument.
259 // This is implemented in the following way: ParsedCommandOverload (the class responsible for
260 // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
261 // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
262 // function will be directly passed to ParsedCommandOverload.
264 // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
265 // the create member will use boost::bind to DROP the first argument.
267 template <class Traits, bool ignoreOneArg, unsigned arity=Traits::arity>
268 struct CreateParsedCommandOverload
271 template <class Traits, unsigned arity>
272 struct CreateParsedCommandOverload<Traits, false, arity>
274 typedef Traits traits;
276 template <class Function>
277 static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
278 { return senf::console::ParsedCommandOverload<traits>::create(fn); };
281 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
282 SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
284 # include BOOST_PP_ITERATE()
288 template <class Function>
289 prefix_ senf::console::ParsedArgumentAttributor<
290 senf::console::ParsedCommandOverload<
291 typename senf::console::detail::ParsedCommandTraits<Function>::traits> >
292 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
295 OverloadedCommandNode & cmdNode (
297 ? dynamic_cast<OverloadedCommandNode &>(node(name))
298 : node.add(name, OverloadedCommandNode::create()) );
300 typedef detail::ParsedCommandTraits<Function> CmdTraits;
301 typedef ParsedCommandOverload<typename CmdTraits::traits> Overload;
302 typedef ParsedArgumentAttributor<Overload> Attributor;
305 cmdNode.add( CreateParsedCommandOverload<
306 typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(fn) ) );
309 template <class Owner, class Function>
310 prefix_ senf::console::ParsedArgumentAttributor<
311 senf::console::ParsedCommandOverload<
312 typename senf::console::detail::ParsedCommandTraits<Function>::traits> >
314 senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
316 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
318 OverloadedCommandNode & cmdNode (
320 ? dynamic_cast<OverloadedCommandNode &>(node(name))
321 : node.add(name, OverloadedCommandNode::create()) );
323 typedef detail::ParsedCommandTraits<Function> CmdTraits;
324 typedef ParsedCommandOverload<typename CmdTraits::traits> Overload;
325 typedef ParsedArgumentAttributor<Overload> Attributor;
328 cmdNode.add( CreateParsedCommandOverload<
329 typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(
330 senf::membind(fn,&owner)) ) );
333 ///////////////////////////////////////////////////////////////////////////
334 // senf::console::detail::CheckVoidReturn<Type>
336 template <class Type>
338 prefix_ void senf::console::detail::CheckVoidReturn<Type>::call(Fn fn, std::ostream & os)
340 ReturnValueTraits<Type>::format(fn(),os);
345 prefix_ void senf::console::detail::CheckVoidReturn<void>::call(Fn fn, std::ostream & os)
350 ///////////////////////////////cti.e///////////////////////////////////////
357 // comment-column: 40
358 // c-file-style: "senf"
359 // indent-tabs-mode: nil
360 // ispell-local-dictionary: "american"
361 // compile-command: "scons -u test"