Utils: Add unit-test and documentation for type_traits.hh
[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 "../config.hh"
37 #include "OverloadedCommand.hh"
38 #include "ParseParameter.hh"
39 #include "../Utils/type_traits.hh"
40
41 #include "ParsedCommand.ih"
42 #include "ParsedCommand.mpp"
43 ///////////////////////////////hh.p////////////////////////////////////////
44
45 namespace senf {
46 namespace console {
47
48     class ParsedCommandOverloadBase
49         : public CommandOverload
50     {
51     public:
52         typedef boost::intrusive_ptr<ParsedCommandOverloadBase> ptr;
53
54         detail::ParameterInfoBase & arg(unsigned n) const;
55         template <class Type> detail::ParameterInfo<Type> & arg(unsigned n) const;
56
57         void doc(std::string const & d);
58
59     protected:
60         ParsedCommandOverloadBase();
61
62         template <class Type>
63         void addParameter();
64
65     private:
66         virtual unsigned v_numArguments() const;
67         virtual void v_argumentDoc(unsigned index, ArgumentDoc & doc) const;
68         virtual std::string v_doc() const;
69
70         typedef std::vector<detail::ParameterInfoBase::ptr> Parameters;
71         Parameters parameters_;
72         std::string doc_;
73     };
74
75     template <class FunctionTraits, unsigned arity=FunctionTraits::arity>
76     class ParsedCommandOverload {};
77
78 #   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                     \
79                                             SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
80                                             1))
81 #   include BOOST_PP_ITERATE()
82
83     class ParsedCommandAttributorBase
84     {
85     public:
86         OverloadedCommandNode & node() const;
87         operator OverloadedCommandNode & () const;
88
89     protected:
90         ParsedCommandAttributorBase(ParsedCommandOverloadBase & overload, unsigned index);
91
92         void argName(std::string const & name) const;
93         void argDoc(std::string const & doc) const;
94         template <class Type> void defaultValue(Type const & value) const;
95
96         ParsedCommandOverloadBase & overload() const;
97         void overloadDoc(std::string const & doc) const;
98
99         void nodeDoc(std::string const & doc) const;
100         
101     private:
102         ParsedCommandOverloadBase & overload_;
103         unsigned index_;
104     };
105
106     template <class Overload>
107     class ParsedCommandAttributor
108         : public ParsedCommandAttributorBase
109     {
110     public:
111         Overload & overload() const;
112
113     protected:
114         ParsedCommandAttributor(Overload & overload, unsigned index);
115
116     private:
117     };
118
119     template <class Overload, class Self>
120     class ParsedAttributeAttributorBase
121         : public ParsedCommandAttributor<Overload>
122     {
123     public:
124         Self doc(std::string const & doc) const;
125         Self overloadDoc(std::string const & doc) const;
126
127     protected:
128         ParsedAttributeAttributorBase(Overload & overload, unsigned index);
129
130     private:
131     };
132
133     template <class Overload, unsigned index=0, bool flag=(index < unsigned(Overload::traits::arity))>
134     class ParsedAttributeAttributor
135         : public ParsedAttributeAttributorBase< Overload, 
136                                                 ParsedAttributeAttributor<Overload, index, flag> >
137     {
138     public:
139         typedef typename senf::function_traits_arg_type< 
140             typename Overload::traits, int(index) >::type arg_type;
141         typedef typename senf::remove_cvref< arg_type >::type value_type;
142
143         typedef OverloadedCommandNode node_type;
144         typedef ParsedAttributeAttributor return_type;
145
146         ParsedAttributeAttributor<Overload, index+1> arg(std::string const & name = "",
147                                                          std::string const & doc = "") const;
148         ParsedAttributeAttributor<Overload, index+1> arg(std::string const & name,
149                                                          std::string const & doc,
150                                                          value_type const & value) const;
151
152     private:
153         explicit ParsedAttributeAttributor(Overload & overload);
154
155         ParsedAttributeAttributor<Overload, index+1> next() const;
156
157         void defaultValue(value_type const & value) const;
158
159         template <class O, unsigned i, bool f>
160         friend class ParsedAttributeAttributor;
161         
162         template <class Function>
163         friend ParsedAttributeAttributor<
164             ParsedCommandOverload<typename detail::ParsedCommandTraits<Function>::traits> >
165         senf_console_add_node(DirectoryNode & node, std::string const & name, Function fn, int);
166         
167         template <class Owner, class Function>
168         friend ParsedAttributeAttributor<
169             ParsedCommandOverload<typename detail::ParsedCommandTraits<Function>::traits> >
170         senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
171                               Function fn, int,
172                               typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type * = 0);
173     };
174
175     template <class Overload, unsigned index>
176     class ParsedAttributeAttributor<Overload, index, false>
177         : public ParsedAttributeAttributorBase< Overload, 
178                                                 ParsedAttributeAttributor<Overload, index, false> >
179     {
180     public:
181         typedef OverloadedCommandNode node_type;
182         typedef ParsedAttributeAttributor return_type;
183
184     private:
185         explicit ParsedAttributeAttributor(Overload & overload);
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<detail::ParsedCommandTraits<Function>::is_member>::type * = 0);
201     };
202
203 #ifndef DOXYGEN
204
205     template <class Function>
206     ParsedAttributeAttributor<
207         ParsedCommandOverload<typename detail::ParsedCommandTraits<Function>::traits> >
208     senf_console_add_node(DirectoryNode & node, std::string const & name, Function fn, int);
209
210     template <class Owner, class Function>
211     ParsedAttributeAttributor<
212         ParsedCommandOverload<typename detail::ParsedCommandTraits<Function>::traits> >
213     senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
214                           Function fn, int,
215                           typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type * = 0);
216
217 #endif
218
219 }}
220
221 #include BOOST_TYPEOF_INCREMENT_REGISTRATION_GROUP()
222
223 BOOST_TYPEOF_REGISTER_TEMPLATE(senf::console::ParsedCommandOverload, (class,unsigned))
224 BOOST_TYPEOF_REGISTER_TEMPLATE(senf::console::ParsedAttributeAttributor, (class, unsigned, bool))
225 BOOST_TYPEOF_REGISTER_TEMPLATE(boost::function_traits, 1)
226
227 ///////////////////////////////hh.e////////////////////////////////////////
228 #include "ParsedCommand.cci"
229 #include "ParsedCommand.ct"
230 #include "ParsedCommand.cti"
231 #endif
232
233 \f
234 // Local Variables:
235 // mode: c++
236 // fill-column: 100
237 // comment-column: 40
238 // c-file-style: "senf"
239 // indent-tabs-mode: nil
240 // ispell-local-dictionary: "american"
241 // compile-command: "scons -u test"
242 // End: