Console: Remove senf_console_add_node() overloads from documentation
[senf.git] / Console / Variables.hh
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 Variables public header */
25
26 #ifndef HH_Variables_
27 #define HH_Variables_ 1
28
29 // Custom includes
30 #include <boost/utility.hpp>
31 #include <boost/type_traits/is_convertible.hpp>
32 #include <boost/ref.hpp>
33 #include "ParsedCommand.hh"
34
35 #include "Variables.ih"
36 //#include "Variables.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
38
39 namespace senf {
40 namespace console {
41
42     class ScopedDirectoryBase;
43     template <class Variable> class VariableAttributor;
44
45 #ifndef DOXYGEN
46
47     template <class Variable>
48     VariableAttributor<Variable> senf_console_add_node(
49         DirectoryNode & node, std::string const & name, Variable & var, int,
50         typename boost::disable_if< boost::is_convertible<Variable*, ScopedDirectoryBase*> >::type * = 0);
51
52     template <class Variable>
53     typename detail::VariableNodeCreator<Variable>::result_type
54     senf_console_add_node(DirectoryNode & node, std::string const & name, 
55                           boost::reference_wrapper<Variable> var, int);
56
57 #endif
58
59     /** \brief Variable command attributes (const)
60         
61         \see VariableAttributor
62      */
63     template <class Variable>
64     class ConstVariableAttributor
65     {
66     public:
67         typedef typename detail::QueryVariable<Variable>::Traits::Overload QueryOverload;
68         typedef typename QueryOverload::Formatter Formatter;
69         typedef OverloadedCommandNode node_type;
70         typedef ConstVariableAttributor return_type;
71
72         ConstVariableAttributor doc(std::string const & doc);
73         ConstVariableAttributor formatter(Formatter formatter);
74
75     protected:
76         explicit ConstVariableAttributor(QueryOverload & queryOverload);
77
78     private:
79         QueryOverload & queryOverload_;
80
81         friend class detail::VariableNodeCreator<Variable const>;
82     };
83  
84     /** \brief Variable command attributes
85
86         Variable commands allow to register any arbitrary variable as a command node. The variable
87         will be registered as two command overloads: One which takes a single argument of the
88         variables type to set the variable and another one taking no arguments and just querying the
89         current variable value.
90         \code
91         int var;
92         ScopedDirectory<> dir;
93
94         dir.add("var", var);
95         \endcode
96
97         Variables should be registered only with a ScopedDirectory declared in the same scope
98         (e.g. as a class member for member variables). This ensures, that the variable node is
99         removed from the tree when the scope is destroyed.
100
101         Since a variable command is added as a combination of two ordinary overloads, it is possible
102         to register additional overloads with the same name before or after registering the
103         variable. 
104
105         It is also possible, to register a variable read-only. To achieve this, just wrap it with \c
106         boost::cref(). Such a variable cannot be changed only queried. Therefore, it does not have
107         the parser() and typeName() attributes.
108         \code
109         dir.add("const_var", boost::cref(var))
110         \endcode
111
112         \ingroup console_commands
113      */
114    template <class Variable>
115     class VariableAttributor
116         : public ConstVariableAttributor<Variable>
117     {
118     public:
119         typedef typename detail::SetVariable<Variable>::Traits::Overload SetOverload;
120         typedef typename detail::ArgumentInfo<typename SetOverload::arg1_type>::Parser Parser;
121         typedef typename detail::SetVariable<Variable>::OnChangeHandler OnChangeHandler;
122         typedef OverloadedCommandNode node_type;
123         typedef VariableAttributor return_type;
124
125         typedef typename ConstVariableAttributor<Variable>::Formatter Formatter;
126         typedef typename ConstVariableAttributor<Variable>::QueryOverload QueryOverload;
127
128         VariableAttributor doc(std::string const & doc); ///< Set documentation of the variable
129         VariableAttributor formatter(Formatter formatter); ///< Set formatter
130                                         /**< The \a formatter must be a callable with a signature
131                                              compatible with
132                                              \code
133                                              void formatter(Variable const & value, std::ostream & os);
134                                              \endcode
135                                              The \a formatter takes the return value of the call \a
136                                              value and writes it properly formated to \a os. */
137        
138         VariableAttributor parser(Parser parser); ///< Set argument parser
139                                         /**< The parser is an arbitrary callable object with
140                                              the signature
141                                              \code
142                                                  void parser(senf::console::ParseCommandInfo::TokensRange const & tokens, value_type & out);
143                                              \endcode
144
145                                              where \c value_type is the type of the overload
146                                              parameter. The parser must read and parse the complete
147                                              \a tokens range and return the parsed value in \a
148                                              out. If the parser fails, it must raise a
149                                              senf::console::SyntaxErrorException. */
150         VariableAttributor typeName(std::string const & name); ///< Set name of the variable type
151         VariableAttributor onChange(OnChangeHandler handler); ///< Set change callback
152                                         /**< The \a handler callback is called, whenever the value
153                                              of the variable is changed. The new value has already
154                                              been set, when the callback is called, the old value is
155                                              passed to the callback. The callback must have a
156                                              signature compatible to
157                                              \code
158                                              void handler(Variable const & oldValue);
159                                              \endcode */
160  
161     protected:
162
163     private:
164         VariableAttributor(QueryOverload & queryOverload, SetOverload & setOverload, 
165                            Variable & var);
166
167         SetOverload & setOverload_;
168         Variable & var_;
169
170         friend class detail::VariableNodeCreator<Variable>;
171     };
172 }}
173
174 ///////////////////////////////hh.e////////////////////////////////////////
175 //#include "Variables.cci"
176 //#include "Variables.ct"
177 #include "Variables.cti"
178 #endif
179
180 \f
181 // Local Variables:
182 // mode: c++
183 // fill-column: 100
184 // comment-column: 40
185 // c-file-style: "senf"
186 // indent-tabs-mode: nil
187 // ispell-local-dictionary: "american"
188 // compile-command: "scons -u test"
189 // End: