Move Console from Scheduler into Utils
[senf.git] / Utils / Console / Variables.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 Variables inline template implementation */
25
26 #include "Variables.ih"
27
28 // Custom includes
29
30 #define prefix_ inline
31 ///////////////////////////////cti.p///////////////////////////////////////
32
33 ///////////////////////////////////////////////////////////////////////////
34 // senf::console::detail::QueryVariable<Variable>
35
36 template <class Variable>
37 prefix_ senf::console::detail::QueryVariable<Variable>::QueryVariable(Variable const & var)
38     : var_ (var)
39 {}
40
41 template <class Variable>
42 prefix_ Variable const & senf::console::detail::QueryVariable<Variable>::operator()()
43     const
44 {
45     return var_;
46 }
47
48 ///////////////////////////////////////////////////////////////////////////
49 // senf::console::detail::SetVariable<Variable>
50
51 template <class Variable>
52 prefix_ senf::console::detail::SetVariable<Variable>::SetVariable(Variable & var,
53                                                                   OnChangeHandler handler)
54     : var_ (var), handler_ (handler)
55 {}
56
57 template <class Variable>
58 prefix_ void senf::console::detail::SetVariable<Variable>::operator()(Variable const & value)
59     const
60 {
61     if (handler_) {
62         Variable old (var_);
63         var_ = value;
64         handler_(old);
65     }
66     else
67         var_ = value;
68 }
69
70 ///////////////////////////////////////////////////////////////////////////
71 // senf::console::ConstVariableAttributor<Variable>
72
73 template <class Variable>
74 prefix_ senf::console::ConstVariableAttributor<Variable>
75 senf::console::ConstVariableAttributor<Variable>::doc(std::string const & doc)
76 {
77     queryOverload_.node().doc(doc);
78     return *this;
79 }
80
81 template <class Variable>
82 prefix_ senf::console::ConstVariableAttributor<Variable>
83 senf::console::ConstVariableAttributor<Variable>::formatter(Formatter formatter)
84 {
85     queryOverload_.formatter(formatter);
86     return *this;
87 }
88
89 template <class Variable>
90 prefix_ senf::console::ConstVariableAttributor<Variable>::
91 ConstVariableAttributor(QueryOverload & queryOverload)
92     : queryOverload_ (queryOverload)
93 {}
94
95 template <class Variable>
96 prefix_ senf::console::OverloadedCommandNode &
97 senf::console::ConstVariableAttributor<Variable>::node()
98     const
99 {
100     return queryOverload_.node();
101 }
102
103 template <class Variable>
104 prefix_ senf::console::ConstVariableAttributor<Variable>::
105 operator senf::console::OverloadedCommandNode &()
106     const
107 {
108     return node();
109 }
110
111 ///////////////////////////////////////////////////////////////////////////
112 // senf::console::VariableAttributor<Variable>
113
114 template <class Variable>
115 prefix_ senf::console::VariableAttributor<Variable>
116 senf::console::VariableAttributor<Variable>::parser(Parser parser)
117 {
118     setOverload_.template arg<0>().parser = parser;
119     return *this;
120 }
121
122 template <class Variable>
123 prefix_ senf::console::VariableAttributor<Variable>
124 senf::console::VariableAttributor<Variable>::typeName(std::string const & name)
125 {
126     setOverload_.arg(0).type = name;
127     return *this;
128 }
129
130 template <class Variable>
131 prefix_ typename senf::console::VariableAttributor<Variable>
132 senf::console::VariableAttributor<Variable>::onChange(OnChangeHandler handler)
133 {
134     setOverload_.function( 
135         boost::bind(detail::SetVariable<Variable>(var_, handler),_2));
136     return *this;
137 }
138
139 template <class Variable>
140 prefix_ typename senf::console::VariableAttributor<Variable>
141 senf::console::VariableAttributor<Variable>::doc(std::string const & doc)
142 {
143     ConstVariableAttributor<Variable>::doc(doc);
144     return *this;
145 }
146
147 template <class Variable>
148 prefix_ typename senf::console::VariableAttributor<Variable>
149 senf::console::VariableAttributor<Variable>::formatter(Formatter formatter)
150 {
151     ConstVariableAttributor<Variable>::formatter(formatter);
152     return *this;
153 }
154
155 template <class Variable>
156 prefix_
157 senf::console::VariableAttributor<Variable>::VariableAttributor(QueryOverload & queryOverload,
158                                                                 SetOverload & setOverload,
159                                                                 Variable & var)
160     : ConstVariableAttributor<Variable> (queryOverload), setOverload_ (setOverload), var_ (var)
161 {}
162
163 ///////////////////////////////////////////////////////////////////////////
164
165 template <class Variable, bool isConst>
166 prefix_ senf::console::VariableAttributor<Variable>
167 senf::console::detail::VariableNodeCreator<Variable,isConst>::add(DirectoryNode & node,
168                                                                   std::string const & name,
169                                                                   Variable & var)
170 {
171     typename VariableAttributor<Variable>::SetOverload & setOverload ( 
172         node.add(name, typename detail::SetVariable<Variable>::Function(
173                      detail::SetVariable<Variable>(var)))
174             .arg("new_value")
175             .overload() );
176     typename VariableAttributor<Variable>::QueryOverload & queryOverload ( 
177         node.add(name, typename detail::QueryVariable<Variable>::Function(
178                      detail::QueryVariable<Variable>(var))).overload() );
179
180     return VariableAttributor<Variable>(queryOverload, setOverload, var);
181 }
182
183 template <class Variable>
184 prefix_ senf::console::ConstVariableAttributor<Variable>
185 senf::console::detail::VariableNodeCreator<Variable, true>::add(DirectoryNode & node,
186                                                                 std::string const & name,
187                                                                 Variable & var)
188 {
189     typename VariableAttributor<Variable>::QueryOverload & queryOverload ( 
190         node.add(name, typename detail::QueryVariable<Variable>::Function(
191                      detail::QueryVariable<Variable>(var))).overload() );
192
193     return ConstVariableAttributor<Variable>(queryOverload);
194 }
195
196 #ifndef DOXYGEN
197
198 template <class Variable>
199 prefix_ senf::console::VariableAttributor<Variable> senf::console::
200 senf_console_add_node(DirectoryNode & node, std::string const & name, Variable & var, int,
201                       typename boost::disable_if< boost::is_convertible<Variable*, ScopedDirectoryBase*> >::type *,
202                       typename boost::disable_if_c<detail::ParsedCommandTraits<Variable>::is_callable>::type *)
203 {
204     return detail::VariableNodeCreator<Variable>::add(node, name, var);
205 }
206
207 template <class Variable>
208 prefix_ typename senf::console::detail::VariableNodeCreator<Variable>::result_type
209 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
210                                      boost::reference_wrapper<Variable> var, int)
211 {
212     return detail::VariableNodeCreator<Variable>::add(node, name, var.get());
213 }
214
215 template <class Variable, class Owner>
216 prefix_ senf::console::VariableAttributor<Variable> senf::console::
217 senf_console_add_node(DirectoryNode & node, Owner &, std::string const & name,
218                       Variable & var, int,
219                       typename boost::disable_if< boost::is_convertible<Variable*, ScopedDirectoryBase*> >::type *,
220                       typename boost::disable_if_c<detail::ParsedCommandTraits<Variable>::is_callable>::type *)
221 {
222     return detail::VariableNodeCreator<Variable>::add(node, name, var);
223 }
224
225 template <class Variable, class Owner>
226 prefix_ typename senf::console::detail::VariableNodeCreator<Variable>::result_type
227 senf::console::senf_console_add_node(DirectoryNode & node, Owner &,
228                                      std::string const & name,
229                                      boost::reference_wrapper<Variable> var, int)
230 {
231     return detail::VariableNodeCreator<Variable>::add(node, name, var.get());
232 }
233
234 #endif
235
236 ///////////////////////////////cti.e///////////////////////////////////////
237 #undef prefix_
238
239 \f
240 // Local Variables:
241 // mode: c++
242 // fill-column: 100
243 // comment-column: 40
244 // c-file-style: "senf"
245 // indent-tabs-mode: nil
246 // ispell-local-dictionary: "american"
247 // compile-command: "scons -u test"
248 // End: