SharedPtr.hpp
1 #pragma once
2 
3 #include <opals/config.hpp>
4 #include <opals/ObjectBase.hpp>
5 #include <opals/fwd.hpp>
6 
7 #pragma once
8 
9 namespace opals
10 {
11  struct SharedPtrImpl;
12 
13  OPALS_API void shared_ptr_create(SharedPtrImpl *&, void *obj, void(*deleter)(void *));
14  OPALS_API void shared_ptr_create(SharedPtrImpl *&, ObjectBase *obj);
15  OPALS_API void shared_ptr_inc(SharedPtrImpl *&);
16  OPALS_API void shared_ptr_dec(SharedPtrImpl *&);
17  OPALS_API unsigned int shared_ptr_get_count(SharedPtrImpl * const &);
18  OPALS_API void* shared_ptr_get(SharedPtrImpl * const &);
19  OPALS_API void* shared_ptr_release(SharedPtrImpl *&);
20 
21  /** \brief smart pointer class for opals objects
22 
23  Smart pointers uses a reference counting mechanism to control the life time of an object. When the last
24  SharePtr instance goes out of scope, the controlled object is deleted. In case an object is shared across
25  dll boundaries, it is essential create and delete objects within the same heap. To do so you can either
26  provide a corresponding delete function (only non-member functions are supported) or an object is derived
27  from ObjectBase and the (overloaded) Delete function is called to delete the object.
28 
29  \author JO
30  \date 29.01.2019
31  */
32  template <typename T>
33  class OPALS_API SharedPtr
34  {
35  SharedPtrImpl *pimpl_;
36 
37  public:
38  SharedPtr() noexcept : pimpl_(0) {}
39  /// Put an object that is derived from ObjectBase under the control of the smart pointer class
40  SharedPtr(ObjectBase *obj) noexcept : pimpl_(0) { shared_ptr_create(pimpl_, obj); }
41  /// Put an object under the control of the smart pointer class with the corresponding deleter function pointer
42  SharedPtr(T *obj, void(*deleter)(void *)) noexcept : pimpl_(0) { shared_ptr_create(pimpl_, obj, deleter); }
43  SharedPtr(const SharedPtr &ref) noexcept : pimpl_(ref.pimpl_) { shared_ptr_inc(pimpl_); }
44 
45  ~SharedPtr() noexcept { shared_ptr_dec(pimpl_); }
46 
47  SharedPtr &operator=(const SharedPtr &h) {
48  SharedPtr tmp = h;
49  swap(tmp);
50  return *this;
51  }
52 
53  /// reset shared pointer and delete controlled object if necessary
54  void reset() noexcept {
55  SharedPtr tmp;
56  swap(tmp);
57  }
58  /// put a new ObjectBase object under the shared pointer control
59  void reset(ObjectBase *obj) noexcept {
60  SharedPtr tmp(obj);
61  swap(tmp);
62  }
63  /// put a new object under the shared pointer control (with the corresponding deleter function pointer)
64  void reset(T *obj, void(*deleter)(void *)) noexcept {
65  SharedPtr tmp(obj, deleter);
66  swap(tmp);
67  }
68  void swap(SharedPtr &h) noexcept {
69  std::swap(pimpl_, h.pimpl_);
70  }
71  /// get pointer to controlled object
72  T* get() const noexcept { return (T *)shared_ptr_get(pimpl_); }
73 
74  const T& operator*() const { return *get(); }
75  T& operator*() { return *get(); }
76 
77  const T* operator->() const { return get(); }
78  T* operator->() { return get(); }
79 
80  /// get number of SharedPtr instances that reference the same object
81  unsigned int use_count() const noexcept { return shared_ptr_get_count(pimpl_); }
82  /// is the controlled object only referenced by this SharedPtr instance
83  bool unique() const noexcept { return (shared_ptr_get_count(pimpl_) == 1); }
84 
85  operator bool() const noexcept { return (pimpl_ != 0); }
86 
87  /// \brief release the controlled object
88  /// This is only possible if the controlled object is only referenced by the current SharedPtr instance.
89  /// The user has to take care of deleting the object.
90  T* release() {
91  if (unique() == true)
92  return (T*)shared_ptr_release(pimpl_);
93  else
94  return (T*)0;
95  }
96  };
97 
98 }
unsigned int use_count() const noexcept
get number of SharedPtr instances that reference the same object
Definition: SharedPtr.hpp:81
T * release()
release the controlled object This is only possible if the controlled object is only referenced by th...
Definition: SharedPtr.hpp:90
void reset(ObjectBase *obj) noexcept
put a new ObjectBase object under the shared pointer control
Definition: SharedPtr.hpp:59
bool unique() const noexcept
is the controlled object only referenced by this SharedPtr instance
Definition: SharedPtr.hpp:83
smart pointer class for opals objects
Definition: SharedPtr.hpp:33
Contains the public interface of OPALS.
Definition: AbsValueOrQuantile.hpp:8
T * get() const noexcept
get pointer to controlled object
Definition: SharedPtr.hpp:72
SharedPtr(ObjectBase *obj) noexcept
Put an object that is derived from ObjectBase under the control of the smart pointer class.
Definition: SharedPtr.hpp:40
void reset(T *obj, void(*deleter)(void *)) noexcept
put a new object under the shared pointer control (with the corresponding deleter function pointer)
Definition: SharedPtr.hpp:64
void reset() noexcept
reset shared pointer and delete controlled object if necessary
Definition: SharedPtr.hpp:54
SharedPtr(T *obj, void(*deleter)(void *)) noexcept
Put an object under the control of the smart pointer class with the corresponding deleter function po...
Definition: SharedPtr.hpp:42
base class for objects which are controlled using the SharedPtr class. the virutal Delete function is...
Definition: c++_api/inc/opals/ObjectBase.hpp:9