4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Variables public header */
26 #ifndef HH_SENF_Scheduler_Console_Variables_
27 #define HH_SENF_Scheduler_Console_Variables_ 1
30 #include <boost/utility.hpp>
31 #include <boost/type_traits/is_convertible.hpp>
32 #include <boost/ref.hpp>
33 #include "ParsedCommand.hh"
35 #include "Variables.ih"
36 //#include "Variables.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
42 class ScopedDirectoryBase;
43 template <class Variable> class VariableAttributor;
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 typename boost::disable_if_c<detail::ParsedCommandTraits<Variable>::is_callable>::type * = 0);
53 template <class Variable>
54 typename detail::VariableNodeCreator<Variable>::result_type
55 senf_console_add_node(DirectoryNode & node, std::string const & name,
56 boost::reference_wrapper<Variable> var, int);
58 template <class Variable, class Owner>
59 VariableAttributor<Variable> senf_console_add_node(
60 DirectoryNode & node, Owner & owner, std::string const & name, Variable & var, int,
61 typename boost::disable_if< boost::is_convertible<Variable*, ScopedDirectoryBase*> >::type * = 0,
62 typename boost::disable_if_c<detail::ParsedCommandTraits<Variable>::is_callable>::type * = 0);
64 template <class Variable, class Owner>
65 typename detail::VariableNodeCreator<Variable>::result_type
66 senf_console_add_node(DirectoryNode & node, Owner & owner, std::string const & name,
67 boost::reference_wrapper<Variable> var, int);
71 /** \brief Variable command attributes (const)
73 \see VariableAttributor
75 template <class Variable>
76 class ConstVariableAttributor
79 typedef typename detail::QueryVariable<Variable>::Traits::Overload QueryOverload;
80 typedef typename QueryOverload::Formatter Formatter;
81 typedef OverloadedCommandNode node_type;
82 typedef ConstVariableAttributor return_type;
84 ConstVariableAttributor doc(std::string const & doc);
85 ConstVariableAttributor formatter(Formatter formatter);
87 OverloadedCommandNode & node() const; ///< Return the node object
88 operator OverloadedCommandNode & () const; ///< Automatically convert to node object
91 explicit ConstVariableAttributor(QueryOverload & queryOverload);
94 QueryOverload & queryOverload_;
96 friend class detail::VariableNodeCreator<Variable const>;
99 /** \brief Variable command attributes
101 Variable commands allow to register any arbitrary variable as a command node. The variable
102 will be registered as two command overloads: One which takes a single argument of the
103 variables type to set the variable and another one taking no arguments and just querying the
104 current variable value.
107 ScopedDirectory<> dir;
112 Variables should be registered only with a ScopedDirectory declared in the same scope
113 (e.g. as a class member for member variables). This ensures, that the variable node is
114 removed from the tree when the scope is destroyed.
116 Since a variable command is added as a combination of two ordinary overloads, it is possible
117 to register additional overloads with the same name before or after registering the
120 It is also possible, to register a variable read-only. To achieve this, just wrap it with \c
121 boost::cref(). Such a variable cannot be changed only queried. Therefore, it does not have
122 the parser() and typeName() attributes.
124 dir.add("const_var", boost::cref(var))
127 \ingroup console_commands
129 template <class Variable>
130 class VariableAttributor
131 : public ConstVariableAttributor<Variable>
134 typedef typename detail::SetVariable<Variable>::Traits::Overload SetOverload;
135 typedef typename detail::ArgumentInfo<typename SetOverload::arg1_type>::Parser Parser;
136 typedef typename detail::SetVariable<Variable>::OnChangeHandler OnChangeHandler;
137 typedef OverloadedCommandNode node_type;
138 typedef VariableAttributor return_type;
140 typedef typename ConstVariableAttributor<Variable>::Formatter Formatter;
141 typedef typename ConstVariableAttributor<Variable>::QueryOverload QueryOverload;
143 VariableAttributor doc(std::string const & doc); ///< Set documentation of the variable
144 VariableAttributor formatter(Formatter formatter); ///< Set formatter
145 /**< The \a formatter must be a callable with a signature
148 void formatter(Variable const & value, std::ostream & os);
150 The \a formatter takes the return value of the call \a
151 value and writes it properly formated to \a os. */
153 VariableAttributor parser(Parser parser); ///< Set argument parser
154 /**< The parser is an arbitrary callable object with
157 void parser(senf::console::ParseCommandInfo::TokensRange const & tokens, value_type & out);
160 where \c value_type is the type of the overload
161 parameter. The parser must read and parse the complete
162 \a tokens range and return the parsed value in \a
163 out. If the parser fails, it must raise a
164 senf::console::SyntaxErrorException. */
165 VariableAttributor typeName(std::string const & name); ///< Set name of the variable type
166 VariableAttributor onChange(OnChangeHandler handler); ///< Set change callback
167 /**< The \a handler callback is called, whenever the value
168 of the variable is changed. The new value has already
169 been set, when the callback is called, the old value is
170 passed to the callback. The callback must have a
171 signature compatible to
173 void handler(Variable const & oldValue);
179 VariableAttributor(QueryOverload & queryOverload, SetOverload & setOverload,
182 SetOverload & setOverload_;
185 friend class detail::VariableNodeCreator<Variable>;
189 ///////////////////////////////hh.e////////////////////////////////////////
190 //#include "Variables.cci"
191 //#include "Variables.ct"
192 #include "Variables.cti"
199 // comment-column: 40
200 // c-file-style: "senf"
201 // indent-tabs-mode: nil
202 // ispell-local-dictionary: "american"
203 // compile-command: "scons -u test"