C++ API Reference for Intel® Data Analytics Acceleration Library 2020 Update 1

internal/collection.h
1 /* file: collection.h */
2 /*******************************************************************************
3 * Copyright 2014-2020 Intel Corporation
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 
18 #ifndef __SERVICES_INTERNAL_COLLECTION_H__
19 #define __SERVICES_INTERNAL_COLLECTION_H__
20 
21 #include "services/base.h"
22 #include "services/collection.h"
23 #include "services/internal/error_handling_helpers.h"
24 
25 namespace daal
26 {
27 namespace services
28 {
29 namespace internal
30 {
38 template<typename T, typename Deleter = ObjectDeleter<T> >
39 class ObjectPtrCollection : public Base
40 {
41 public:
42  ObjectPtrCollection() { }
43 
44  ObjectPtrCollection(const Deleter &deleter) :
45  _deleter(deleter) { }
46 
47  virtual ~ObjectPtrCollection()
48  {
49  for (size_t i = 0; i < _objects.size(); i++)
50  { _deleter( (const void *)_objects[i] ); }
51  }
52 
53  T &operator [] (size_t index) const
54  {
55  DAAL_ASSERT( index < _objects.size() );
56  return *(_objects[index]);
57  }
58 
59  size_t size() const
60  {
61  return _objects.size();
62  }
63 
64  bool push_back(T *object)
65  {
66  if (!object)
67  { return false; }
68 
69  return _objects.safe_push_back(object);
70  }
71 
72  template<typename U>
73  bool safe_push_back()
74  {
75  return _objects.push_back(new U());
76  }
77 
78 private:
79  ObjectPtrCollection(const ObjectPtrCollection &);
80  ObjectPtrCollection &operator = (const ObjectPtrCollection &);
81 
82 private:
83  Deleter _deleter;
84  services::Collection<T *> _objects;
85 };
86 
93 template<typename T>
94 class HeapAllocatableCollection : public Base, public services::Collection<T>
95 {
96 public:
97  static SharedPtr<HeapAllocatableCollection<T> > create(services::Status *status = NULL)
98  {
99  typedef SharedPtr<HeapAllocatableCollection<T> > PtrType;
100 
101  HeapAllocatableCollection<T> *collection = new internal::HeapAllocatableCollection<T>();
102  if (!collection)
103  {
104  services::internal::tryAssignStatusAndThrow(status, services::ErrorMemoryAllocationFailed);
105  return PtrType();
106  }
107 
108  return PtrType(collection);
109  }
110 
111  static SharedPtr<HeapAllocatableCollection<T> > create(size_t n, services::Status *status = NULL)
112  {
113  typedef SharedPtr<HeapAllocatableCollection<T> > PtrType;
114 
115  HeapAllocatableCollection<T> *collection = new internal::HeapAllocatableCollection<T>(n);
116  if (!collection || !collection->data())
117  {
118  delete collection;
119  services::internal::tryAssignStatusAndThrow(status, services::ErrorMemoryAllocationFailed);
120  return PtrType();
121  }
122 
123  return PtrType(collection);
124  }
125 
126  HeapAllocatableCollection() { }
127 
128  explicit HeapAllocatableCollection(size_t n) :
129  services::Collection<T>(n) { }
130 };
131 
137 template<class T>
138 class CollectionPtr : public SharedPtr<HeapAllocatableCollection<T> >
139 {
140 private:
141  typedef SharedPtr<HeapAllocatableCollection<T> > super;
142 
143 public:
144  CollectionPtr() { }
145 
146  template<class U>
147  CollectionPtr(const SharedPtr<U> &other) : super(other) { }
148 
149  template<class U>
150  explicit CollectionPtr(U *ptr) : super(ptr) { }
151 
152  template<class U, class D>
153  explicit CollectionPtr(U *ptr, const D& deleter) : super(ptr, deleter) { }
154 };
155 
156 } // namespace internal
157 } // namespace services
158 } // namespace daal
159 
160 #endif
daal::services::internal::ObjectPtrCollection
Class that implements functionality of collection container and holds pointers to objects of specifie...
Definition: internal/collection.h:39
daal::services::internal::HeapAllocatableCollection
Wrapper for services::Collection that allocates and deallocates memory using internal new/delete oper...
Definition: internal/collection.h:94
daal::Base
Base class for Intel(R) Data Analytics Acceleration Library objects
Definition: base.h:41
daal::services::internal::CollectionPtr
Shared pointer to the Collection object.
Definition: internal/collection.h:138
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:150

For more complete information about compiler optimizations, see our Optimization Notice.