Variables.cti
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 /** \file
15  \brief Variables inline template implementation */
16 
17 #include "Variables.ih"
18 
19 // Custom includes
20 
21 #define prefix_ inline
22 //-/////////////////////////////////////////////////////////////////////////////////////////////////
23 
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 // senf::console::detail::QueryVariable<Variable>
26 
27 template <class Variable>
28 prefix_ senf::console::detail::QueryVariable<Variable>::QueryVariable(Variable const & var)
29  : var_ (var)
30 {}
31 
32 template <class Variable>
33 prefix_ typename senf::console::detail::QueryVariable<Variable>::Traits::Overload::ptr
34 senf::console::detail::QueryVariable<Variable>::create(Variable const & var)
35 {
36  return CreateOverload::create(Function(QueryVariable(var)));
37 }
38 
39 template <class Variable>
40 prefix_ Variable const & senf::console::detail::QueryVariable<Variable>::operator()()
41  const
42 {
43  return var_;
44 }
45 
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47 // senf::console::detail::SetVariable<Variable>
48 
49 template <class Variable>
50 prefix_ senf::console::detail::SetVariable<Variable>::SetVariable(Variable & var,
51  OnChangeHandler handler)
52  : var_ (var), handler_ (handler)
53 {}
54 
55 template <class Variable>
56 prefix_ typename senf::console::detail::SetVariable<Variable>::Traits::Overload::ptr
57 senf::console::detail::SetVariable<Variable>::create(Variable & var)
58 {
59  typename Traits::Overload::ptr overload (CreateOverload::create(Function(SetVariable(var))));
60  overload->arg(0).name = "new_value";
61  return overload;
62 }
63 
64 template <class Variable>
65 prefix_ void senf::console::detail::SetVariable<Variable>::operator()(Variable const & value)
66  const
67 {
68  if (handler_) {
69  Variable old (var_);
70  var_ = value;
71  handler_(old);
72  }
73  else
74  var_ = value;
75 }
76 
77 //-/////////////////////////////////////////////////////////////////////////////////////////////////
78 // senf::console::ConstVariableFactory<Variable>
79 
80 template <class Variable>
81 prefix_ senf::console::factory::ConstVariableFactory<Variable>
82 senf::console::factory::ConstVariableFactory<Variable>::doc(std::string const & doc)
83 {
84  doc_ = doc;
85  return *this;
86 }
87 
88 template <class Variable>
89 prefix_ senf::console::factory::ConstVariableFactory<Variable>
90 senf::console::factory::ConstVariableFactory<Variable>::shortdoc(std::string const & doc)
91 {
92  shortdoc_ = doc;
93  return *this;
94 }
95 
96 template <class Variable>
97 prefix_ senf::console::factory::ConstVariableFactory<Variable>
98 senf::console::factory::ConstVariableFactory<Variable>::formatter(Formatter formatter)
99 {
100  queryOverload_->formatter(formatter);
101  return *this;
102 }
103 
104 template <class Variable>
105 prefix_ senf::console::OverloadedCommandNode &
106 senf::console::factory::ConstVariableFactory<Variable>::create(DirectoryNode & dir,
107  std::string const & name)
108  const
109 {
110  OverloadedCommandNode & node (OverloadedCommandNode::insertOverload(dir, name, queryOverload_));
111  if (doc_) node.doc(*doc_);
112  if (shortdoc_) node.shortdoc(*shortdoc_);
113  return node;
114 }
115 
116 template <class Variable>
117 prefix_ senf::console::factory::ConstVariableFactory<Variable>::
118 ConstVariableFactory(Variable const & var)
119  : queryOverload_ (detail::QueryVariable<Variable>::create(var))
120 {}
121 
122 //-/////////////////////////////////////////////////////////////////////////////////////////////////
123 // senf::console::VariableFactory<Variable>
124 
125 template <class Variable>
126 prefix_ typename senf::console::factory::VariableFactory<Variable>
127 senf::console::factory::VariableFactory<Variable>::parser(Parser parser)
128 {
129  setOverload_->template arg<0>().parser = parser;
130  return *this;
131 }
132 
133 template <class Variable>
134 prefix_ typename senf::console::factory::VariableFactory<Variable>
135 senf::console::factory::VariableFactory<Variable>::typeName(std::string const & name)
136 {
137  setOverload_->arg(0).type = name;
138  return *this;
139 }
140 
141 template <class Variable>
142 prefix_ typename senf::console::factory::VariableFactory<Variable>
143 senf::console::factory::VariableFactory<Variable>::onChange(OnChangeHandler handler)
144 {
145  setOverload_->function(
146  boost::bind(detail::SetVariable<Variable>(var_, handler),_2));
147  return *this;
148 }
149 
150 template <class Variable>
151 prefix_ typename senf::console::factory::VariableFactory<Variable>
152 senf::console::factory::VariableFactory<Variable>::doc(std::string const & doc)
153 {
154  ConstVariableFactory<Variable>::doc(doc);
155  return *this;
156 }
157 
158 template <class Variable>
159 prefix_ typename senf::console::factory::VariableFactory<Variable>
160 senf::console::factory::VariableFactory<Variable>::shortdoc(std::string const & doc)
161 {
162  ConstVariableFactory<Variable>::shortdoc(doc);
163  return *this;
164 }
165 
166 template <class Variable>
167 prefix_ typename senf::console::factory::VariableFactory<Variable>
168 senf::console::factory::VariableFactory<Variable>::formatter(Formatter formatter)
169 {
170  ConstVariableFactory<Variable>::formatter(formatter);
171  return *this;
172 }
173 
174 template <class Variable>
175 prefix_ senf::console::OverloadedCommandNode &
176 senf::console::factory::VariableFactory<Variable>::create(DirectoryNode & dir,
177  std::string const & name)
178  const
179 {
180  OverloadedCommandNode & node (ConstVariableFactory<Variable>::create(dir,name));
181  node.add(setOverload_);
182  return node;
183 }
184 
185 template <class Variable>
186 prefix_ senf::console::factory::VariableFactory<Variable>::VariableFactory(Variable & var)
187  : ConstVariableFactory<Variable> (var),
188  setOverload_ (detail::SetVariable<Variable>::create(var)),
189  var_ (var)
190 {}
191 
192 //-/////////////////////////////////////////////////////////////////////////////////////////////////
193 
194 template <class Var>
195 prefix_ senf::console::factory::VariableFactory<Var>
196 senf::console::factory::Variable(Var & var)
197 {
198  return VariableFactory<Var>(var);
199 }
200 
201 template <class Var>
202 prefix_ senf::console::factory::VariableFactory<Var>
203 senf::console::factory::Variable(boost::reference_wrapper<Var> var)
204 {
205  return VariableFactory<Var>(var);
206 }
207 
208 template <class Var>
209 prefix_ senf::console::factory::ConstVariableFactory<Var>
210 senf::console::factory::Variable(Var const & var)
211 {
212  return ConstVariableFactory<Var>(var);
213 }
214 
215 template <class Var>
216 prefix_ senf::console::factory::ConstVariableFactory<Var>
217 senf::console::factory::Variable(boost::reference_wrapper<Var const> var)
218 {
219  return ConstVariableFactory<Var>(var);
220 }
221 
222 //-/////////////////////////////////////////////////////////////////////////////////////////////////
223 #undef prefix_
224 
225 
226 // Local Variables:
227 // mode: c++
228 // fill-column: 100
229 // comment-column: 40
230 // c-file-style: "senf"
231 // indent-tabs-mode: nil
232 // ispell-local-dictionary: "american"
233 // compile-command: "scons -u test"
234 // End: