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