c2d887baa58bf4ee8d1c8c3ac5295353171bd74a
[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 #ifndef DOXYGEN
52
53     struct ArgumentInfoBase
54         : public intrusive_refcount
55     {
56         typedef boost::intrusive_ptr<ArgumentInfoBase> ptr;
57         
58         std::string type;
59         std::string name;
60         std::string defaultDoc;
61         bool hasDefault;
62         std::string doc;
63         
64         ArgumentInfoBase(std::string const & type);
65
66         virtual std::string defaultValueStr() const = 0;
67     };
68     
69     template <class ParameterType>
70     struct ArgumentInfo 
71         : public ArgumentInfoBase
72     {
73         typedef boost::intrusive_ptr<ArgumentInfo> ptr;
74         typedef boost::function<void (ParseCommandInfo::TokensRange const &, 
75                                       ParameterType &)> Parser;
76
77         static ptr create();
78         ArgumentInfo();
79
80         ParameterType defaultValue;
81         Parser parser;
82
83         virtual std::string defaultValueStr() const;
84     };
85     
86     // FirstArgType returns void, if the function has no arguments, otherwise it returns arg1_type
87
88     template <class Traits, bool flag=(Traits::arity>0)>
89     struct FirstArgType
90     {
91         typedef void type;
92     };
93
94     template <class Traits>
95     struct FirstArgType<Traits,true>
96     {
97         typedef typename Traits::arg1_type type;
98     };
99
100     template <class Function, bool isFN=senf::is_any_function<Function>::value>
101     struct ParsedCommandTraits
102     {};
103
104     template <class Fn>
105     struct ParsedCommandTraits<Fn, true>
106     {
107         typedef Fn base_type;
108         typedef typename senf::remove_any_pointer<base_type>::type function_type;
109         typedef boost::function_traits<function_type> base_traits;
110         typedef typename FirstArgType<base_traits>::type first_arg_type;
111
112         static const bool has_ostream_arg = boost::is_same<first_arg_type, std::ostream &>::value;
113
114         typedef typename boost::mpl::if_c<
115             has_ostream_arg, 
116             typename function_traits_remove_arg<base_traits>::type, 
117             base_traits>
118         ::type traits;
119
120         typedef typename senf::remove_cvref<typename base_traits::result_type>::type result_type;
121
122         static const bool is_member = boost::is_member_pointer<base_type>::value;
123         
124         typedef typename senf::member_class<base_type>::type class_type;
125
126         typedef ParsedCommandOverload<traits> Overload;
127         typedef ParsedArgumentAttributor<Overload> Attributor;
128     };
129
130     struct ParsedCommandAddNodeAccess;
131
132 #endif
133
134 }}}
135
136 ///////////////////////////////ih.e////////////////////////////////////////
137 #endif
138
139 \f
140 // Local Variables:
141 // mode: c++
142 // fill-column: 100
143 // comment-column: 40
144 // c-file-style: "senf"
145 // indent-tabs-mode: nil
146 // ispell-local-dictionary: "american"
147 // compile-command: "scons -u test"
148 // End: