NEST  2.6.0,not_revisioned_source_dir@0
token.h
Go to the documentation of this file.
1 /*
2  * token.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 TOKEN_H
24 #define TOKEN_H
25 /*
26  token.h defines the base objects used by the SLI interpreter.
27 */
28 
29 #include <typeinfo>
30 #include <iostream>
31 #include <iomanip>
32 #include <string>
33 #include <vector>
34 #include <valarray>
35 #include "config.h"
36 #include "datum.h"
37 class Name;
38 class Token;
39 class TokenArray;
40 class TokenArrayObj;
41 
42 /***********************************************************/
43 /* Token */
44 /* --------- */
45 /* Token class for all Data Objects */
46 /*
47 
48 const Datum* p; makes p a pointer to a const. Any change to the
49  object p points to is prevented.
50 
51 Datum *const p; makes p a const pointer to a Datum. Any change to the
52  pointer is prevented.
53 
54  It is not necessary to declare the pointer itself const, because the
55  return value is copied anyway. Only if the return value can be used as
56  an lvalue do we need to protect it.
57 
58  A member function declared const does not change any member object
59  of the class, it can be called for const class objects.
60 
61 */
62 
63 /***********************************************************/
64 
68 class Token
69 {
70  friend class Datum;
71  friend class TokenArrayObj;
72 
73 private:
74  Datum *p;
75 
79  mutable bool accessed_;
80 
81 public:
82 
84  {
85  if(p)
86  p->removeReference();
87  p=0;
88  }
89 
90  Token(const Token& c_s)
91  :p(NULL)
92  {
93  if(c_s.p)
94  p=c_s.p->get_ptr();
95  }
96 
97 
98  Token(Datum *p_s = NULL)
99  :p(p_s)
100  {}
101 
102  Token(const Datum &d )
103  { p=d.clone();}
104 
105  Token(int);
106  Token(unsigned int);
107  Token(long);
108  Token(bool);
109  Token(unsigned long);
110  Token(double);
111  Token(const char*);
112  Token(std::string);
113  Token(const std::vector<double>&);
114  Token(const std::valarray<double>&);
115  Token(const std::vector<long>&);
116  Token(const std::vector<size_t>&);
117  Token(const std::ostream&);
118  Token(const std::istream&);
119  operator Datum*() const;
120  operator size_t() const;
121  operator long() const;
122  operator double() const;
123  operator float() const;
124  operator bool() const;
125  operator std::string() const;
126 // operator vector<double> const;
127 // operator vector<long> const;
128 
133  void detach()
134  {
135  if(p and p->numReferences() > 1)
136  {
137  p->removeReference();
138  p=p->clone();
139  }
140  }
141 
142  void move( Token &c)
143  {
144  if(p)
145  p->removeReference();
146  p=c.p;
147  c.p = NULL;
148  }
149 
150 
158  void init_move(Token &rhs)
159  {
160  p=rhs.p;
161  rhs.p=NULL;
162  }
163 
171  void init_by_copy(const Token &rhs)
172  {
173  p=rhs.p->get_ptr();
174  }
175 
184  void init_by_ref(const Token &rhs)
185  {
186  rhs.p->addReference();
187  p=rhs.p;
188  }
189 
197  {
198  p=rhs;
199  }
200 
201  void assign_by_ref(const Token &rhs)
202  {
203  // assert(rhs.p !=NULL);
204  if(p != rhs.p)
205  {
206  if(p)
207  p->removeReference();
208  p=rhs.p->get_ptr();
209  }
210  }
211 
213  {
214  assert(rhs != NULL);
215  rhs->addReference();
216  if(p)
217  p->removeReference();
218  p=rhs;
219  }
220 
221 
222  void swap(Token &c)
223  {
224  std::swap(p,c.p);
225  }
226 
227  void clear(void)
228  {
229  if(p)
230  p->removeReference();
231  p = NULL;
232  }
233 
234  bool contains(const Datum &d) const
235  {
236  return (p!=NULL) ? p->equals(&d) : false;
237  }
238 
239  bool empty(void) const
240  {
241  return p == NULL;
242  }
243 
244  bool operator!(void) const
245  {
246  return p == NULL;
247  }
248 
249  Datum* datum(void) const
250  {
251  accessed_ = true;
252  return p;
253  }
254 
255 
256 
257  bool valid() const
258  {
259  return ! empty();
260  }
261 
262  Datum* operator->() const
263  {
264  // assert(p!= NULL);
265  return p;
266  }
267 
268 
269  Datum& operator*() const
270  {
271  // assert(p != NULL);
272  return *p;
273  }
274 
275 
276  const std::type_info& type(void) const
277  {
278  return typeid(*p);
279  }
280 
281 
282  Token& operator=(const Token& c_s)
283  {
284  if(c_s.p == p)
285  return *this;
286 
287  if (c_s.p == NULL)
288  {
289  clear();
290  return *this;
291  }
292 
293  if(p)
294  p->removeReference();
295  p=c_s.p->get_ptr();
296 
297  return *this;
298  }
299 
301  {
302  if(p != p_s)
303  {
304  if(p)
305  p->removeReference();
306  p=p_s;
307  }
308 
309  return *this;
310  }
311 
312 
313  bool operator==(const Token &t) const
314  {
315  if(p == t.p)
316  return true;
317 
318  return p and p->equals(t.p);
319 
320  }
321 
322  // define != explicitly --- HEP 2001-08-09
323  bool operator!=(const Token &t) const
324  {
325  return !( *this == t );
326  }
327 
328  void info(std::ostream &) const;
329 
330  void pprint(std::ostream &) const;
331 
333  void clear_access_flag() { accessed_ = false; }
334  void set_access_flag() const { accessed_ = true; }
335 
343  bool accessed() const { return accessed_; }
344 
345 
350  template <typename DatumType>
351  bool is_a() const
352  {
353  return dynamic_cast<DatumType*>(p);
354  }
355 
356 
363  bool matches_as_string(const Token& rhs) const;
364 
365 };
366 
367 
368 /************* Misc functions ********************/
369 
370 std::ostream& operator<<(std::ostream&, const Token&);
371 
372 typedef unsigned long Index;
373 
374 #endif
Token(Datum *p_s=NULL)
Definition: token.h:98
Datum * p
Definition: token.h:74
void clear(void)
Definition: token.h:227
std::ostream & operator<<(std::ostream &, const Token &)
Definition: token.cc:169
void detach()
If the contained datum has more than one reference, clone it, so it can be modified.
Definition: token.h:133
size_t numReferences() const
Definition: datum.h:103
bool operator==(const Token &t) const
Definition: token.h:313
const Name d("d")
Specific to Izhikevich 2003.
Definition: nest_names.h:83
void swap(Token &c)
Definition: token.h:222
void move(Token &c)
Definition: token.h:142
bool accessed_
Flag for access control.
Definition: token.h:79
Token(const Datum &d)
Definition: token.h:102
bool valid() const
Definition: token.h:257
Token(const Token &c_s)
Definition: token.h:90
virtual Datum * get_ptr()
Returns a reference counted pointer to the datum, or a new pointer, if the type does not support refe...
Definition: datum.h:52
void init_by_ref(const Token &rhs)
Initialize the token with a reference.
Definition: token.h:184
Represent strings by ints to facilitate fast comparison.
Definition: name.h:53
Definition: tarrayobj.h:37
virtual bool equals(const Datum *d) const
Definition: datum.h:141
void info(std::ostream &) const
Definition: token.cc:145
bool is_a() const
Check whether Token contains a Datum of a given type.
Definition: token.h:351
Definition: tokenarray.h:62
void addReference() const
Definition: datum.h:91
Datum * operator->() const
Definition: token.h:262
assert(pNet!=0)
Datum & operator*() const
Definition: token.h:269
unsigned long Index
Definition: token.h:372
bool empty(void) const
Definition: token.h:239
void removeReference()
Definition: datum.h:96
void swap(sparsegroup< T, GROUP_SIZE, Alloc > &x, sparsegroup< T, GROUP_SIZE, Alloc > &y)
Definition: sparsetable.h:1348
bool contains(const Datum &d) const
Definition: token.h:234
void assign_by_pointer(Datum *rhs)
Definition: token.h:212
void init_move(Token &rhs)
Initialize the token by moving a datum from another token.
Definition: token.h:158
bool accessed() const
Check for access.
Definition: token.h:343
bool matches_as_string(const Token &rhs) const
Returns true if token equals rhs as string.
Definition: token.cc:178
bool operator!(void) const
Definition: token.h:244
Token & operator=(Datum *p_s)
Definition: token.h:300
void pprint(std::ostream &) const
Definition: token.cc:161
virtual Datum * clone(void) const =0
Virtual copy constructor.
void init_by_pointer(Datum *rhs)
Initialize the token with a datum pointer.
Definition: token.h:196
void assign_by_ref(const Token &rhs)
Definition: token.h:201
bool operator!=(const Token &t) const
Definition: token.h:323
Token & operator=(const Token &c_s)
Definition: token.h:282
Definition: datum.h:33
const std::type_info & type(void) const
Definition: token.h:276
A type-independent container for C++-types.
Definition: token.h:68
const Name c("c")
Specific to Izhikevich 2003.
Definition: nest_names.h:62
~Token()
Definition: token.h:83
void clear_access_flag()
Clear accessed flag.
Definition: token.h:333
Datum * datum(void) const
Definition: token.h:249
void set_access_flag() const
Definition: token.h:334
void init_by_copy(const Token &rhs)
Initialize the token by moving a datum from another token.
Definition: token.h:171