NEST  2.6.0,not_revisioned_source_dir@0
tokenutils.h
Go to the documentation of this file.
1 /*
2  * tokenutils.h
3  *
4  * This file is part of NEST.
5  *
6  * Copyright (C) 2004 The NEST Initiative
7  *
8  * NEST 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  * NEST 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 NEST. If not, see <http://www.gnu.org/licenses/>.
20  *
21  */
22 
23 #ifndef TOKENUTILS_H
24 #define TOKENUTILS_H
25 
26 #include "token.h"
27 #include "sliexceptions.h"
28 #include "namedatum.h"
29 #include <string>
30 #include "config.h"
31 
147 template <typename FT>
148 FT getValue(const Token &t)
149 {
150  FT *value = dynamic_cast<FT *>(t.datum());
151  if (value == NULL)
152  {
153  throw TypeMismatch();
154  }
155  return *value;
156 }
157 
171 template <typename FT>
172 void setValue(const Token &t, FT const &value)
173 {
174  FT *old = dynamic_cast<FT *>(t.datum());
175  if (old == NULL)
176  {
177  throw TypeMismatch();
178  }
179 
180  *old = value;
181 }
182 
187 template <typename FT, class D>
188 Token newToken2(FT const &value)
189 {
190  Token t(new D(value));
191 
192  // this is for typechecking reasons:
193  getValue<FT>(t);
194 
195  return t;
196 }
197 
198 
204 template <typename FT>
205 Token newToken(FT const &value)
206 {
207  return newToken2<FT,NameDatum>(value);
208 }
209 
210 // specializations below this line: -----------------------
211 template<>
212 long getValue<long>(const Token&);
213 template<>
214 void setValue<long>(const Token&, long const &value);
215 
216 template<>
217 Token newToken<long>(long const &value);
218 
219 template<>
220 double getValue<double>(const Token&);
221 
222 template<>
223 void setValue<double>(const Token&, double const &value);
224 
225 template<>
226 float getValue<float>(const Token&);
227 
228 template<>
229 void setValue<float>(const Token&, float const &value);
230 
231 
232 template<>
233 Token newToken<double>(double const &value);
234 
235 template<>
236 bool getValue<bool>(const Token&);
237 template<>
238 void setValue<bool>(const Token&, bool const &value);
239 
240 template<>
241 Token newToken<bool>(bool const &value);
242 
243 
244 // These will handle StringDatum, NameDatum,
245 // LiteralDatum and SymbolDatum tokens:
246 template<>
247 std::string getValue<std::string>(const Token&);
248 template<>
249 void setValue<std::string>(const Token&, std::string const &value);
250 
251 
252 template<>
253 Token newToken<std::string>(std::string const &value);
254 
255 
256 // To get NameDatum, LiteralDatum or SymbolDatum tokens from a string,
257 // use newToken2<FT,D> instead (e.g. newToken2<string,LiteralDatum>);
258 
259 
260 // These will convert homogeneous int arrays to vectors:
261 template<>
262 std::vector<long> getValue<std::vector<long> >(const Token&);
263 template<>
264 void setValue<std::vector<long> >(const Token&, std::vector<long> const &value);
265 
266 template<>
267 Token newToken<std::vector<long> >(std::vector<long> const &value);
268 
269 
270 // These will convert homogeneous double arrays to vectors:
271 template<>
272 std::vector<double> getValue<std::vector<double> >(const Token&);
273 template<>
274 void setValue<std::vector<double> >(const Token&, std::vector<double> const &value);
275 
276 template<>
277 Token newToken<std::vector<double> >(std::vector<double> const &value);
278 
279 
280 
281 // These will convert homogeneous int arrays to valarrays:
282 template<>
283 std::valarray<long> getValue<std::valarray<long> >(const Token&);
284 template<>
285 void setValue<std::valarray<long> >(const Token&, std::valarray<long> const &value);
286 
287 template<>
288 Token newToken<std::valarray<long> >(std::valarray<long> const &value);
289 
290 
291 // These will convert homogeneous double arrays to valarrays:
292 template<>
293 std::valarray<double> getValue<std::valarray<double> >(const Token&);
294 template<>
295 void setValue<std::valarray<double> >(const Token&, std::valarray<double> const &value);
296 
297 template<>
298 Token newToken<std::valarray<double> >(std::valarray<double> const &value);
299 
300 
301 
302 
303 #endif
bool getValue< bool >(const Token &)
Definition: tokenutils.cc:124
float getValue< float >(const Token &)
Definition: tokenutils.cc:92
void setValue(const Token &t, FT const &value)
setValue provides easy write-access to a Token's contents.
Definition: tokenutils.h:172
Exception to be thrown if a given SLI type does not match the expected type.
Definition: sliexceptions.h:147
void setValue< double >(const Token &, double const &value)
Definition: tokenutils.cc:80
void setValue< long >(const Token &, long const &value)
Definition: tokenutils.cc:48
Token newToken(FT const &value)
Create a new Token from a fundamental data type.
Definition: tokenutils.h:205
FT getValue(const Token &t)
getValue provides easy read-access to a Token's contents.
Definition: tokenutils.h:148
Token newToken< long >(long const &value)
Definition: tokenutils.cc:61
void setValue< float >(const Token &, float const &value)
Definition: tokenutils.cc:105
long getValue< long >(const Token &)
Definition: tokenutils.cc:36
void setValue< bool >(const Token &, bool const &value)
Definition: tokenutils.cc:137
Token newToken< bool >(bool const &value)
Definition: tokenutils.cc:152
Token newToken< double >(double const &value)
Definition: tokenutils.cc:118
double getValue< double >(const Token &)
Definition: tokenutils.cc:68
A type-independent container for C++-types.
Definition: token.h:68
Token newToken2(FT const &value)
Create a new Token from a fundamental data type.
Definition: tokenutils.h:188
Datum * datum(void) const
Definition: token.h:249