ITK  5.2.0
Insight Toolkit
itkObjectStore.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright NumFOCUS
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.txt
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  *=========================================================================*/
18 #ifndef itkObjectStore_h
19 #define itkObjectStore_h
20 
21 #include "itkObjectFactory.h"
22 #include "itkObject.h"
23 #include "itkIntTypes.h"
24 #include <vector>
25 
26 namespace itk
27 {
35 {
36 public:
40  enum class GrowthStrategy : uint8_t
41  {
42  LINEAR_GROWTH = 0,
44  };
45 };
46 extern ITKCommon_EXPORT std::ostream &
47  operator<<(std::ostream & out, const ObjectStoreEnums::GrowthStrategy value);
48 
83 template <typename TObjectType>
84 class ITK_TEMPLATE_EXPORT ObjectStore : public Object
85 {
86 public:
87  ITK_DISALLOW_COPY_AND_MOVE(ObjectStore);
88 
90  using Self = ObjectStore;
91  using Superclass = Object;
94 
96  itkNewMacro(Self);
97 
99  itkTypeMacro(ObjectStore, Object);
100 
102  using ObjectType = TObjectType;
103 
105  using FreeListType = std::vector<ObjectType *>;
106 
108 #if !defined(ITK_LEGACY_REMOVE)
109  // We need to expose the enum values at the class level
110  // for backwards compatibility
111  static constexpr GrowthStrategyEnum LINEAR_GROWTH = GrowthStrategyEnum::LINEAR_GROWTH;
112  static constexpr GrowthStrategyEnum EXPONENTIAL_GROWTH = GrowthStrategyEnum::EXPONENTIAL_GROWTH;
113 #endif
114 
115  ObjectType *
116  Borrow();
117 
121  void
122  Return(ObjectType * p);
123 
126  itkGetConstMacro(Size, SizeValueType);
127 
131  void
132  Reserve(SizeValueType n);
133 
136  void
137  Squeeze();
138 
140  void
141  Clear();
142 
144  itkSetMacro(LinearGrowthSize, SizeValueType);
145  itkGetConstMacro(LinearGrowthSize, SizeValueType);
147 
149  itkSetEnumMacro(GrowthStrategy, GrowthStrategyEnum);
150  itkGetConstMacro(GrowthStrategy, GrowthStrategyEnum);
152 
154  void
156  {
157  this->SetGrowthStrategy(GrowthStrategyEnum::EXPONENTIAL_GROWTH);
158  }
159 
161  void
163  {
164  this->SetGrowthStrategy(GrowthStrategyEnum::LINEAR_GROWTH);
165  }
166 
167 protected:
168  ObjectStore();
169  ~ObjectStore() override;
170  void
171  PrintSelf(std::ostream & os, Indent indent) const override;
172 
175  GetGrowthSize();
176 
177  struct MemoryBlock
178  {
180  : Begin(0)
181  {}
182 
184  : Size(n)
185  {
186  Begin = new ObjectType[n];
187  }
188 
189  ~MemoryBlock() = default; // Purposely does *not* free memory
190 
191  void
193  {
194  delete[] Begin;
195  }
196 
199  };
200 
201 private:
203 
206 
209 
211  std::vector<MemoryBlock> m_Store;
212 };
213 
214 } // end namespace itk
215 
216 #ifndef ITK_MANUAL_INSTANTIATION
217 # include "itkObjectStore.hxx"
218 #endif
219 
220 #endif
itk::ObjectStore::ObjectType
TObjectType ObjectType
Definition: itkObjectStore.h:102
itk::ObjectStore::MemoryBlock::MemoryBlock
MemoryBlock()
Definition: itkObjectStore.h:179
itk::uint8_t
::uint8_t uint8_t
Definition: itkIntTypes.h:29
itkObjectFactory.h
itk::ObjectStore::m_Size
SizeValueType m_Size
Definition: itkObjectStore.h:204
itk::ObjectStore::MemoryBlock
Definition: itkObjectStore.h:177
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:69
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:213
itk::ObjectStore::SetGrowthStrategyToExponential
void SetGrowthStrategyToExponential()
Definition: itkObjectStore.h:155
itk::ObjectStore::FreeListType
std::vector< ObjectType * > FreeListType
Definition: itkObjectStore.h:105
itk::SmartPointer< Self >
itk::Indent
Control indentation during Print() invocation.
Definition: itkIndent.h:49
itk::ObjectStore::m_LinearGrowthSize
SizeValueType m_LinearGrowthSize
Definition: itkObjectStore.h:205
itk::ObjectStore::m_Store
std::vector< MemoryBlock > m_Store
Definition: itkObjectStore.h:211
itk::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:59
itk::ObjectStore::MemoryBlock::Delete
void Delete()
Definition: itkObjectStore.h:192
itk::ObjectStoreEnums::GrowthStrategy::LINEAR_GROWTH
itk::ObjectStore
A specialized memory management object for allocating and destroying contiguous blocks of objects.
Definition: itkObjectStore.h:84
itk::ObjectStore::SetGrowthStrategyToLinear
void SetGrowthStrategyToLinear()
Definition: itkObjectStore.h:162
itk::ObjectStore::m_GrowthStrategy
GrowthStrategyEnum m_GrowthStrategy
Definition: itkObjectStore.h:202
itkIntTypes.h
itk::ObjectStoreEnums
enums for ObjectStore
Definition: itkObjectStore.h:34
itkObject.h
itk::ObjectStore::MemoryBlock::Begin
ObjectType * Begin
Definition: itkObjectStore.h:197
itk::ObjectStore::MemoryBlock::MemoryBlock
MemoryBlock(SizeValueType n)
Definition: itkObjectStore.h:183
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ObjectStoreEnums::GrowthStrategy::EXPONENTIAL_GROWTH
itk::ObjectStore::m_FreeList
FreeListType m_FreeList
Definition: itkObjectStore.h:208
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:62
itk::ObjectStoreEnums::GrowthStrategy
GrowthStrategy
Definition: itkObjectStore.h:40
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
GrowthStrategy