C++ API Reference for Intel® Data Analytics Acceleration Library 2018 Update 2

daal_shared_ptr.h
1 /* file: daal_shared_ptr.h */
2 /*******************************************************************************
3 * Copyright 2014-2018 Intel Corporation
4 * All Rights Reserved.
5 *
6 * If this software was obtained under the Intel Simplified Software License,
7 * the following terms apply:
8 *
9 * The source code, information and material ("Material") contained herein is
10 * owned by Intel Corporation or its suppliers or licensors, and title to such
11 * Material remains with Intel Corporation or its suppliers or licensors. The
12 * Material contains proprietary information of Intel or its suppliers and
13 * licensors. The Material is protected by worldwide copyright laws and treaty
14 * provisions. No part of the Material may be used, copied, reproduced,
15 * modified, published, uploaded, posted, transmitted, distributed or disclosed
16 * in any way without Intel's prior express written permission. No license under
17 * any patent, copyright or other intellectual property rights in the Material
18 * is granted to or conferred upon you, either expressly, by implication,
19 * inducement, estoppel or otherwise. Any license under such intellectual
20 * property rights must be express and approved by Intel in writing.
21 *
22 * Unless otherwise agreed by Intel in writing, you may not remove or alter this
23 * notice or any other notice embedded in Materials by Intel or Intel's
24 * suppliers or licensors in any way.
25 *
26 *
27 * If this software was obtained under the Apache License, Version 2.0 (the
28 * "License"), the following terms apply:
29 *
30 * You may not use this file except in compliance with the License. You may
31 * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
32 *
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
36 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 *
38 * See the License for the specific language governing permissions and
39 * limitations under the License.
40 *******************************************************************************/
41 
42 /*
43 //++
44 // Declaration and implementation of the shared pointer class.
45 //--
46 */
47 
48 #ifndef __DAAL_SHARED_PTR_H__
49 #define __DAAL_SHARED_PTR_H__
50 
51 #include "services/base.h"
52 #include "services/daal_memory.h"
53 #include "services/error_id.h"
54 #include "services/daal_atomic_int.h"
55 
56 namespace daal
57 {
58 namespace services
59 {
60 
61 namespace interface1
62 {
73 class DeleterIface
74 {
75 public:
79  virtual ~DeleterIface() {}
80 
85  virtual void operator() (const void *ptr) = 0;
86 };
87 
94 template<class T>
95 class ObjectDeleter : public DeleterIface
96 {
97 public:
98  void operator() (const void *ptr) DAAL_C11_OVERRIDE
99  {
100  delete (const T *)(ptr);
101  }
102 };
103 
110 class ServiceDeleter : public DeleterIface
111 {
112 public:
113  void operator() (const void *ptr) DAAL_C11_OVERRIDE
114  {
115  daal::services::daal_free((void *)ptr);
116  }
117 };
118 
125 class EmptyDeleter : public DeleterIface
126 {
127 public:
128  void operator() (const void *ptr) DAAL_C11_OVERRIDE
129  {
130  }
131 };
132 
138 class DAAL_EXPORT RefCounter: public AtomicInt
139 {
140 public:
144  RefCounter() : AtomicInt(1){}
146  virtual ~RefCounter() {}
147  virtual void operator() (const void *ptr) = 0;
148 };
149 
156 template<class Deleter>
157 class RefCounterImp: public RefCounter
158 {
159 public:
163  RefCounterImp(){}
164  RefCounterImp(const Deleter& d) : _deleter(d){}
166  virtual ~RefCounterImp() {}
167  void operator() (const void *ptr) DAAL_C11_OVERRIDE
168  {
169  _deleter(ptr);
170  }
171 protected:
172  Deleter _deleter;
173 };
174 
186 template<class T>
187 class SharedPtr
188 {
189 public:
190  DAAL_NEW_DELETE();
191 
192  typedef T ElementType;
193 
197  SharedPtr() : _ownedPtr(NULL), _ptr(NULL), _refCount(NULL)
198  {
199  }
200 
205  template<class U>
206  explicit SharedPtr(U *ptr) : _ownedPtr(ptr), _ptr(ptr), _refCount(NULL)
207  {
208  if(_ownedPtr)
209  _refCount = new RefCounterImp<ObjectDeleter<U> >();
210  }
211 
219  template<class U, class D>
220  explicit SharedPtr(U *ptr, const D& deleter) : _ownedPtr(ptr), _ptr(ptr), _refCount(NULL)
221  {
222  if(_ownedPtr)
223  _refCount = new RefCounterImp<D>(deleter);
224  }
225 
226  SharedPtr(const SharedPtr<T> &ptr);
227 
228  SharedPtr(const SharedPtr<T> &ptr, T* shiftedPtr);
229 
234  template<class U>
235  SharedPtr(const SharedPtr<U> &other);
236 
247  template<class U>
248  SharedPtr(const SharedPtr<U> &r, T *ptr, T *shiftedPtr);
249 
254  ~SharedPtr() { _remove(); }
255 
260  SharedPtr<T> &operator=(const SharedPtr<T> &ptr);
261 
265  void reset()
266  {
267  _remove();
268  _ownedPtr = NULL;
269  _refCount = NULL;
270  _ptr = NULL;
271  }
272 
278  template<class U>
279  void reset(U* ptr);
280 
288  template<class U, class D>
289  void reset(U* ptr, const D& deleter);
290 
295  template<class U>
296  SharedPtr<T> &operator=(const SharedPtr<U> &ptr)
297  {
298  if (((void *)&ptr != (void *)this) && ((void *)(ptr.get()) != (void *)(this->_ownedPtr)))
299  {
300  _remove();
301  _ownedPtr = ptr._ownedPtr;
302  _refCount = ptr._refCount;
303  _ptr = ptr._ptr;
304  if(_refCount)
305  _refCount->inc();
306  }
307  return *this;
308  }
309 
314  T *operator->() const { return _ptr; }
315 
320  T &operator* () const { return *_ptr; }
321 
326  operator bool() const { return (_ptr != NULL); }
327 
332  T *get() const { return _ptr; }
333 
338  T *getStartPtr() const { return _ownedPtr; }
339 
344  int useCount() const { return _refCount ? _refCount->get() : 0; }
345 
346 protected:
347  T *_ownedPtr; /* Pointer to the beginning of the owned memory */
348  T *_ptr; /* Pointer to return */
349  RefCounter *_refCount; /* Reference count */
350 
355  void _remove();
356 
357  template<class U> friend class SharedPtr;
358 }; // class SharedPtr
359 
360 template<class T>
361 template<class U>
362 SharedPtr<T>::SharedPtr(const SharedPtr<U> &other) : _ownedPtr(other._ownedPtr), _ptr(other._ptr), _refCount(other._refCount)
363 {
364  if(_refCount)
365  _refCount->inc();
366 }
367 
368 template<class T>
369 SharedPtr<T>::SharedPtr(const SharedPtr<T> &other) : _ownedPtr(other._ownedPtr), _ptr(other._ptr), _refCount(other._refCount)
370 {
371  if(_refCount)
372  _refCount->inc();
373 }
374 
375 template<class T>
376 SharedPtr<T>::SharedPtr(const SharedPtr<T> &other, T* shiftedPtr) : _ownedPtr(other._ownedPtr), _ptr(shiftedPtr), _refCount(other._refCount)
377 {
378  if(_refCount)
379  _refCount->inc();
380 }
381 
382 template<class T>
383 template<class U>
384 SharedPtr<T>::SharedPtr(const SharedPtr<U> &other, T *ptr, T* shiftedPtr) : _ownedPtr(ptr), _ptr(shiftedPtr), _refCount(other._refCount)
385 {
386  if(_refCount)
387  _refCount->inc();
388 }
389 
390 template<class T>
391 void SharedPtr<T>::_remove()
392 {
393  if(_refCount && (_refCount->dec() <= 0))
394  {
395  (*_refCount)(_ownedPtr);
396  delete _refCount;
397  _ptr = NULL;
398  }
399 }
400 
401 template<class T>
402 SharedPtr<T> &SharedPtr<T>::operator=(const SharedPtr<T> &ptr)
403 {
404  if (&ptr != this || ptr._ownedPtr != this->_ownedPtr || ptr._ptr != this->_ptr)
405  {
406  _remove();
407  _ownedPtr = ptr._ownedPtr;
408  _refCount = ptr._refCount;
409  _ptr = ptr._ptr;
410  if(_refCount)
411  _refCount->inc();
412  }
413  return *this;
414 }
415 
416 template<class T>
417 template<class U>
418 void SharedPtr<T>::reset(U* ptr)
419 {
420  if(ptr != this->_ownedPtr)
421  {
422  _remove();
423  _ownedPtr = ptr;
424  _ptr = ptr;
425  _refCount = (ptr ? new RefCounterImp<ObjectDeleter<U> >() : NULL);
426  }
427 }
428 
429 template<class T>
430 template<class U, class D>
431 void SharedPtr<T>::reset(U* ptr, const D& deleter)
432 {
433  if(ptr != this->_ownedPtr)
434  {
435  _remove();
436  _ownedPtr = ptr;
437  _ptr = ptr;
438  _refCount = (ptr ? new RefCounterImp<D>(deleter) : NULL);
439  }
440 }
441 
447 template<class T, class U>
448 SharedPtr<T> staticPointerCast(const SharedPtr<U> &r)
449 {
450  T *shifted = static_cast<T *>(r.get());
451  T *start = static_cast<T *>(r.getStartPtr());
452  return SharedPtr<T>(r, start, shifted);
453 }
454 
460 template<class T, class U>
461 SharedPtr<T> reinterpretPointerCast(const SharedPtr<U> &r)
462 {
463  T *shifted = reinterpret_cast<T *>(r.get());
464  T *start = reinterpret_cast<T *>(r.getStartPtr());
465  return SharedPtr<T>(r, start, shifted);
466 }
467 
473 template<class T, class U>
474 SharedPtr<T> dynamicPointerCast(const SharedPtr<U> &r)
475 {
476  T *shifted = dynamic_cast<T *>(r.get());
477  T *start = dynamic_cast<T *>(r.getStartPtr());
478  if (!r.get() || start)
479  {
480  return SharedPtr<T>(r, start, shifted);
481  }
482  else
483  {
484  return SharedPtr<T>();
485  }
486 }
488 } // namespace interface1
489 using interface1::DeleterIface;
490 using interface1::ObjectDeleter;
491 using interface1::EmptyDeleter;
492 using interface1::ServiceDeleter;
493 using interface1::RefCounter;
494 using interface1::RefCounterImp;
495 using interface1::SharedPtr;
496 using interface1::staticPointerCast;
497 using interface1::dynamicPointerCast;
498 using interface1::reinterpretPointerCast;
499 
500 } // namespace services;
501 } // namespace daal
502 
503 #endif
daal::services::interface1::dynamicPointerCast
SharedPtr< T > dynamicPointerCast(const SharedPtr< U > &r)
Definition: daal_shared_ptr.h:474
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:73
daal
Definition: algorithm_base_common.h:57
daal::services::interface1::RefCounterImp
Provides implementations of the operator() method of the RefCounter class.
Definition: daal_shared_ptr.h:157
daal::services::interface1::DeleterIface::operator()
virtual void operator()(const void *ptr)=0
daal::services::interface1::SharedPtr::SharedPtr
SharedPtr(U *ptr, const D &deleter)
Definition: daal_shared_ptr.h:220
daal::services::interface1::RefCounter::~RefCounter
virtual ~RefCounter()
Definition: daal_shared_ptr.h:146
daal::services::interface1::DeleterIface::~DeleterIface
virtual ~DeleterIface()
Definition: daal_shared_ptr.h:79
daal::services::interface1::SharedPtr::SharedPtr
SharedPtr(U *ptr)
Definition: daal_shared_ptr.h:206
daal::services::interface1::ObjectDeleter
Implementation of DeleterIface to destroy a pointer by the delete operator.
Definition: daal_shared_ptr.h:95
daal::services::interface1::SharedPtr
Shared pointer that retains shared ownership of an object through a pointer. Several SharedPtr object...
Definition: daal_shared_ptr.h:187
daal::services::interface1::SharedPtr::get
T * get() const
Definition: daal_shared_ptr.h:332
daal::services::interface1::RefCounter
Implementation of reference counter.
Definition: daal_shared_ptr.h:138
daal::services::interface1::SharedPtr::reset
void reset()
Definition: daal_shared_ptr.h:265
daal::services::interface1::RefCounterImp::RefCounterImp
RefCounterImp()
Definition: daal_shared_ptr.h:163
daal::services::interface1::SharedPtr::SharedPtr
SharedPtr()
Definition: daal_shared_ptr.h:197
daal::services::interface1::reinterpretPointerCast
SharedPtr< T > reinterpretPointerCast(const SharedPtr< U > &r)
Definition: daal_shared_ptr.h:461
daal::services::interface1::SharedPtr::operator->
T * operator->() const
Definition: daal_shared_ptr.h:314
daal::services::daal_free
DAAL_EXPORT void daal_free(void *ptr)
daal::services::interface1::ServiceDeleter
Implementation of DeleterIface to destroy a pointer by the daal_free function.
Definition: daal_shared_ptr.h:110
daal::services::interface1::SharedPtr::~SharedPtr
~SharedPtr()
Definition: daal_shared_ptr.h:254
daal::services::interface1::RefCounter::RefCounter
RefCounter()
Definition: daal_shared_ptr.h:144
daal::services::interface1::Atomic
Class that represents an atomic object.
Definition: daal_atomic_int.h:72
daal::services::interface1::SharedPtr::useCount
int useCount() const
Definition: daal_shared_ptr.h:344
daal::services::interface1::RefCounterImp::~RefCounterImp
virtual ~RefCounterImp()
Definition: daal_shared_ptr.h:166
daal::services::interface1::SharedPtr::operator=
SharedPtr< T > & operator=(const SharedPtr< U > &ptr)
Definition: daal_shared_ptr.h:296
daal::services::interface1::SharedPtr::getStartPtr
T * getStartPtr() const
Definition: daal_shared_ptr.h:338
daal::services::interface1::EmptyDeleter
Implementation of DeleterIface without pointer destroying.
Definition: daal_shared_ptr.h:125
daal::services::interface1::staticPointerCast
SharedPtr< T > staticPointerCast(const SharedPtr< U > &r)
Definition: daal_shared_ptr.h:448

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