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