ITK  5.4.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  * https://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  itkOverrideGetNameOfClassMacro(ObjectStore);
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);
149  itkSetEnumMacro(GrowthStrategy, GrowthStrategyEnum);
150  itkGetConstMacro(GrowthStrategy, GrowthStrategyEnum);
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() = default;
169  ~ObjectStore() override;
170  void
171  PrintSelf(std::ostream & os, Indent indent) const override;
172 
175  GetGrowthSize();
176 
177  struct MemoryBlock
178  {
179  MemoryBlock() = default;
180 
182  : Size(n)
183  {
184  Begin = new ObjectType[n];
185  }
186 
187  ~MemoryBlock() = default; // Purposely does *not* free memory
188 
189  void
191  {
192  delete[] Begin;
193  }
194 
195  ObjectType * Begin{};
197  };
198 
199 private:
200  GrowthStrategyEnum m_GrowthStrategy{ GrowthStrategyEnum::EXPONENTIAL_GROWTH };
201 
202  SizeValueType m_Size{};
203  SizeValueType m_LinearGrowthSize{ 1024 };
204 
206  FreeListType m_FreeList{};
207 
209  std::vector<MemoryBlock> m_Store{};
210 };
211 
212 } // end namespace itk
213 
214 #ifndef ITK_MANUAL_INSTANTIATION
215 # include "itkObjectStore.hxx"
216 #endif
217 
218 #endif
itk::ObjectStore::ObjectType
TObjectType ObjectType
Definition: itkObjectStore.h:102
itkObjectFactory.h
itk::ObjectStore::MemoryBlock
Definition: itkObjectStore.h:177
itk::Size
Represent a n-dimensional size (bounds) of a n-dimensional image.
Definition: itkSize.h:71
itk::operator<<
std::ostream & operator<<(std::ostream &os, const Array< TValue > &arr)
Definition: itkArray.h:216
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::LightObject
Light weight base class for most itk classes.
Definition: itkLightObject.h:55
itk::ObjectStore::MemoryBlock::Delete
void Delete()
Definition: itkObjectStore.h:190
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
itkIntTypes.h
itk::ObjectStoreEnums
enums for ObjectStore
Definition: itkObjectStore.h:34
itkObject.h
itk::ObjectStore::MemoryBlock::MemoryBlock
MemoryBlock(SizeValueType n)
Definition: itkObjectStore.h:181
itk
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....
Definition: itkAnnulusOperator.h:24
itk::ObjectStoreEnums::GrowthStrategy::EXPONENTIAL_GROWTH
itk::Object
Base class for most ITK classes.
Definition: itkObject.h:61
itk::ObjectStoreEnums::GrowthStrategy
GrowthStrategy
Definition: itkObjectStore.h:40
itk::SizeValueType
unsigned long SizeValueType
Definition: itkIntTypes.h:83
GrowthStrategy