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 internal header */
26 #ifndef IH_SENF_Scheduler_Console_ParsedCommand_
27 #define IH_SENF_Scheduler_Console_ParsedCommand_ 1
30 #include <boost/function.hpp>
31 #include <boost/intrusive_ptr.hpp>
34 ///////////////////////////////ih.p////////////////////////////////////////
39 template < class FunctionTraits,
40 class ReturnType=typename FunctionTraits::result_type,
41 unsigned arity=FunctionTraits::arity >
42 class ParsedCommandOverload;
44 template < class Overload,
46 bool flag=(index < unsigned(Overload::traits::arity)) >
47 class ParsedArgumentAttributor;
51 /** \brief Internal: Argument information structure
53 This class is used to hold argument information for automatically parsed commands.
55 \see ParsedCommandOverloadBase
57 struct ArgumentInfoBase
58 : public intrusive_refcount
60 typedef boost::intrusive_ptr<ArgumentInfoBase> ptr;
64 std::string defaultDoc;
69 explicit ArgumentInfoBase(std::string const & type, bool singleToken=false);
70 virtual ~ArgumentInfoBase();
72 virtual std::string defaultValueStr() const = 0;
75 /** \brief Internal: Argument information structure
77 This class is used to hold argument information for automatically parsed commands.
79 \see ParsedCommandOverloadBase
81 template <class ParameterType>
83 : public ArgumentInfoBase
85 typedef boost::intrusive_ptr<ArgumentInfo> ptr;
86 typedef boost::function<void (ParseCommandInfo::TokensRange const &,
87 ParameterType &)> Parser;
92 ParameterType defaultValue;
95 virtual std::string defaultValueStr() const;
100 // FirstArgType returns void, if the function has no arguments, otherwise it returns arg1_type
102 template <class Traits, bool flag=(Traits::arity>0)>
108 template <class Traits>
109 struct FirstArgType<Traits,true>
111 typedef typename Traits::arg1_type type;
114 template <class FnunctionP, class Function, bool isFN=boost::is_function<Function>::value>
115 struct ParsedCommandTraits_i
117 static const bool is_callable = false;
118 static const bool is_member = false;
121 template <class FunctionP, class Function>
122 struct ParsedCommandTraits_i<FunctionP, Function, true>
124 typedef FunctionP base_type;
125 typedef typename senf::remove_any_pointer<base_type>::type function_type;
126 typedef boost::function_traits<function_type> base_traits;
127 typedef typename FirstArgType<base_traits>::type first_arg_type;
129 static const bool has_ostream_arg = boost::is_same<first_arg_type, std::ostream &>::value;
131 typedef typename boost::mpl::if_c<
133 typename function_traits_remove_arg<base_traits>::type,
137 typedef typename senf::remove_cvref<typename base_traits::result_type>::type result_type;
139 static const bool is_callable = true;
140 static const bool is_member = boost::is_member_pointer<base_type>::value;
142 typedef typename senf::member_class<base_type>::type class_type;
144 typedef ParsedCommandOverload<traits> Overload;
145 typedef ParsedArgumentAttributor<Overload> Attributor;
148 // Disable auto-parsing for ParseCommandInfo arg -> register manually parsed command
149 template <class FunctionP>
150 struct ParsedCommandTraits_i<FunctionP, void (std::ostream &, ParseCommandInfo const &), true>
153 template <class FunctionP>
154 struct ParsedCommandTraits
155 : public ParsedCommandTraits_i< FunctionP,
156 typename senf::remove_any_pointer<FunctionP>::type >
159 struct ParsedCommandAddNodeAccess;
161 // What is THIS about ??
163 // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
164 // this argument is given, then the function will be called with the console output stream as
165 // it's first argument.
167 // This is implemented in the following way: ParsedCommandOverload (the class responsible for
168 // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
169 // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
170 // function will be directly passed to ParsedCommandOverload.
172 // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
173 // the create member will use boost::bind to DROP the first argument.
175 template <class Traits,
176 bool ignoreOneArg=! Traits::has_ostream_arg,
177 unsigned arity=Traits::traits::arity>
178 struct CreateParsedCommandOverload
181 template <class Traits, unsigned arity>
182 struct CreateParsedCommandOverload<Traits, false, arity>
184 typedef typename Traits::traits traits;
186 template <class Function>
187 static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
188 { return senf::console::ParsedCommandOverload<traits>::create(fn); };
191 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
192 SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
194 # include BOOST_PP_ITERATE()
200 ///////////////////////////////ih.e////////////////////////////////////////
207 // comment-column: 40
208 // c-file-style: "senf"
209 // indent-tabs-mode: nil
210 // ispell-local-dictionary: "american"
211 // compile-command: "scons -u test"