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 /** \brief Variable command attributes (const)
51 template <class Variable>
52 class ConstVariableFactory
53 : public detail::NodeFactory
56 typedef typename detail::QueryVariable<Variable>::Traits::Overload QueryOverload;
57 typedef typename QueryOverload::Formatter Formatter;
58 typedef OverloadedCommandNode node_type;
59 typedef OverloadedCommandNode & result_type;
61 ConstVariableFactory doc(std::string const & doc);
62 ConstVariableFactory shortdoc(std::string const & doc);
63 ConstVariableFactory formatter(Formatter formatter);
65 OverloadedCommandNode & create(DirectoryNode & dir, std::string const & name) const;
67 explicit ConstVariableFactory(Variable const & var);
70 typename QueryOverload::ptr queryOverload_;
71 boost::optional<std::string> doc_;
72 boost::optional<std::string> shortdoc_;
75 /** \brief Variable command attributes
77 Variable commands allow to register any arbitrary variable as a command node. The variable
78 will be registered as two command overloads: One which takes a single argument of the
79 variables type to set the variable and another one taking no arguments and just querying the
80 current variable value.
83 ScopedDirectory<> dir;
88 Variables should be registered only with a ScopedDirectory declared in the same scope
89 (e.g. as a class member for member variables). This ensures, that the variable node is
90 removed from the tree when the scope is destroyed.
92 Since a variable command is added as a combination of two ordinary overloads, it is possible
93 to register additional overloads with the same name before or after registering the
96 It is also possible, to register a variable read-only. To achieve this, just wrap it with \c
97 boost::cref(). Such a variable cannot be changed only queried. Therefore, it does not have
98 the parser() and typeName() attributes.
100 dir.add("const_var", boost::cref(var))
103 \ingroup console_commands
105 template <class Variable>
106 class VariableFactory
107 : public ConstVariableFactory<Variable>
110 typedef typename detail::SetVariable<Variable>::Traits::Overload SetOverload;
111 typedef typename detail::ArgumentInfo<typename SetOverload::arg1_type>::Parser Parser;
112 typedef typename detail::SetVariable<Variable>::OnChangeHandler OnChangeHandler;
114 typedef typename ConstVariableFactory<Variable>::Formatter Formatter;
115 typedef typename ConstVariableFactory<Variable>::QueryOverload QueryOverload;
117 VariableFactory doc(std::string const & doc); ///< Set documentation of the variable
118 VariableFactory shortdoc(std::string const & doc); ///< Set short documentation
119 VariableFactory formatter(Formatter formatter); ///< Set formatter
120 /**< The \a formatter must be a callable with a signature
123 void formatter(Variable const & value, std::ostream & os);
125 The \a formatter takes the return value of the call \a
126 value and writes it properly formated to \a os. */
127 VariableFactory parser(Parser parser); ///< Set argument parser
128 /**< The parser is an arbitrary callable object with
131 void parser(senf::console::ParseCommandInfo::TokensRange const & tokens, value_type & out);
134 where \c value_type is the type of the overload
135 parameter. The parser must read and parse the complete
136 \a tokens range and return the parsed value in \a
137 out. If the parser fails, it must raise a
138 senf::console::SyntaxErrorException. */
139 VariableFactory typeName(std::string const & name); ///< Set name of the variable type
140 VariableFactory onChange(OnChangeHandler handler); ///< Set change callback
141 /**< The \a handler callback is called, whenever the value
142 of the variable is changed. The new value has already
143 been set, when the callback is called, the old value is
144 passed to the callback. The callback must have a
145 signature compatible to
147 void handler(Variable const & oldValue);
150 OverloadedCommandNode & create(DirectoryNode & dir, std::string const & name) const;
152 explicit VariableFactory(Variable & var);
157 typename SetOverload::ptr setOverload_;
162 VariableFactory<Var> Variable(Var & var);
165 VariableFactory<Var> Variable(boost::reference_wrapper<Var> var);
168 ConstVariableFactory<Var> Variable(Var const & var);
171 ConstVariableFactory<Var> Variable(boost::reference_wrapper<Var const> var);
175 ///////////////////////////////hh.e////////////////////////////////////////
176 //#include "Variables.cci"
177 //#include "Variables.ct"
178 #include "Variables.cti"
185 // comment-column: 40
186 // c-file-style: "senf"
187 // indent-tabs-mode: nil
188 // ispell-local-dictionary: "american"
189 // compile-command: "scons -u test"