14d63a02ce173d1e8956ba35dad042bc1b2be209
[senf.git] / Console / ParsedCommand.hh
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 public header */
25
26 #ifndef HH_ParsedCommand_
27 #define HH_ParsedCommand_ 1
28
29 // Custom includes
30 #include <vector>
31 #include <boost/type_traits/function_traits.hpp>
32 #include <boost/type_traits/is_member_pointer.hpp>
33 #include <boost/type_traits/is_same.hpp>
34 #include <boost/mpl/if.hpp>
35 #include <boost/utility.hpp>
36 #include <boost/parameter/keyword.hpp>
37 #include <boost/parameter/parameters.hpp>
38 #include "../config.hh"
39 #include "OverloadedCommand.hh"
40 #include "ParseParameter.hh"
41 #include "../Utils/type_traits.hh"
42
43 #include "ParsedCommand.ih"
44 #include "ParsedCommand.mpp"
45 ///////////////////////////////hh.p////////////////////////////////////////
46
47 namespace senf {
48 namespace console {
49
50     class ParsedCommandOverloadBase
51         : public CommandOverload
52     {
53     public:
54         typedef boost::intrusive_ptr<ParsedCommandOverloadBase> ptr;
55
56         detail::ParameterInfoBase & arg(unsigned n) const;
57         template <class Type> detail::ParameterInfo<Type> & arg(unsigned n) const;
58
59         void doc(std::string const & d);
60
61     protected:
62         ParsedCommandOverloadBase();
63
64         template <class Type>
65         void addParameter();
66
67     private:
68         virtual unsigned v_numArguments() const;
69         virtual void v_argumentDoc(unsigned index, ArgumentDoc & doc) const;
70         virtual std::string v_doc() const;
71
72         typedef std::vector<detail::ParameterInfoBase::ptr> Parameters;
73         Parameters parameters_;
74         std::string doc_;
75     };
76
77     template <class FunctionTraits, unsigned arity=FunctionTraits::arity>
78     class ParsedCommandOverload {};
79
80 #   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                     \
81                                             SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
82                                             1))
83 #   include BOOST_PP_ITERATE()
84
85     class ParsedCommandAttributorBase
86     {
87     public:
88         OverloadedCommandNode & node() const;
89         operator OverloadedCommandNode & () const;
90
91     protected:
92         ParsedCommandAttributorBase(ParsedCommandOverloadBase & overload, unsigned index);
93
94         void argName(std::string const & name) const;
95         void argDoc(std::string const & doc) const;
96         template <class Type> void defaultValue(Type const & value) const;
97
98         ParsedCommandOverloadBase & overload() const;
99         void overloadDoc(std::string const & doc) const;
100
101         void nodeDoc(std::string const & doc) const;
102         
103     private:
104         ParsedCommandOverloadBase & overload_;
105         unsigned index_;
106     };
107
108     template <class Overload>
109     class ParsedCommandAttributor
110         : public ParsedCommandAttributorBase
111     {
112     public:
113         Overload & overload() const;
114
115     protected:
116         ParsedCommandAttributor(Overload & overload, unsigned index);
117
118     private:
119     };
120     
121     namespace tag {
122         BOOST_PARAMETER_KEYWORD(detail, name_);
123         BOOST_PARAMETER_KEYWORD(detail, description_);
124         BOOST_PARAMETER_KEYWORD(detail, default_value_);
125     }
126
127     template <class Overload, class Self>
128     class ParsedAttributeAttributorBase
129         : public ParsedCommandAttributor<Overload>
130     {
131     public:
132         Self doc(std::string const & doc) const;
133         Self overloadDoc(std::string const & doc) const;
134
135     protected:
136         ParsedAttributeAttributorBase(Overload & overload, unsigned index);
137
138     private:
139     };
140
141     template < class Overload, 
142                unsigned index=0, 
143                bool flag=(index < unsigned(Overload::traits::arity)) >
144     class ParsedAttributeAttributor
145         : public ParsedAttributeAttributorBase< Overload, 
146                                                 ParsedAttributeAttributor<Overload, index, flag> >
147     {
148     public:
149         typedef typename senf::function_traits_arg_type< 
150             typename Overload::traits, int(index) >::type arg_type;
151         typedef typename senf::remove_cvref< arg_type >::type value_type;
152         typedef ParsedAttributeAttributor<Overload, index+1> next_type;
153
154         typedef OverloadedCommandNode node_type;
155         typedef ParsedAttributeAttributor return_type;
156
157         typedef boost::parameter::parameters<
158             tag::detail::name_,
159             tag::detail::description_,
160             tag::detail::default_value_> arg_params;
161
162         next_type arg() const;
163
164 #       define BOOST_PP_ITERATION_PARAMS_1 \
165             (4, (1, 3, SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), 5))
166 #       include BOOST_PP_ITERATE()
167
168     private:
169         explicit ParsedAttributeAttributor(Overload & overload);
170
171         template <class ArgumentPack>
172         next_type argInfo(ArgumentPack const & args) const;
173
174         template <class ArgumentPack>
175         next_type argInfo(ArgumentPack const & args, boost::mpl::true_) const;
176         template <class ArgumentPack>
177         next_type argInfo(ArgumentPack const & args, boost::mpl::false_) const;
178
179         next_type argInfo(std::string const & name, std::string const & doc) const;
180         next_type argInfo(std::string const & name, std::string const & doc,
181                           value_type const & value) const;
182
183         ParsedAttributeAttributor<Overload, index+1> next() const;
184
185         void defaultValue(value_type const & value) const;
186
187         template <class O, unsigned i, bool f>
188         friend class ParsedAttributeAttributor;
189         
190         template <class Function>
191         friend ParsedAttributeAttributor<
192             ParsedCommandOverload<typename detail::ParsedCommandTraits<Function>::traits> >
193         senf_console_add_node(DirectoryNode & node, std::string const & name, Function fn, int);
194         
195         template <class Owner, class Function>
196         friend ParsedAttributeAttributor<
197             ParsedCommandOverload<typename detail::ParsedCommandTraits<Function>::traits> >
198         senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
199                               Function fn, int,
200                               typename boost::enable_if_c<
201                                   detail::ParsedCommandTraits<Function>::is_member>::type * = 0);
202     };
203
204     template <class Overload, unsigned index>
205     class ParsedAttributeAttributor<Overload, index, false>
206         : public ParsedCommandAttributor< Overload >
207     {
208     public:
209         typedef OverloadedCommandNode node_type;
210         typedef ParsedAttributeAttributor return_type;
211
212     private:
213         explicit ParsedAttributeAttributor(Overload & overload);
214
215         template <class O, unsigned i, bool f>
216         friend class ParsedAttributeAttributor;
217         
218         template <class Function>
219         friend ParsedAttributeAttributor<
220             ParsedCommandOverload<typename detail::ParsedCommandTraits<Function>::traits> >
221         senf_console_add_node(DirectoryNode & node, std::string const & name, Function fn, int);
222         
223         template <class Owner, class Function>
224         friend ParsedAttributeAttributor<
225             ParsedCommandOverload<typename detail::ParsedCommandTraits<Function>::traits> >
226         senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
227                               Function fn, int,
228                               typename boost::enable_if_c<
229                                   detail::ParsedCommandTraits<Function>::is_member>::type * = 0);
230     };
231
232 #ifndef DOXYGEN
233
234     template <class Function>
235     ParsedAttributeAttributor<
236         ParsedCommandOverload<typename detail::ParsedCommandTraits<Function>::traits> >
237     senf_console_add_node(DirectoryNode & node, std::string const & name, Function fn, int);
238
239     template <class Owner, class Function>
240     ParsedAttributeAttributor<
241         ParsedCommandOverload<typename detail::ParsedCommandTraits<Function>::traits> >
242     senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
243                           Function fn, int,
244                           typename boost::enable_if_c<
245                               detail::ParsedCommandTraits<Function>::is_member>::type * = 0);
246
247 #endif
248
249 }}
250
251 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
252
253 BOOST_TYPEOF_REGISTER_TEMPLATE(senf::console::ParsedCommandOverload, (class,unsigned))
254 BOOST_TYPEOF_REGISTER_TEMPLATE(senf::console::ParsedAttributeAttributor, (class, unsigned, bool))
255 BOOST_TYPEOF_REGISTER_TEMPLATE(boost::function_traits, 1)
256
257 ///////////////////////////////hh.e////////////////////////////////////////
258 #include "ParsedCommand.cci"
259 #include "ParsedCommand.ct"
260 #include "ParsedCommand.cti"
261 #endif
262
263 \f
264 // Local Variables:
265 // mode: c++
266 // fill-column: 100
267 // comment-column: 40
268 // c-file-style: "senf"
269 // indent-tabs-mode: nil
270 // ispell-local-dictionary: "american"
271 // compile-command: "scons -u test"
272 // End: