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