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>
32 #define prefix_ inline
33 ///////////////////////////////cti.p///////////////////////////////////////
35 ///////////////////////////////////////////////////////////////////////////
36 // senf::console::ParsedCommandOverloadBase
39 prefix_ senf::console::detail::ParameterInfo<Type> &
40 senf::console::ParsedCommandOverloadBase::arg(unsigned n)
43 return dynamic_cast<detail::ParameterInfo<Type> &>(arg(n));
47 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
49 parameters_.push_back(detail::ParameterInfo<Type>::create());
52 ///////////////////////////////////////////////////////////////////////////
53 // senf::console::ParsedCommandOverload<FunctionTraits,n>
55 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
56 SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
58 #include BOOST_PP_ITERATE()
60 ///////////////////////////////////////////////////////////////////////////
61 // senf::console::ParsedCommandAttributorBase
64 prefix_ void senf::console::ParsedCommandAttributorBase::defaultValue(Type const & value)
67 overload().arg<Type>(index_).defaultValue = value;
68 overload().arg(index_).hasDefault = true;
71 ///////////////////////////////////////////////////////////////////////////
72 // senf::console::ParsedCommandAttributor<Overload>
74 template <class Overload>
75 prefix_ Overload & senf::console::ParsedCommandAttributor<Overload>::overload()
78 return static_cast<Overload &>(ParsedCommandAttributorBase::overload());
81 template <class Overload>
83 senf::console::ParsedCommandAttributor<Overload>::ParsedCommandAttributor(Overload & overload,
85 : ParsedCommandAttributorBase (overload, index)
88 ///////////////////////////////////////////////////////////////////////////
89 // senf::console::ParsedAttributeAttributorBase<Overload,Self>
91 template <class Overload, class Self>
93 senf::console::ParsedAttributeAttributorBase<Overload,Self>::doc(std::string const & doc)
96 this->ParsedCommandAttributorBase::nodeDoc(doc);
97 return static_cast<Self const &>(*this);
100 template <class Overload, class Self>
101 prefix_ Self senf::console::ParsedAttributeAttributorBase<Overload,Self>::
102 overloadDoc(std::string const & doc)
105 this->ParsedCommandAttributorBase::overloadDoc(doc);
106 return static_cast<Self const &>(*this);
109 template <class Overload, class Self>
111 senf::console::ParsedAttributeAttributorBase<Overload,Self>::
112 ParsedAttributeAttributorBase(Overload & overload, unsigned index)
113 : ParsedCommandAttributor<Overload> (overload, index)
116 ///////////////////////////////////////////////////////////////////////////
117 // senf::console::ParsedAttributeAttributor<Overload,index,flag>
119 template <class Overload, unsigned index, bool flag>
120 prefix_ senf::console::ParsedAttributeAttributor<Overload, index+1>
121 senf::console::ParsedAttributeAttributor<Overload,index,flag>::arg(std::string const & name,
122 std::string const & doc)
130 template <class Overload, unsigned index, bool flag>
131 prefix_ senf::console::ParsedAttributeAttributor<Overload, index+1>
132 senf::console::ParsedAttributeAttributor<Overload,index,flag>::arg(std::string const & name,
133 std::string const & doc,
134 value_type const & value)
143 template <class Overload, unsigned index, bool flag>
145 senf::console::ParsedAttributeAttributor<Overload,index,flag>::
146 ParsedAttributeAttributor(Overload & overload)
147 : ParsedAttributeAttributorBase<Overload, ParsedAttributeAttributor> (overload, index)
150 template <class Overload, unsigned index, bool flag>
151 prefix_ senf::console::ParsedAttributeAttributor<Overload, index+1>
152 senf::console::ParsedAttributeAttributor<Overload,index,flag>::next()
155 return ParsedAttributeAttributor<Overload, index+1>(this->overload());
158 template <class Overload, unsigned index, bool flag>
159 prefix_ void senf::console::ParsedAttributeAttributor<Overload,index,flag>::
160 defaultValue(value_type const & value)
163 ParsedCommandAttributorBase::defaultValue<arg_type>(value);
166 ///////////////////////////////////////////////////////////////////////////
167 // senf::console::ParsedAttributeAttributor<Overload, index, false>
169 template <class Overload, unsigned index>
171 senf::console::ParsedAttributeAttributor<Overload, index, false>::
172 ParsedAttributeAttributor(Overload & overload)
173 : ParsedAttributeAttributorBase<Overload, ParsedAttributeAttributor> (overload, index)
176 ///////////////////////////////////////////////////////////////////////////
181 // What is THIS about ??
183 // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
184 // this argument is given, then the function will be called with the console output stream as
185 // it's first argument.
187 // This is implemented in the following way: ParsedCommandOverload (the class responsible for
188 // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
189 // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
190 // function will be directly passed to ParsedCommandOverload.
192 // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
193 // the create member will use boost::bind to DROP the first argument.
195 template <class Traits, bool ignoreOneArg, unsigned arity=Traits::arity>
196 struct CreateParsedCommandOverload
199 template <class Traits, unsigned arity>
200 struct CreateParsedCommandOverload<Traits, false, arity>
202 typedef Traits traits;
204 template <class Function>
205 static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
206 { return senf::console::ParsedCommandOverload<traits>::create(fn); };
209 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
210 SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
212 # include BOOST_PP_ITERATE()
216 template <class Function>
217 prefix_ senf::console::ParsedAttributeAttributor<
218 senf::console::ParsedCommandOverload<
219 typename senf::console::detail::ParsedCommandTraits<Function>::traits> >
220 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
223 OverloadedCommandNode & cmdNode (
225 ? dynamic_cast<OverloadedCommandNode &>(node(name))
226 : node.add(name, OverloadedCommandNode::create()) );
228 typedef detail::ParsedCommandTraits<Function> CmdTraits;
229 typedef ParsedCommandOverload<typename CmdTraits::traits> Overload;
230 typedef ParsedAttributeAttributor<Overload> Attributor;
233 cmdNode.add( CreateParsedCommandOverload<
234 typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(fn) ) );
237 template <class Owner, class Function>
238 prefix_ senf::console::ParsedAttributeAttributor<
239 senf::console::ParsedCommandOverload<
240 typename senf::console::detail::ParsedCommandTraits<Function>::traits> >
242 senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
244 typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
246 OverloadedCommandNode & cmdNode (
248 ? dynamic_cast<OverloadedCommandNode &>(node(name))
249 : node.add(name, OverloadedCommandNode::create()) );
251 typedef detail::ParsedCommandTraits<Function> CmdTraits;
252 typedef ParsedCommandOverload<typename CmdTraits::traits> Overload;
253 typedef ParsedAttributeAttributor<Overload> Attributor;
256 cmdNode.add( CreateParsedCommandOverload<
257 typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(
258 senf::membind(fn,&owner)) ) );
261 ///////////////////////////////cti.e///////////////////////////////////////
268 // comment-column: 40
269 // c-file-style: "senf"
270 // indent-tabs-mode: nil
271 // ispell-local-dictionary: "american"
272 // compile-command: "scons -u test"