pianod2
multisource multiuser scriptable networked music player
retainer.h
Go to the documentation of this file.
1 
9 #pragma once
10 
12 template <typename ptr_type>
13 class Retainer {
14  static_assert (std::is_pointer <ptr_type>::value, "Retained type must be a pointer.");
15  using value_type = typename std::remove_pointer <ptr_type>::type;
16  static_assert (std::is_base_of <MusicThingie, value_type>::value, "Retained value type must derive from MusicThingie");
18 private:
19  value_type *value {nullptr};
20 public:
22  inline Retainer () {};
23 
25  inline Retainer (value_type *item) : value (item) {
26  if (value) {
27  value->retain();
28  }
29  }
30 
32  inline Retainer (const this_type &item) : value (item.value) {
33  if (value) {
34  value->retain();
35  }
36  }
37 
39  template <typename OtherType>
40  inline Retainer (const Retainer<OtherType> &item) : value (item.get()) {
41  if (value) {
42  value->retain();
43  }
44  }
45 
47  inline Retainer (this_type &&item) : value (item.value) {
48  item.value = nullptr;
49  }
50 
52  template <typename OtherType>
53  inline Retainer (Retainer <OtherType> &&item) : value (item.value) {
54  item.value = nullptr;
55  }
56 
58  inline this_type &operator= (value_type *item) {
59  if (value != item) {
60  if (value) {
61  value->release();
62  }
63  value = item;
64  if (value) {
65  value->retain();
66  }
67  }
68  return *this;
69  }
70 
72  inline value_type &operator= (const this_type &item) {
73  *this = item.value;
74  return *this;
75  }
76 
78  template <typename OtherType>
79  inline this_type &operator= (const Retainer <OtherType> &item) {
80  *this = item.value;
81  return *this;
82  }
83 
85  inline this_type &operator= (this_type &&item) {
86  if (value != item.value) {
87  if (value) {
88  value->release();
89  }
90  value = item.value;
91  item.value = nullptr;
92  }
93  return *this;
94  }
95 
97  template <typename OtherType>
99  if (value != item.value) {
100  if (value) {
101  value->release();
102  }
103  value = item.value;
104  item.value = nullptr;
105  }
106  return *this;
107  }
108 
110  inline ~Retainer () {
111  if (value) {
112  value->release();
113  }
114  }
115 
117  inline value_type *operator->() const {
118  assert (value);
119  return value;
120  }
121 
123  inline value_type &operator*() const {
124  return *value;
125  }
126 
128  inline value_type *get() const {
129  return value;
130  }
131 
134  inline operator bool() const {
135  return (value != nullptr);
136  }
137 };
Smart containers to help manage reference counting the MusicThingies.
Definition: retainer.h:13
Retainer(const this_type &item)
Copy constructor.
Definition: retainer.h:32
value_type * get() const
Get the pointer itself.
Definition: retainer.h:128
this_type & operator=(value_type *item)
Direct assignment of new pointer.
Definition: retainer.h:58
~Retainer()
On destruction, release any contained pointer.
Definition: retainer.h:110
value_type * value
Definition: retainer.h:19
Retainer(this_type &&item)
Move construction.
Definition: retainer.h:47
Retainer()
Default constructor.
Definition: retainer.h:22
value_type & operator*() const
Dereference contained pointer via operator *.
Definition: retainer.h:123
Retainer(const Retainer< OtherType > &item)
Copy construct from a different retained type.
Definition: retainer.h:40
typename std::remove_pointer< ptr_type >::type value_type
Definition: retainer.h:15
Retainer(value_type *item)
Direct construction of new item via pointer.
Definition: retainer.h:25
value_type * operator->() const
Allow arrow use of contained pointer.
Definition: retainer.h:117
Retainer(Retainer< OtherType > &&item)
Move construction from a different retained type.
Definition: retainer.h:53
uint32_t value
Definition: audiooutput.cpp:68