d8a72d28ab5715739957ddca477e4aa97c8d3138
[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
31 ///////////////////////////////ih.p////////////////////////////////////////
32
33 namespace senf {
34 namespace console {
35 namespace detail {
36
37 #ifndef DOXYGEN
38
39     struct ArgumentInfoBase
40         : public intrusive_refcount
41     {
42         typedef boost::intrusive_ptr<ArgumentInfoBase> ptr;
43         
44         std::string type;
45         std::string name;
46         bool hasDefault;
47         std::string doc;
48         
49         ArgumentInfoBase(std::string const & type);
50
51         virtual std::string defaultValueStr() const = 0;
52     };
53     
54     template <class ParameterType>
55     struct ArgumentInfo 
56         : public ArgumentInfoBase
57     {
58         typedef boost::intrusive_ptr<ArgumentInfo> ptr;
59
60         static ptr create();
61         ArgumentInfo();
62
63         ParameterType defaultValue;
64
65         virtual std::string defaultValueStr() const;
66     };
67     
68     template <class Function, bool isFN=senf::is_any_function<Function>::value>
69     struct ParsedCommandTraits
70     {};
71
72     // FirstArgType returns void, if the function has no arguments, otherwise it returns arg1_type
73
74     template <class Traits, bool flag=(Traits::arity>0)>
75     struct FirstArgType
76     {
77         typedef void type;
78     };
79
80     template <class Traits>
81     struct FirstArgType<Traits,true>
82     {
83         typedef typename Traits::arg1_type type;
84     };
85
86     template <class Function>
87     struct ParsedCommandTraits<Function, true>
88     {
89         typedef Function base_type;
90         typedef typename senf::remove_any_pointer<base_type>::type function_type;
91         typedef boost::function_traits<function_type> base_traits;
92         typedef typename FirstArgType<base_traits>::type first_arg_type;
93
94         static const bool has_ostream_arg = boost::is_same<first_arg_type, std::ostream &>::value;
95
96         typedef typename boost::mpl::if_c<
97             has_ostream_arg, 
98             typename function_traits_remove_arg<base_traits>::type, 
99             base_traits>
100         ::type traits;
101
102         static const bool is_member = boost::is_member_pointer<base_type>::value;
103         
104         typedef typename senf::member_class<base_type>::type class_type;
105     };
106
107     template <class Type>
108     struct CheckVoidReturn
109     {
110         template <class Fn>
111         static void call(Fn fn, std::ostream & os);
112     };
113
114     template <>
115     struct CheckVoidReturn<void>
116     {
117         template <class Fn>
118         static void call(Fn fn, std::ostream & os);
119     };
120
121 #endif
122
123 }}}
124
125 ///////////////////////////////ih.e////////////////////////////////////////
126 #endif
127
128 \f
129 // Local Variables:
130 // mode: c++
131 // fill-column: 100
132 // comment-column: 40
133 // c-file-style: "senf"
134 // indent-tabs-mode: nil
135 // ispell-local-dictionary: "american"
136 // compile-command: "scons -u test"
137 // End: