taco-db  0.1.0
ResourceGuard.h
Go to the documentation of this file.
1 #ifndef UTILS_RESOURCEGUARD_H
2 #define UTILS_RESOURCEGUARD_H
3 
4 #include "tdb.h"
5 
6 namespace taco {
7 
59 template<class T,
60  class Relinquish,
61  class FlagType = bool,
62  FlagType InvalidVal = std::is_lvalue_reference<T>::value,
63  class = typename std::enable_if<
64  // T may not be bool
65  !std::is_same<FlagType, bool>::value &&
66  // FlagType must be bool, same as T
67  (std::is_same<FlagType, bool>::value ||
68  std::is_same<FlagType, T>::value) &&
69  // Relinquish must be trivially default constructible
70  std::is_trivially_default_constructible<Relinquish>::value
71  >::type>
73 public:
74  ResourceGuard(): m_val(InvalidVal) {}
75 
76  ResourceGuard(T val): m_val(val) {}
77 
79  if (m_val != InvalidVal)
80  Relinquish()(m_val);
81  }
82 
83  // no copy
84  ResourceGuard(const ResourceGuard&) = delete;
85  ResourceGuard& operator=(const ResourceGuard&) = delete;
86 
87  // move ctor and assignment
89  m_val(other.m_val) {
90  other.m_val = InvalidVal;
91  }
92 
94  if (m_val != InvalidVal) {
95  Relinquish()(m_val);
96  }
97  m_val = other.m_val;
98  other.m_val = InvalidVal;
99  return *this;
100  }
101 
102  operator T() const {
103  return m_val;
104  }
105 
106  T
107  Get() const {
108  return m_val;
109  }
110 
111  operator bool() const {
112  return m_val != InvalidVal;
113  }
114 
115  bool
116  IsValid() const {
117  return m_val != InvalidVal;
118  }
119 
123  void
124  Reset() {
125  if (m_val != InvalidVal) {
126  Relinquish()(m_val);
127  m_val = InvalidVal;
128  }
129  }
130 
136  T
138  T ret = m_val;
139  m_val = InvalidVal;
140  return ret;
141  }
142 
143  constexpr bool
144  operator==(T rhs) const {
145  return m_val == rhs;
146  }
147 
148  constexpr bool
149  operator!=(T rhs) const {
150  return m_val != rhs;
151  }
152 
153 private:
154  T m_val;
155 };
156 
161 template<class T, class Relinquish>
163 public:
164  ResourceGuard(): m_isvalid(false) {}
165 
167  m_val(val), m_isvalid(true) {}
168 
170  if (m_isvalid)
171  Relinquish()(m_val);
172  }
173 
174  // no copy
175  ResourceGuard(const ResourceGuard&) = delete;
176  ResourceGuard& operator=(const ResourceGuard&) = delete;
177 
178  // move ctor and assignment
180  m_val(other.m_val),
181  m_isvalid(other.m_isvalid) {
182  other.m_val = T();
183  other.m_isvalid = false;
184  }
185 
187  if (m_isvalid) {
188  Relinquish()(m_val);
189  }
190  m_val = other.m_val;
191  m_isvalid = other.m_isvalid;
192  other.m_val = T();
193  other.m_isvalid = false;
194  return *this;
195  }
196 
197  operator T() const {
198  return m_val;
199  }
200 
201  T
202  Get() const {
203  return m_val;
204  }
205 
206  operator bool() const {
207  return m_isvalid;
208  }
209 
210  bool
211  IsValid() const {
212  return m_isvalid;
213  }
214 
215  void
216  Reset() {
217  if (m_isvalid) {
218  Relinquish()(m_val);
219  m_val = T();
220  m_isvalid = false;
221  }
222  }
223 
224  T
226  T ret = m_val;
227  m_val = T();
228  m_isvalid = false;
229  return ret;
230  }
231 
232  constexpr bool
233  operator==(T rhs) const {
234  return m_val == rhs;
235  }
236 
237  constexpr bool
238  operator!=(T rhs) const {
239  return m_val != rhs;
240  }
241 
242 
243 private:
244  T m_val;
245  bool m_isvalid;
246 };
247 
253 template<class T, class Relinquish>
255 public:
256  // no default-ctor
257 
259  m_val(val) {}
260 
262  Relinquish()(m_val);
263  }
264 
265  // no copy
266  ResourceGuard(const ResourceGuard&) = delete;
267  ResourceGuard& operator=(const ResourceGuard&) = delete;
268 
269  // no move
270  ResourceGuard(ResourceGuard&& other) = delete;
271  ResourceGuard& operator=(ResourceGuard&& other) = delete;
272 
273  operator T() const {
274  return m_val;
275  }
276 
277  T
278  Get() const {
279  return m_val;
280  }
281 
282  operator bool() const {
283  return true;
284  }
285 
286  bool
287  IsValid() const {
288  return true;
289  }
290 
291  constexpr bool
292  operator==(T rhs) const {
293  return m_val == rhs;
294  }
295 
296  constexpr bool
297  operator!=(T rhs) const {
298  return m_val != rhs;
299  }
300 
301 private:
302  T m_val;
303 };
304 
305 } // namespace stdb
306 
307 #endif // UTILS_RESOURCEGUARD_H
taco::ResourceGuard< T, Relinquish, bool, false, void >::operator!=
constexpr bool operator!=(T rhs) const
Definition: ResourceGuard.h:238
taco::ResourceGuard< T, Relinquish, bool, false, void >::m_isvalid
bool m_isvalid
Definition: ResourceGuard.h:245
taco::ResourceGuard::IsValid
bool IsValid() const
Definition: ResourceGuard.h:116
taco::ResourceGuard< T, Relinquish, bool, false, void >::ResourceGuard
ResourceGuard()
Definition: ResourceGuard.h:164
taco::ResourceGuard< T, Relinquish, bool, true, void >::ResourceGuard
ResourceGuard(T val)
Definition: ResourceGuard.h:258
taco::ResourceGuard::Release
T Release()
Releases the underlying resource without relinquishing it and returns it to the caller.
Definition: ResourceGuard.h:137
taco::ResourceGuard< T, Relinquish, bool, true, void >::~ResourceGuard
~ResourceGuard()
Definition: ResourceGuard.h:261
taco
Definition: datum.h:28
taco::ResourceGuard< T, Relinquish, bool, false, void >::~ResourceGuard
~ResourceGuard()
Definition: ResourceGuard.h:169
taco::ResourceGuard< T, Relinquish, bool, false, void >::Reset
void Reset()
Definition: ResourceGuard.h:216
taco::ResourceGuard< T, Relinquish, bool, false, void >::ResourceGuard
ResourceGuard(T val)
Definition: ResourceGuard.h:166
taco::ResourceGuard< T, Relinquish, bool, false, void >::ResourceGuard
ResourceGuard(ResourceGuard &&other)
Definition: ResourceGuard.h:179
taco::ResourceGuard< T, Relinquish, bool, true, void >::operator==
constexpr bool operator==(T rhs) const
Definition: ResourceGuard.h:292
tdb.h
taco::ResourceGuard::ResourceGuard
ResourceGuard(ResourceGuard &&other)
Definition: ResourceGuard.h:88
taco::ResourceGuard< T, Relinquish, bool, true, void >
Specialization of ResourceGuard where we have an always-valid value (e.g., lvalue-ref).
Definition: ResourceGuard.h:254
taco::ResourceGuard::operator!=
constexpr bool operator!=(T rhs) const
Definition: ResourceGuard.h:149
taco::ResourceGuard< T, Relinquish, bool, false, void >::Release
T Release()
Definition: ResourceGuard.h:225
taco::ResourceGuard::ResourceGuard
ResourceGuard()
Definition: ResourceGuard.h:74
taco::ResourceGuard< T, Relinquish, bool, false, void >::Get
T Get() const
Definition: ResourceGuard.h:202
taco::ResourceGuard< T, Relinquish, bool, false, void >::m_val
T m_val
Definition: ResourceGuard.h:244
taco::ResourceGuard< T, Relinquish, bool, true, void >::Get
T Get() const
Definition: ResourceGuard.h:278
taco::ResourceGuard::operator==
constexpr bool operator==(T rhs) const
Definition: ResourceGuard.h:144
taco::ResourceGuard< T, Relinquish, bool, false, void >::IsValid
bool IsValid() const
Definition: ResourceGuard.h:211
taco::ResourceGuard::operator=
ResourceGuard & operator=(ResourceGuard &&other)
Definition: ResourceGuard.h:93
taco::ResourceGuard< T, Relinquish, bool, true, void >::operator!=
constexpr bool operator!=(T rhs) const
Definition: ResourceGuard.h:297
taco::ResourceGuard
ResourceGuard is used for automatically relinquishes some resource when it goes out of scope.
Definition: ResourceGuard.h:72
taco::ResourceGuard< T, Relinquish, bool, false, void >
Specialization of ResourceGuard when we use an additional boolean flag to denote the invalid value.
Definition: ResourceGuard.h:162
taco::ResourceGuard::ResourceGuard
ResourceGuard(T val)
Definition: ResourceGuard.h:76
taco::ResourceGuard::~ResourceGuard
~ResourceGuard()
Definition: ResourceGuard.h:78
taco::ResourceGuard::Get
T Get() const
Definition: ResourceGuard.h:107
taco::ResourceGuard< T, Relinquish, bool, false, void >::operator==
constexpr bool operator==(T rhs) const
Definition: ResourceGuard.h:233
taco::ResourceGuard< T, Relinquish, bool, true, void >::IsValid
bool IsValid() const
Definition: ResourceGuard.h:287
taco::ResourceGuard< T, Relinquish, bool, false, void >::operator=
ResourceGuard & operator=(ResourceGuard &&other)
Definition: ResourceGuard.h:186
taco::ResourceGuard< T, Relinquish, bool, true, void >::m_val
T m_val
Definition: ResourceGuard.h:302
taco::ResourceGuard::m_val
T m_val
Definition: ResourceGuard.h:154
taco::ResourceGuard::Reset
void Reset()
Relinquishes the underlying resource and sets its to an invalid value.
Definition: ResourceGuard.h:124