f074c5f3d1156dab3abc7c8a8e86f1fde4d3ec90
[senf.git] / 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     : var_ (var)
54 {}
55
56 template <class Variable>
57 prefix_ void senf::console::detail::SetVariable<Variable>::operator()(Variable const & value)
58     const
59 {
60     if (handler_) {
61         Variable old (var_);
62         var_ = value;
63         handler_(old);
64     }
65     else
66         var_ = value;
67 }
68
69 template <class Variable>
70 prefix_ void senf::console::detail::SetVariable<Variable>::onChange(OnChangeHandler handler)
71 {
72     handler_ = handler;
73 }
74
75 ///////////////////////////////////////////////////////////////////////////
76 // senf::console::ConstVariableAttributor<Variable>
77
78 template <class Variable>
79 prefix_ senf::console::ConstVariableAttributor<Variable>
80 senf::console::ConstVariableAttributor<Variable>::doc(std::string const & doc)
81 {
82     queryOverload_.node().doc(doc);
83     return *this;
84 }
85
86 template <class Variable>
87 prefix_ senf::console::ConstVariableAttributor<Variable>
88 senf::console::ConstVariableAttributor<Variable>::formatter(Formatter formatter)
89 {
90     queryOverload_.formatter(formatter);
91     return *this;
92 }
93
94 template <class Variable>
95 prefix_ senf::console::ConstVariableAttributor<Variable>::
96 ConstVariableAttributor(QueryOverload & queryOverload)
97     : queryOverload_ (queryOverload)
98 {}
99
100 ///////////////////////////////////////////////////////////////////////////
101 // senf::console::VariableAttributor<Variable>
102
103 template <class Variable>
104 prefix_ senf::console::VariableAttributor<Variable>
105 senf::console::VariableAttributor<Variable>::parser(Parser parser)
106 {
107     setOverload_.template arg<0>().parser = parser;
108     return *this;
109 }
110
111 template <class Variable>
112 prefix_ senf::console::VariableAttributor<Variable>
113 senf::console::VariableAttributor<Variable>::typeName(std::string const & name)
114 {
115     setOverload_.arg(0).type = name;
116     return *this;
117 }
118
119 template <class Variable>
120 prefix_ typename senf::console::VariableAttributor<Variable>
121 senf::console::VariableAttributor<Variable>::doc(std::string const & doc)
122 {
123     ConstVariableAttributor<Variable>::doc(doc);
124     return *this;
125 }
126
127 template <class Variable>
128 prefix_ typename senf::console::VariableAttributor<Variable>
129 senf::console::VariableAttributor<Variable>::formatter(Formatter formatter)
130 {
131     ConstVariableAttributor<Variable>::formatter(formatter);
132     return *this;
133 }
134
135 template <class Variable>
136 prefix_
137 senf::console::VariableAttributor<Variable>::VariableAttributor(QueryOverload & queryOverload,
138                                                                 SetOverload & setOverload)
139     : ConstVariableAttributor<Variable> (queryOverload), setOverload_ (setOverload)
140 {}
141
142 ///////////////////////////////////////////////////////////////////////////
143
144 template <class Variable, bool isConst>
145 prefix_ senf::console::VariableAttributor<Variable>
146 senf::console::detail::VariableNodeCreator<Variable,isConst>::add(DirectoryNode & node,
147                                                                   std::string const & name,
148                                                                   Variable & var)
149 {
150     typename VariableAttributor<Variable>::SetOverload & setOverload ( 
151         node.add(name, typename detail::SetVariable<Variable>::Function(
152                      detail::SetVariable<Variable>(var)))
153             .arg("new_value")
154             .overload() );
155     typename VariableAttributor<Variable>::QueryOverload & queryOverload ( 
156         node.add(name, typename detail::QueryVariable<Variable>::Function(
157                      detail::QueryVariable<Variable>(var))).overload() );
158
159     return VariableAttributor<Variable>(queryOverload, setOverload);
160 }
161
162 template <class Variable>
163 prefix_ senf::console::ConstVariableAttributor<Variable>
164 senf::console::detail::VariableNodeCreator<Variable, true>::add(DirectoryNode & node,
165                                                                 std::string const & name,
166                                                                 Variable & var)
167 {
168     typename VariableAttributor<Variable>::QueryOverload & queryOverload ( 
169         node.add(name, typename detail::QueryVariable<Variable>::Function(
170                      detail::QueryVariable<Variable>(var))).overload() );
171
172     return ConstVariableAttributor<Variable>(queryOverload);
173 }
174
175 template <class Variable>
176 prefix_ senf::console::VariableAttributor<Variable> senf::console::
177 senf_console_add_node(DirectoryNode & node, std::string const & name, Variable & var, int,
178                       typename boost::disable_if< boost::is_convertible<Variable*, ScopedDirectoryBase*> >::type *)
179 {
180     return detail::VariableNodeCreator<Variable>::add(node, name, var);
181 }
182
183 template <class Variable>
184 prefix_ typename senf::console::detail::VariableNodeCreator<Variable>::result_type
185 senf::console::senf_console_add_node(DirectoryNode & node, std::string const & name,
186                                      boost::reference_wrapper<Variable> var, int)
187 {
188     return detail::VariableNodeCreator<Variable>::add(node, name, var.get());
189 }
190
191 ///////////////////////////////cti.e///////////////////////////////////////
192 #undef prefix_
193
194 \f
195 // Local Variables:
196 // mode: c++
197 // fill-column: 100
198 // comment-column: 40
199 // c-file-style: "senf"
200 // indent-tabs-mode: nil
201 // ispell-local-dictionary: "american"
202 // compile-command: "scons -u test"
203 // End: