NEST  2.6.0,not_revisioned_source_dir@0
instance.h
Go to the documentation of this file.
1 /*
2  * instance.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 INSTANCE_H
24 #define INSTANCE_H
25 #include "nest.h"
26 #include "allocator.h"
27 
28 
29 namespace nest
30 {
75  template<typename ClassT>
76  class Instance: public ClassT
77  {
78 
79  public:
81  :ClassT(){}
82 
84  :ClassT(c){}
85 
86  Instance(const ClassT &c)
87  :ClassT(c){}
88 
89  ClassT *clone() const;
90 
91  static void reserve_additional(size_t);
92 
93  static void * operator new(size_t size);
94  static void operator delete(void *p, size_t size);
95 
96  static size_t memory_used();
97  static size_t memory_capacity();
98 
99  static size_t instantiations();
100 
101  private:
103  };
104 
105  template<typename ClassT>
106  inline
107  ClassT * Instance<ClassT>::clone() const
108  {
109  return new Instance<ClassT>(*this);
110  }
111 
112  template<typename ClassT>
113  inline
114  void * Instance<ClassT>::operator new(size_t size)
115  {
116  if(size != sizeof(Instance<ClassT>))
117  {
124  return ::operator new(size);
125  }
126  return memory_.alloc();
127  }
128 
129  template< typename ClassT>
130  inline
131  void Instance<ClassT>::operator delete(void *p, size_t size)
132  {
133  if(p == NULL)
134  return;
135 
136  if(size != sizeof(Instance<ClassT>))
137  {
138  /*
139  * If a derived class does not possess its own
140  * new/delete operators, they will end up here.
141  * Thus, we forward their request to the standard
142  * new/delete operators.
143  */
144  ::operator delete(p);
145  return;
146  }
147  memory_.free(p);
148  }
149 
150  template <typename ClassT>
151  inline
153  {
154  memory_.reserve_additional(s);
155  }
156 
157  template <typename ClassT>
158  inline
160  {
161  return memory_.get_instantiations() * memory_.get_el_size();
162  }
163 
164  template <typename ClassT>
165  inline
167  {
168  return memory_.get_total() * memory_.get_el_size();
169  }
170 
171  template <typename ClassT>
172  inline
174  {
175  return memory_.get_instantiations();
176  }
177 
178 
180  template <typename ClassT>
182 
183 } // namespace nest
184 #endif
185 
186 
187 
188 
189 
190 
191 
192 
193 
static sli::pool memory_
Declaration of static memory object.
Definition: instance.h:102
ClassT * clone() const
Definition: instance.h:107
Instance(const Instance< ClassT > &c)
Definition: instance.h:83
Instance(const ClassT &c)
Definition: instance.h:86
static size_t memory_capacity()
Definition: instance.h:166
Instance()
Definition: instance.h:80
Instance is a template is used to provide a pool-based management for arbitrary classes.
Definition: instance.h:76
static size_t memory_used()
Definition: instance.h:159
Default types used by the NEST kernel.
pool is a specialized allocator class for many identical small objects.
Definition: allocator.h:50
static void reserve_additional(size_t)
Definition: instance.h:152
const Name p("p")
current release probability (Tsodyks2_connection)
Definition: nest_names.h:218
const Name c("c")
Specific to Izhikevich 2003.
Definition: nest_names.h:62
static size_t instantiations()
Definition: instance.h:173