4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Stefan Bund <g0dil@berlios.de>
29 \brief ParsedCommand internal header */
31 #ifndef IH_SENF_Scheduler_Console_ParsedCommand_
32 #define IH_SENF_Scheduler_Console_ParsedCommand_ 1
35 #include <boost/preprocessor/repetition/enum_trailing.hpp>
36 #include <boost/function.hpp>
37 #include <boost/intrusive_ptr.hpp>
40 //-/////////////////////////////////////////////////////////////////////////////////////////////////
45 template < class FunctionTraits,
46 class ReturnType=typename FunctionTraits::result_type,
47 unsigned arity=FunctionTraits::arity >
48 class ParsedCommandOverload;
50 template < class Overload,
52 bool flag=(index < unsigned(Overload::traits::arity)) >
53 class ParsedArgumentAttributor;
57 /** \brief Internal: Argument information structure
59 This class is used to hold argument information for automatically parsed commands.
61 \see ParsedCommandOverloadBase
63 struct ArgumentInfoBase
64 : public intrusive_refcount
66 typedef boost::intrusive_ptr<ArgumentInfoBase> ptr;
70 std::string defaultDoc;
75 explicit ArgumentInfoBase(std::string const & type, bool singleToken=false);
76 virtual ~ArgumentInfoBase();
78 virtual std::string defaultValueStr() const = 0;
81 /** \brief Internal: Argument information structure
83 This class is used to hold argument information for automatically parsed commands.
85 \see ParsedCommandOverloadBase
87 template <class ParameterType>
89 : public ArgumentInfoBase
91 typedef boost::intrusive_ptr<ArgumentInfo> ptr;
92 typedef boost::function<void (ParseCommandInfo::TokensRange const &,
93 ParameterType &)> Parser;
98 ParameterType defaultValue;
101 virtual std::string defaultValueStr() const;
106 // FirstArgType returns void, if the function has no arguments, otherwise it returns arg1_type
108 template <class Traits, bool flag=(Traits::arity>0)>
114 template <class Traits>
115 struct FirstArgType<Traits,true>
117 typedef typename Traits::arg1_type type;
120 template <class FnunctionP, class Function, bool isFN=boost::is_function<Function>::value>
121 struct ParsedCommandTraits_i
123 static const bool is_callable = false;
124 static const bool is_member = false;
125 static const bool is_simple = false;
128 template <class FunctionP, class Function>
129 struct ParsedCommandTraits_i<FunctionP, Function, true>
131 typedef FunctionP base_type;
132 typedef typename senf::remove_any_pointer<base_type>::type function_type;
133 typedef boost::function_traits<function_type> base_traits;
134 typedef typename FirstArgType<base_traits>::type first_arg_type;
136 static const bool has_ostream_arg = boost::is_same<first_arg_type, std::ostream &>::value;
138 typedef typename boost::mpl::if_c<
140 typename function_traits_remove_arg<base_traits>::type,
144 typedef typename senf::remove_cvref<typename base_traits::result_type>::type result_type;
146 static const bool is_callable = true;
147 static const bool is_member = boost::is_member_pointer<base_type>::value;
148 static const bool is_simple = false;
150 typedef typename senf::member_class<base_type>::type class_type;
152 typedef ParsedCommandOverload<traits> Overload;
153 typedef ParsedArgumentAttributor<Overload> Attributor;
156 // Disable auto-parsing for ParseCommandInfo arg -> register manually parsed command
157 template <class FunctionP>
158 struct ParsedCommandTraits_i<FunctionP, void (std::ostream &, ParseCommandInfo const &), true>
160 static const bool is_simple = true;
163 template <class FunctionP>
164 struct ParsedCommandTraits
165 : public ParsedCommandTraits_i< FunctionP,
166 typename senf::remove_any_pointer<FunctionP>::type >
169 struct ParsedCommandAddNodeAccess;
171 // What is THIS about ??
173 // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
174 // this argument is given, then the function will be called with the console output stream as
175 // it's first argument.
177 // This is implemented in the following way: ParsedCommandOverload (the class responsible for
178 // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
179 // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
180 // function will be directly passed to ParsedCommandOverload.
182 // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
183 // the create member will use boost::bind to DROP the first argument.
185 template <class Traits,
186 bool ignoreOneArg=! Traits::has_ostream_arg,
187 unsigned arity=Traits::traits::arity>
188 struct CreateParsedCommandOverload
191 template <class Traits, unsigned arity>
192 struct CreateParsedCommandOverload<Traits, false, arity>
194 typedef typename Traits::traits traits;
196 template <class Function>
197 static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
198 { return senf::console::ParsedCommandOverload<traits>::create(fn); };
201 # define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY, \
202 SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
204 # include BOOST_PP_ITERATE()
210 //-/////////////////////////////////////////////////////////////////////////////////////////////////
217 // comment-column: 40
218 // c-file-style: "senf"
219 // indent-tabs-mode: nil
220 // ispell-local-dictionary: "american"
221 // compile-command: "scons -u test"