SheafSystem  0.0.0.0
list_pool.impl.h
Go to the documentation of this file.
1 
2 //
3 // Copyright (c) 2014 Limit Point Systems, Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 
20 
21 #ifndef LIST_POOL_IMPL_H
22 #define LIST_POOL_IMPL_H
23 
24 #ifndef LIST_POOL_H
25 #include "SheafSystem/list_pool.h"
26 #endif
27 
28 #ifndef SHEAF_DLL_SPEC_H
29 #include "SheafSystem/sheaf_dll_spec.h"
30 #endif
31 
32 #ifndef ASSERT_CONTRACT_H
33 #include "SheafSystem/assert_contract.h"
34 #endif
35 
36 namespace sheaf
37 {
38 
39 // ===========================================================
40 // LIST_POOL FACET
41 // ===========================================================
42 
43 // PUBLIC MEMBER FUNCTIONS
44 
45 template <typename T>
48 {
49  // Preconditions:
50 
51  // Body:
52 
53  // Postconditions:
54 
55  // Exit:
56 
57  return;
58 };
59 
60 template <typename T>
63 {
64  // Preconditions:
65 
66  // Body:
67 
68  typename allocated_type::iterator itr = _allocated.begin();
69 
70  while(itr != _allocated.end())
71  {
72  // Get the object to delete.
73 
74  T* obj = *itr;
75 
76  // Delete the object.
77 
78  delete obj;
79 
80  // Move to the next object.
81 
82  ++itr;
83  }
84 
85  // Postconditions:
86 
87  ensure(unexecutable("All allocated objects have been deleted."));
88 
89  // Exit:
90 
91  return;
92 };
93 
94 template <typename T>
95 T&
97 get()
98 {
99  // Preconditions:
100 
101  // Body:
102 
103  T* result;
104 
105  if(_free_list.empty())
106  {
107  // No available free list objects, allocate a new object.
108 
109  result = new T();
110 
111  // Add new object to the set of allocated objects.
112 
113  _allocated.insert(result);
114  }
115  else
116  {
117  // Get the top object in the list.
118 
119  result = _free_list.front();
120  _free_list.pop_front();
121  }
122 
123  // Postconditions:
124 
125  ensure(allocated(*result));
126 
127  // Exit:
128 
129  return *result;
130 };
131 
132 template <typename T>
133 void
135 release(T& xobj)
136 {
137  // Preconditions:
138 
139  require(allocated(xobj));
140 
141  // Body:
142 
143  _free_list.push_front(&xobj);
144 
145  // Postconditions:
146 
147  ensure(is_basic_query);
148 
149  // Exit:
150 
151  return;
152 };
153 
154 template <typename T>
155 bool
157 allocated(const T& xobj) const
158 {
159  // Preconditions:
160 
161  // Body:
162 
163  T& lobj = const_cast<T&>(xobj);
164 
165  bool result = (_allocated.find(&lobj) != _allocated.end());
166 
167  // Postconditions:
168 
169  ensure(is_basic_query);
170 
171  // Exit:
172 
173  return result;
174 };
175 
176 template <typename T>
179 ct() const
180 {
181  // Preconditions:
182 
183  // Body:
184 
185  size_type result = _allocated.size();
186 
187  // Postconditions:
188 
189  ensure(result >= 0);
190 
191  // Exit:
192 
193  return result;
194 };
195 
196 // PROTECTED MEMBER FUNCTIONS
197 
198 // PRIVATE MEMBER FUNCTIONS
199 
200 // ===========================================================
201 // NON-MEMBER FUNCTIONS
202 // ===========================================================
203 
204 template <typename T>
205 size_t
206 deep_size(const list_pool<T>& xpool, bool xinclude_shallow)
207 {
208  // Preconditions:
209 
210  // Body:
211 
212  size_t result = xinclude_shallow ? sizeof(xpool) : 0;
213 
215  for(itr = xpool._allocated.begin(); itr != xpool._allocated.end(); ++itr)
216  {
217  result += deep_size(**itr, true);
218  }
219 
220  // Postconditions:
221 
222  // Exit:
223 
224  return result;
225 };
226 
227 } // namespace sheaf
228 
229 #endif // ifndef LIST_POOL_IMPL_H
virtual ~list_pool()
Destructor.
T & get()
Allocates an object of type T from this pool.
size_type ct() const
SHEAF_DLL_SPEC size_t deep_size(const dof_descriptor_array &xp, bool xinclude_shallow=true)
The deep size of the referenced object of type dof_descriptor_array.
bool allocated(const T &xobj) const
True if and only if this pool allocated object xobj.
void release(T &xobj)
Return the object xobj to the free list of this pool.
list_pool()
Default constructor.
Namespace for the sheaves component of the sheaf system.
unsigned long int size_type
An unsigned integral type used to represent sizes and capacities.
Definition: list_pool.h:94
A reallocated pool of objects of type T. Objects in the pool are either allocated or stored in a free...
Definition: list_pool.h:42