C++ API Reference for Intel® Data Analytics Acceleration Library 2019 Update 4

internal/collection.h
1 /* file: collection.h */
2 /*******************************************************************************
3 * Copyright 2014-2019 Intel Corporation.
4 *
5 * This software and the related documents are Intel copyrighted materials, and
6 * your use of them is governed by the express license under which they were
7 * provided to you (License). Unless the License provides otherwise, you may not
8 * use, modify, copy, publish, distribute, disclose or transmit this software or
9 * the related documents without Intel's prior written permission.
10 *
11 * This software and the related documents are provided as is, with no express
12 * or implied warranties, other than those that are expressly stated in the
13 * License.
14 *******************************************************************************/
15 
16 #ifndef __SERVICES_INTERNAL_COLLECTION_H__
17 #define __SERVICES_INTERNAL_COLLECTION_H__
18 
19 #include "services/base.h"
20 #include "services/collection.h"
21 #include "services/internal/error_handling_helpers.h"
22 
23 namespace daal
24 {
25 namespace services
26 {
27 namespace internal
28 {
36 template<typename T, typename Deleter = ObjectDeleter<T> >
37 class ObjectPtrCollection : public Base
38 {
39 public:
40  ObjectPtrCollection() { }
41 
42  ObjectPtrCollection(const Deleter &deleter) :
43  _deleter(deleter) { }
44 
45  virtual ~ObjectPtrCollection()
46  {
47  for (size_t i = 0; i < _objects.size(); i++)
48  { _deleter( (const void *)_objects[i] ); }
49  }
50 
51  T &operator [] (size_t index) const
52  {
53  DAAL_ASSERT( index < _objects.size() );
54  return *(_objects[index]);
55  }
56 
57  size_t size() const
58  {
59  return _objects.size();
60  }
61 
62  bool push_back(T *object)
63  {
64  if (!object)
65  { return false; }
66 
67  return _objects.safe_push_back(object);
68  }
69 
70  template<typename U>
71  bool safe_push_back()
72  {
73  return _objects.push_back(new U());
74  }
75 
76 private:
77  ObjectPtrCollection(const ObjectPtrCollection &);
78  ObjectPtrCollection &operator = (const ObjectPtrCollection &);
79 
80 private:
81  Deleter _deleter;
82  services::Collection<T *> _objects;
83 };
84 
91 template<typename T>
92 class HeapAllocatableCollection : public Base, public services::Collection<T>
93 {
94 public:
95  static SharedPtr<HeapAllocatableCollection<T> > create(services::Status *status = NULL)
96  {
97  typedef SharedPtr<HeapAllocatableCollection<T> > PtrType;
98 
99  HeapAllocatableCollection<T> *collection = new internal::HeapAllocatableCollection<T>();
100  if (!collection)
101  {
102  services::internal::tryAssignStatusAndThrow(status, services::ErrorMemoryAllocationFailed);
103  return PtrType();
104  }
105 
106  return PtrType(collection);
107  }
108 
109  static SharedPtr<HeapAllocatableCollection<T> > create(size_t n, services::Status *status = NULL)
110  {
111  typedef SharedPtr<HeapAllocatableCollection<T> > PtrType;
112 
113  HeapAllocatableCollection<T> *collection = new internal::HeapAllocatableCollection<T>(n);
114  if (!collection || !collection->data())
115  {
116  delete collection;
117  services::internal::tryAssignStatusAndThrow(status, services::ErrorMemoryAllocationFailed);
118  return PtrType();
119  }
120 
121  return PtrType(collection);
122  }
123 
124  HeapAllocatableCollection() { }
125 
126  explicit HeapAllocatableCollection(size_t n) :
127  services::Collection<T>(n) { }
128 };
129 
135 template<class T>
136 class CollectionPtr : public SharedPtr<HeapAllocatableCollection<T> >
137 {
138 private:
139  typedef SharedPtr<HeapAllocatableCollection<T> > super;
140 
141 public:
142  CollectionPtr() { }
143 
144  template<class U>
145  CollectionPtr(const SharedPtr<U> &other) : super(other) { }
146 
147  template<class U>
148  explicit CollectionPtr(U *ptr) : super(ptr) { }
149 
150  template<class U, class D>
151  explicit CollectionPtr(U *ptr, const D& deleter) : super(ptr, deleter) { }
152 };
153 
154 } // namespace internal
155 } // namespace services
156 } // namespace daal
157 
158 #endif
daal::services::internal::HeapAllocatableCollection
Wrapper for services::Collection that allocates and deallocates memory using internal new/delete oper...
Definition: internal/collection.h:92
daal
Definition: algorithm_base_common.h:31
daal::services::internal::CollectionPtr
Shared pointer to the Collection object.
Definition: internal/collection.h:136
daal::services::ErrorMemoryAllocationFailed
Definition: error_indexes.h:146
daal::services::internal::ObjectPtrCollection
Class that implements functionality of collection container and holds pointers to objects of specifie...
Definition: internal/collection.h:37
daal::Base
Base class for Intel(R) Data Analytics Acceleration Library objects
Definition: base.h:39

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