replaced some tabs with spaces
[senf.git] / Scheduler / 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 ///////////////////////////////ih.p////////////////////////////////////////
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         
68         ArgumentInfoBase(std::string const & type);
69         virtual ~ArgumentInfoBase();
70
71         virtual std::string defaultValueStr() const = 0;
72     };
73     
74     /** \brief Internal: Argument information structure
75         
76         This class is used to hold argument information for automatically parsed commands. 
77
78         \see ParsedCommandOverloadBase
79      */
80     template <class ParameterType>
81     struct ArgumentInfo 
82         : public ArgumentInfoBase
83     {
84         typedef boost::intrusive_ptr<ArgumentInfo> ptr;
85         typedef boost::function<void (ParseCommandInfo::TokensRange const &, 
86                                       ParameterType &)> Parser;
87
88         static ptr create();
89         ArgumentInfo();
90
91         ParameterType defaultValue;
92         Parser parser;
93
94         virtual std::string defaultValueStr() const;
95     };
96     
97 #ifndef DOXYGEN
98
99     // FirstArgType returns void, if the function has no arguments, otherwise it returns arg1_type
100
101     template <class Traits, bool flag=(Traits::arity>0)>
102     struct FirstArgType
103     {
104         typedef void type;
105     };
106
107     template <class Traits>
108     struct FirstArgType<Traits,true>
109     {
110         typedef typename Traits::arg1_type type;
111     };
112
113     template <class FnunctionP, class Function, bool isFN=boost::is_function<Function>::value>
114     struct ParsedCommandTraits_i
115     {
116         static const bool is_callable = false;
117         static const bool is_member = false;
118     };
119
120     template <class FunctionP, class Function>
121     struct ParsedCommandTraits_i<FunctionP, Function, true>
122     {
123         typedef FunctionP base_type;
124         typedef typename senf::remove_any_pointer<base_type>::type function_type;
125         typedef boost::function_traits<function_type> base_traits;
126         typedef typename FirstArgType<base_traits>::type first_arg_type;
127
128         static const bool has_ostream_arg = boost::is_same<first_arg_type, std::ostream &>::value;
129
130         typedef typename boost::mpl::if_c<
131             has_ostream_arg, 
132             typename function_traits_remove_arg<base_traits>::type, 
133             base_traits>
134         ::type traits;
135
136         typedef typename senf::remove_cvref<typename base_traits::result_type>::type result_type;
137
138         static const bool is_callable = true;
139         static const bool is_member = boost::is_member_pointer<base_type>::value;
140         
141         typedef typename senf::member_class<base_type>::type class_type;
142
143         typedef ParsedCommandOverload<traits> Overload;
144         typedef ParsedArgumentAttributor<Overload> Attributor;
145     };
146
147     // Disable auto-parsing for ParseCommandInfo arg -> register manually parsed command
148     template <class FunctionP>
149     struct ParsedCommandTraits_i<FunctionP, void (std::ostream &, ParseCommandInfo const &), true>
150     {};
151
152     template <class FunctionP>
153     struct ParsedCommandTraits
154         : public ParsedCommandTraits_i< FunctionP, 
155                                         typename senf::remove_any_pointer<FunctionP>::type >
156     {};
157
158     struct ParsedCommandAddNodeAccess;
159
160 #endif
161
162 }}}
163
164 ///////////////////////////////ih.e////////////////////////////////////////
165 #endif
166
167 \f
168 // Local Variables:
169 // mode: c++
170 // fill-column: 100
171 // comment-column: 40
172 // c-file-style: "senf"
173 // indent-tabs-mode: nil
174 // ispell-local-dictionary: "american"
175 // compile-command: "scons -u test"
176 // End: