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

daal_shared_ptr.h
1 /* file: daal_shared_ptr.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 /*
19 //++
20 // Declaration and implementation of the shared pointer class.
21 //--
22 */
23 
24 #ifndef __DAAL_SHARED_PTR_H__
25 #define __DAAL_SHARED_PTR_H__
26 
27 #include "services/base.h"
28 #include "services/daal_memory.h"
29 #include "services/error_id.h"
30 #include "services/daal_atomic_int.h"
31 
32 namespace daal
33 {
34 namespace services
35 {
36 
37 namespace interface1
38 {
49 class DeleterIface
50 {
51 public:
55  virtual ~DeleterIface() {}
56 
61  virtual void operator() (const void *ptr) = 0;
62 };
63 
70 template<class T>
71 class ObjectDeleter : public DeleterIface
72 {
73 public:
74  void operator() (const void *ptr) DAAL_C11_OVERRIDE
75  {
76  delete (const T *)(ptr);
77  }
78 };
79 
86 class ServiceDeleter : public DeleterIface
87 {
88 public:
89  void operator() (const void *ptr) DAAL_C11_OVERRIDE
90  {
91  daal::services::daal_free((void *)ptr);
92  }
93 };
94 
101 class EmptyDeleter : public DeleterIface
102 {
103 public:
104  void operator() (const void *ptr) DAAL_C11_OVERRIDE
105  {
106  }
107 };
108 
114 class DAAL_EXPORT RefCounter: public AtomicInt
115 {
116 public:
120  RefCounter() : AtomicInt(1){}
122  virtual ~RefCounter() {}
123  virtual void operator() (const void *ptr) = 0;
124 };
125 
132 template<class Deleter>
133 class RefCounterImp: public RefCounter
134 {
135 public:
139  RefCounterImp(){}
140  RefCounterImp(const Deleter& d) : _deleter(d){}
142  virtual ~RefCounterImp() {}
143  void operator() (const void *ptr) DAAL_C11_OVERRIDE
144  {
145  _deleter(ptr);
146  }
147 protected:
148  Deleter _deleter;
149 };
150 
162 template<class T>
163 class SharedPtr
164 {
165 public:
166  DAAL_NEW_DELETE();
167 
168  typedef T ElementType;
169 
173  SharedPtr() : _ownedPtr(NULL), _ptr(NULL), _refCount(NULL)
174  {
175  }
176 
181  template<class U>
182  explicit SharedPtr(U *ptr) : _ownedPtr(ptr), _ptr(ptr), _refCount(NULL)
183  {
184  if(_ownedPtr)
185  _refCount = new RefCounterImp<ObjectDeleter<U> >();
186  }
187 
195  template<class U, class D>
196  explicit SharedPtr(U *ptr, const D& deleter) : _ownedPtr(ptr), _ptr(ptr), _refCount(NULL)
197  {
198  if(_ownedPtr)
199  _refCount = new RefCounterImp<D>(deleter);
200  }
201 
202  SharedPtr(const SharedPtr<T> &ptr);
203 
204  SharedPtr(const SharedPtr<T> &ptr, T* shiftedPtr);
205 
210  template<class U>
211  SharedPtr(const SharedPtr<U> &other);
212 
223  template<class U>
224  SharedPtr(const SharedPtr<U> &r, T *ptr, T *shiftedPtr);
225 
230  ~SharedPtr() { _remove(); }
231 
236  SharedPtr<T> &operator=(const SharedPtr<T> &ptr);
237 
241  void reset()
242  {
243  _remove();
244  _ownedPtr = NULL;
245  _refCount = NULL;
246  _ptr = NULL;
247  }
248 
254  template<class U>
255  void reset(U* ptr);
256 
264  template<class U, class D>
265  void reset(U* ptr, const D& deleter);
266 
271  template<class U>
272  SharedPtr<T> &operator=(const SharedPtr<U> &ptr)
273  {
274  if (((void *)&ptr != (void *)this) && ((void *)(ptr.get()) != (void *)(this->_ownedPtr)))
275  {
276  _remove();
277  _ownedPtr = ptr._ownedPtr;
278  _refCount = ptr._refCount;
279  _ptr = ptr._ptr;
280  if(_refCount)
281  _refCount->inc();
282  }
283  return *this;
284  }
285 
290  T *operator->() const { return _ptr; }
291 
296  T &operator* () const { return *_ptr; }
297 
302  operator bool() const { return (_ptr != NULL); }
303 
308  T *get() const { return _ptr; }
309 
314  T *getStartPtr() const { return _ownedPtr; }
315 
320  int useCount() const { return _refCount ? _refCount->get() : 0; }
321 
322 protected:
323  T *_ownedPtr; /* Pointer to the beginning of the owned memory */
324  T *_ptr; /* Pointer to return */
325  RefCounter *_refCount; /* Reference count */
326 
331  void _remove();
332 
333  template<class U> friend class SharedPtr;
334 }; // class SharedPtr
335 
336 template<class T>
337 template<class U>
338 SharedPtr<T>::SharedPtr(const SharedPtr<U> &other) : _ownedPtr(other._ownedPtr), _ptr(other._ptr), _refCount(other._refCount)
339 {
340  if(_refCount)
341  _refCount->inc();
342 }
343 
344 template<class T>
345 SharedPtr<T>::SharedPtr(const SharedPtr<T> &other) : _ownedPtr(other._ownedPtr), _ptr(other._ptr), _refCount(other._refCount)
346 {
347  if(_refCount)
348  _refCount->inc();
349 }
350 
351 template<class T>
352 SharedPtr<T>::SharedPtr(const SharedPtr<T> &other, T* shiftedPtr) : _ownedPtr(other._ownedPtr), _ptr(shiftedPtr), _refCount(other._refCount)
353 {
354  if(_refCount)
355  _refCount->inc();
356 }
357 
358 template<class T>
359 template<class U>
360 SharedPtr<T>::SharedPtr(const SharedPtr<U> &other, T *ptr, T* shiftedPtr) : _ownedPtr(ptr), _ptr(shiftedPtr), _refCount(other._refCount)
361 {
362  if(_refCount)
363  _refCount->inc();
364 }
365 
366 template<class T>
367 void SharedPtr<T>::_remove()
368 {
369  if(_refCount && (_refCount->dec() <= 0))
370  {
371  (*_refCount)(_ownedPtr);
372  delete _refCount;
373  _refCount = NULL;
374  _ptr = NULL;
375  }
376 }
377 
378 template<class T>
379 SharedPtr<T> &SharedPtr<T>::operator=(const SharedPtr<T> &ptr)
380 {
381  if (&ptr != this || ptr._ownedPtr != this->_ownedPtr || ptr._ptr != this->_ptr)
382  {
383  _remove();
384  _ownedPtr = ptr._ownedPtr;
385  _refCount = ptr._refCount;
386  _ptr = ptr._ptr;
387  if(_refCount)
388  _refCount->inc();
389  }
390  return *this;
391 }
392 
393 template<class T>
394 template<class U>
395 void SharedPtr<T>::reset(U* ptr)
396 {
397  if(ptr != this->_ownedPtr)
398  {
399  _remove();
400  _ownedPtr = ptr;
401  _ptr = ptr;
402  _refCount = (ptr ? new RefCounterImp<ObjectDeleter<U> >() : NULL);
403  }
404 }
405 
406 template<class T>
407 template<class U, class D>
408 void SharedPtr<T>::reset(U* ptr, const D& deleter)
409 {
410  if(ptr != this->_ownedPtr)
411  {
412  _remove();
413  _ownedPtr = ptr;
414  _ptr = ptr;
415  _refCount = (ptr ? new RefCounterImp<D>(deleter) : NULL);
416  }
417 }
418 
424 template<class T, class U>
425 SharedPtr<T> staticPointerCast(const SharedPtr<U> &r)
426 {
427  T *shifted = static_cast<T *>(r.get());
428  T *start = static_cast<T *>(r.getStartPtr());
429  return SharedPtr<T>(r, start, shifted);
430 }
431 
437 template<class T, class U>
438 SharedPtr<T> reinterpretPointerCast(const SharedPtr<U> &r)
439 {
440  T *shifted = reinterpret_cast<T *>(r.get());
441  T *start = reinterpret_cast<T *>(r.getStartPtr());
442  return SharedPtr<T>(r, start, shifted);
443 }
444 
450 template<class T, class U>
451 SharedPtr<T> dynamicPointerCast(const SharedPtr<U> &r)
452 {
453  T *shifted = dynamic_cast<T *>(r.get());
454  T *start = dynamic_cast<T *>(r.getStartPtr());
455  if (!r.get() || start)
456  {
457  return SharedPtr<T>(r, start, shifted);
458  }
459  else
460  {
461  return SharedPtr<T>();
462  }
463 }
465 } // namespace interface1
466 using interface1::DeleterIface;
467 using interface1::ObjectDeleter;
468 using interface1::EmptyDeleter;
469 using interface1::ServiceDeleter;
470 using interface1::RefCounter;
471 using interface1::RefCounterImp;
472 using interface1::SharedPtr;
473 using interface1::staticPointerCast;
474 using interface1::dynamicPointerCast;
475 using interface1::reinterpretPointerCast;
476 
477 } // namespace services;
478 } // namespace daal
479 
480 #endif
daal::services::interface1::reinterpretPointerCast
SharedPtr< T > reinterpretPointerCast(const SharedPtr< U > &r)
Definition: daal_shared_ptr.h:438
daal::services::interface1::EmptyDeleter::operator()
void operator()(const void *ptr) DAAL_C11_OVERRIDE
Definition: daal_shared_ptr.h:104
daal::services::interface1::SharedPtr::operator*
T & operator*() const
Definition: daal_shared_ptr.h:296
daal::services::interface1::dynamicPointerCast
SharedPtr< T > dynamicPointerCast(const SharedPtr< U > &r)
Definition: daal_shared_ptr.h:451
daal::services::interface1::SharedPtr::operator=
SharedPtr< T > & operator=(const SharedPtr< U > &ptr)
Definition: daal_shared_ptr.h:272
daal::services::interface1::ServiceDeleter
Implementation of DeleterIface to destroy a pointer by the daal_free function.
Definition: daal_shared_ptr.h:86
daal::services::interface1::ServiceDeleter::operator()
void operator()(const void *ptr) DAAL_C11_OVERRIDE
Definition: daal_shared_ptr.h:89
daal::services::interface1::SharedPtr::SharedPtr
SharedPtr()
Definition: daal_shared_ptr.h:173
daal::services::interface1::SharedPtr::SharedPtr
SharedPtr(U *ptr)
Definition: daal_shared_ptr.h:182
daal::services::interface1::SharedPtr::get
T * get() const
Definition: daal_shared_ptr.h:308
daal::services::interface1::SharedPtr::~SharedPtr
~SharedPtr()
Definition: daal_shared_ptr.h:230
daal::services::interface1::RefCounterImp::~RefCounterImp
virtual ~RefCounterImp()
Definition: daal_shared_ptr.h:142
daal::services::interface1::DeleterIface::~DeleterIface
virtual ~DeleterIface()
Definition: daal_shared_ptr.h:55
daal::services::interface1::SharedPtr::getStartPtr
T * getStartPtr() const
Definition: daal_shared_ptr.h:314
daal::services::interface1::RefCounterImp::RefCounterImp
RefCounterImp()
Definition: daal_shared_ptr.h:139
daal::services::interface1::SharedPtr::_remove
void _remove()
Definition: daal_shared_ptr.h:367
daal::services::interface1::SharedPtr::useCount
int useCount() const
Definition: daal_shared_ptr.h:320
daal::services::interface1::DeleterIface
Interface for a utility class used within SharedPtr to delete an object when the object owner is dest...
Definition: daal_shared_ptr.h:49
daal::services::interface1::SharedPtr
Shared pointer that retains shared ownership of an object through a pointer. Several SharedPtr object...
Definition: daal_shared_ptr.h:163
daal::services::interface1::RefCounter::~RefCounter
virtual ~RefCounter()
Definition: daal_shared_ptr.h:122
daal::services::interface1::DeleterIface::operator()
virtual void operator()(const void *ptr)=0
daal::services::interface1::RefCounter::RefCounter
RefCounter()
Definition: daal_shared_ptr.h:120
daal::services::interface1::ObjectDeleter
Implementation of DeleterIface to destroy a pointer by the delete operator.
Definition: daal_shared_ptr.h:71
daal::services::interface1::SharedPtr::SharedPtr
SharedPtr(U *ptr, const D &deleter)
Definition: daal_shared_ptr.h:196
daal::services::interface1::staticPointerCast
SharedPtr< T > staticPointerCast(const SharedPtr< U > &r)
Definition: daal_shared_ptr.h:425
daal::services::daal_free
DAAL_EXPORT void daal_free(void *ptr)
daal::services::interface1::SharedPtr::operator->
T * operator->() const
Definition: daal_shared_ptr.h:290
daal::services::interface1::SharedPtr::reset
void reset()
Definition: daal_shared_ptr.h:241
daal::services::interface1::ObjectDeleter::operator()
void operator()(const void *ptr) DAAL_C11_OVERRIDE
Definition: daal_shared_ptr.h:74
daal::services::interface1::SharedPtr::operator=
SharedPtr< T > & operator=(const SharedPtr< T > &ptr)
Definition: daal_shared_ptr.h:379
daal::services::interface1::RefCounter
Implementation of reference counter.
Definition: daal_shared_ptr.h:114
daal::services::interface1::EmptyDeleter
Implementation of DeleterIface without pointer destroying.
Definition: daal_shared_ptr.h:101
daal::services::interface1::RefCounterImp
Provides implementations of the operator() method of the RefCounter class.
Definition: daal_shared_ptr.h:133

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