Fix documentation build under maverick (doxygen 1.7.1)
[senf.git] / senf / Utils / Console / ParsedCommand.ih
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief ParsedCommand internal header */
25
26 #ifndef IH_SENF_Scheduler_Console_ParsedCommand_
27 #define IH_SENF_Scheduler_Console_ParsedCommand_ 1
28
29 // Custom includes
30 #include <boost/function.hpp>
31 #include <boost/intrusive_ptr.hpp>
32 #include "Parse.hh"
33
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
35
36 namespace senf {
37 namespace console {
38
39     template < class FunctionTraits,
40                class ReturnType=typename FunctionTraits::result_type,
41                unsigned arity=FunctionTraits::arity >
42     class ParsedCommandOverload;
43
44     template < class Overload,
45                unsigned index=0,
46                bool flag=(index < unsigned(Overload::traits::arity)) >
47     class ParsedArgumentAttributor;
48
49 namespace detail {
50
51     /** \brief Internal: Argument information structure
52
53         This class is used to hold argument information for automatically parsed commands.
54
55         \see ParsedCommandOverloadBase
56      */
57     struct ArgumentInfoBase
58         : public intrusive_refcount
59     {
60         typedef boost::intrusive_ptr<ArgumentInfoBase> ptr;
61
62         std::string type;
63         std::string name;
64         std::string defaultDoc;
65         bool hasDefault;
66         std::string doc;
67         bool singleToken;
68
69         explicit ArgumentInfoBase(std::string const & type, bool singleToken=false);
70         virtual ~ArgumentInfoBase();
71
72         virtual std::string defaultValueStr() const = 0;
73     };
74
75     /** \brief Internal: Argument information structure
76
77         This class is used to hold argument information for automatically parsed commands.
78
79         \see ParsedCommandOverloadBase
80      */
81     template <class ParameterType>
82     struct ArgumentInfo
83         : public ArgumentInfoBase
84     {
85         typedef boost::intrusive_ptr<ArgumentInfo> ptr;
86         typedef boost::function<void (ParseCommandInfo::TokensRange const &,
87                                       ParameterType &)> Parser;
88
89         static ptr create();
90         ArgumentInfo();
91
92         ParameterType defaultValue;
93         Parser parser;
94
95         virtual std::string defaultValueStr() const;
96     };
97
98 #ifndef DOXYGEN
99
100     // FirstArgType returns void, if the function has no arguments, otherwise it returns arg1_type
101
102     template <class Traits, bool flag=(Traits::arity>0)>
103     struct FirstArgType
104     {
105         typedef void type;
106     };
107
108     template <class Traits>
109     struct FirstArgType<Traits,true>
110     {
111         typedef typename Traits::arg1_type type;
112     };
113
114     template <class FnunctionP, class Function, bool isFN=boost::is_function<Function>::value>
115     struct ParsedCommandTraits_i
116     {
117         static const bool is_callable = false;
118         static const bool is_member = false;
119         static const bool is_simple = false;
120     };
121
122     template <class FunctionP, class Function>
123     struct ParsedCommandTraits_i<FunctionP, Function, true>
124     {
125         typedef FunctionP base_type;
126         typedef typename senf::remove_any_pointer<base_type>::type function_type;
127         typedef boost::function_traits<function_type> base_traits;
128         typedef typename FirstArgType<base_traits>::type first_arg_type;
129
130         static const bool has_ostream_arg = boost::is_same<first_arg_type, std::ostream &>::value;
131
132         typedef typename boost::mpl::if_c<
133             has_ostream_arg,
134             typename function_traits_remove_arg<base_traits>::type,
135             base_traits>
136         ::type traits;
137
138         typedef typename senf::remove_cvref<typename base_traits::result_type>::type result_type;
139
140         static const bool is_callable = true;
141         static const bool is_member = boost::is_member_pointer<base_type>::value;
142         static const bool is_simple = false;
143
144         typedef typename senf::member_class<base_type>::type class_type;
145
146         typedef ParsedCommandOverload<traits> Overload;
147         typedef ParsedArgumentAttributor<Overload> Attributor;
148     };
149
150     // Disable auto-parsing for ParseCommandInfo arg -> register manually parsed command
151     template <class FunctionP>
152     struct ParsedCommandTraits_i<FunctionP, void (std::ostream &, ParseCommandInfo const &), true>
153     {
154         static const bool is_simple = true;
155     };
156
157     template <class FunctionP>
158     struct ParsedCommandTraits
159         : public ParsedCommandTraits_i< FunctionP,
160                                         typename senf::remove_any_pointer<FunctionP>::type >
161     {};
162
163     struct ParsedCommandAddNodeAccess;
164
165     // What is THIS about ??
166
167     // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
168     // this argument is given, then the function will be called with the console output stream as
169     // it's first argument.
170     //
171     // This is implemented in the following way: ParsedCommandOverload (the class responsible for
172     // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
173     // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
174     // function will be directly passed to ParsedCommandOverload.
175     //
176     // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
177     // the create member will use boost::bind to DROP the first argument.
178
179     template <class Traits,
180               bool ignoreOneArg=! Traits::has_ostream_arg,
181               unsigned arity=Traits::traits::arity>
182     struct CreateParsedCommandOverload
183     {};
184
185     template <class Traits, unsigned arity>
186     struct CreateParsedCommandOverload<Traits, false, arity>
187     {
188         typedef typename Traits::traits traits;
189
190         template <class Function>
191         static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn)
192             { return senf::console::ParsedCommandOverload<traits>::create(fn); };
193     };
194
195 #   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                     \
196                                             SENF_ABSOLUTE_INCLUDE_PATH(Utils/Console/ParsedCommand.mpp), \
197                                             4))
198 #   include BOOST_PP_ITERATE()
199
200 #endif
201
202 }}}
203
204 //-/////////////////////////////////////////////////////////////////////////////////////////////////
205 #endif
206
207 \f
208 // Local Variables:
209 // mode: c++
210 // fill-column: 100
211 // comment-column: 40
212 // c-file-style: "senf"
213 // indent-tabs-mode: nil
214 // ispell-local-dictionary: "american"
215 // compile-command: "scons -u test"
216 // End: