Om
shareable.cpp
Go to the documentation of this file.
1 
15 #ifndef Om_Shareable_
16 
17  #include "om/shareable.hpp"
18 
19 #else
20 
21  #ifndef Om_Macro_Precompilation_
22 
23  #include <stdexcept>
24  #include "boost/checked_delete.hpp"
25  #include "boost/integer_traits.hpp"
26 
27  #endif
28 
29 // MARK: - Om::Shareable
30 
31  #define Template_ \
32  template <typename ThisOwnerCount>
33 
34  #define Type_ \
35  Om::Shareable<ThisOwnerCount>
36 
37 // MARK: public (non-static)
38 
39 Template_
40 inline Type_::~Shareable() {
41  assert(
42  !this->thisOwnerCount &&
43  "Non-zero owner count."
44  );
45 }
46 
47 Template_
48 inline ThisOwnerCount Type_::GetOwnerCount() const {
49  return this->thisOwnerCount;
50 }
51 
52 // MARK: protected (non-static)
53 
54 Template_
55 inline Type_::Shareable():
56 thisOwnerCount() {}
57 
58 Template_
59 inline Type_::Shareable(Shareable const &):
60 thisOwnerCount() {}
61 
62 Template_
63 inline Type_ & Type_::operator =(Shareable const &) {
64  return *this;
65 }
66 
67 // MARK: private (non-static)
68 
69 Template_
70 inline void Type_::DecrementOwnerCount() {
71  assert(
72  this->thisOwnerCount &&
73  "Owner count underflow."
74  );
75  --this->thisOwnerCount;
76 }
77 
78 Template_
79 inline void Type_::IncrementOwnerCount() {
80  if (
81  boost::integer_traits<ThisOwnerCount>::const_max == this->thisOwnerCount
82  ) {
83  throw std::overflow_error("Owner count overflow.");
84  }
85  ++this->thisOwnerCount;
86 }
87 
88  #undef Type_
89  #undef Template_
90 
91 // MARK: - Om::
92 
93 template <typename TheOwnerCount>
94 inline void Om::intrusive_ptr_add_ref(
95  Shareable<TheOwnerCount> * const thePointee
96 ) {
97  assert(
98  thePointee &&
99  "The pointer cannot be null."
100  );
101  thePointee->IncrementOwnerCount();
102 }
103 
104 template <typename TheOwnerCount>
105 inline void Om::intrusive_ptr_release(
106  Shareable<TheOwnerCount> * const thePointee
107 ) {
108  assert(
109  thePointee &&
110  "The pointer cannot be null."
111  );
112  thePointee->DecrementOwnerCount();
113  if (!thePointee->thisOwnerCount) {
114  boost::checked_delete(thePointee);
115  }
116 }
117 
118 #endif
void intrusive_ptr_add_ref(Shareable< TheOwnerCount > *const)
void intrusive_ptr_release(Shareable< TheOwnerCount > *const)
Om header file.