Console: Add member function argument parsing
[senf.git] / Console / ParsedCommand.cti
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 inline template implementation */
25
26 #include "ParsedCommand.ih"
27
28 // Custom includes
29 #include "../Utils/membind.hh"
30
31 #define prefix_ inline
32 ///////////////////////////////cti.p///////////////////////////////////////
33
34 ///////////////////////////////////////////////////////////////////////////
35 // senf::console::ParsedCommandOverloadBase
36
37 template <class Type>
38 prefix_ senf::console::detail::ParameterInfo<Type> &
39 senf::console::ParsedCommandOverloadBase::arg(unsigned n)
40     const
41 {
42     return static_cast<detail::ParameterInfo<Type> &>(arg(n));
43 }
44
45 template <class Type>
46 prefix_ void senf::console::ParsedCommandOverloadBase::addParameter()
47 {
48     parameters_.push_back(detail::ParameterInfo<Type>::create());
49 }
50
51 ///////////////////////////////////////////////////////////////////////////
52 // senf::console::ParsedCommandOverload<FunctionTraits,n>
53
54 #define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                       \
55                                          SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp),   \
56                                          2))
57 #include BOOST_PP_ITERATE()
58
59 ///////////////////////////////////////////////////////////////////////////
60 // namespace members
61
62 namespace {
63
64     // What is THIS about ??
65
66     // Ok, here's the dope: parsed commands may optionally have an std::ostream & first argument. If
67     // this argument is given, then the function will be called with the console output stream as
68     // it's first argument.
69     //
70     // This is implemented in the following way: ParsedCommandOverload (the class responsible for
71     // calling the callback) will ALWAYS pass the stream as first argument. If the user callback
72     // expects os as it's first argument, 'ignoreOneArg' will be false and the user supplied
73     // function will be directly passed to ParsedCommandOverload.
74     //
75     // If however, it does NOT take an std::ostream first argument, 'ignoreOneArg' will be true and
76     // the create member will use boost::bind to DROP the first argument.
77     
78     template <class Traits, bool ignoreOneArg, unsigned arity=Traits::arity>
79     struct CreateParsedCommandOverload
80     {};
81
82     template <class Traits, unsigned arity>
83     struct CreateParsedCommandOverload<Traits, false, arity>
84     {
85         typedef Traits traits;
86         
87         template <class Function>
88         static typename senf::console::ParsedCommandOverload<traits>::ptr create(Function fn) 
89             { return senf::console::ParsedCommandOverload<traits>::create(fn); };
90     };
91
92 #   define BOOST_PP_ITERATION_PARAMS_1 (4, (0, SENF_CONSOLE_MAX_COMMAND_ARITY,                     \
93                                             SENF_ABSOLUTE_INCLUDE_PATH(Console/ParsedCommand.mpp), \
94                                             4))
95 #   include BOOST_PP_ITERATE()
96
97 }
98
99 template <class Function>
100 prefix_ senf::console::ParsedCommandOverload<
101     typename senf::console::detail::ParsedCommandTraits<Function>::traits> &
102 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
103                                      Function fn, int)
104 {
105     OverloadedCommandNode & cmdNode (
106         node.hasChild(name) 
107         ? dynamic_cast<OverloadedCommandNode &>(node(name))
108         : node.add(name, OverloadedCommandNode::create()) );
109
110     typedef senf::console::detail::ParsedCommandTraits<Function> CmdTraits;
111
112     return cmdNode.add( CreateParsedCommandOverload<
113                             typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(fn) );
114 }
115
116 template <class Owner, class Function>
117 prefix_ senf::console::ParsedCommandOverload<
118     typename senf::console::detail::ParsedCommandTraits<Function>::traits> &
119 senf::console::
120 senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
121                       Function fn, int,
122                       typename boost::enable_if_c<detail::ParsedCommandTraits<Function>::is_member>::type *)
123 {
124     OverloadedCommandNode & cmdNode (
125         node.hasChild(name) 
126         ? dynamic_cast<OverloadedCommandNode &>(node(name))
127         : node.add(name, OverloadedCommandNode::create()) );
128
129     typedef senf::console::detail::ParsedCommandTraits<Function> CmdTraits;
130
131     return cmdNode.add( CreateParsedCommandOverload<
132                             typename CmdTraits::traits, ! CmdTraits::has_ostream_arg>::create(
133                                 senf::membind(fn,&owner)) );
134 }
135
136 ///////////////////////////////cti.e///////////////////////////////////////
137 #undef prefix_
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: