8#ifndef META_OCEAN_BASE_SCOPED_OBJECT_H
9#define META_OCEAN_BASE_SCOPED_OBJECT_H
26template <
typename T,
typename TReleaseValue = T,
typename TReleaseFunction =
void(*)(TReleaseValue)>
56 ScopedObjectT(T&&
object, TReleaseFunction&& releaseFunction,
const bool useReleaseFunction)
noexcept;
63 ScopedObjectT(
const T&
object, TReleaseFunction&& releaseFunction)
noexcept;
72 ScopedObjectT(
const T&
object, TReleaseFunction&& releaseFunction,
const bool useReleaseFunction)
noexcept;
154template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue = NotVo
idTyper<TReleaseReturn>::defaultValue(),
bool tCheckReturnValue = true, T tInval
idValue = T()>
248 ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>&
operator=(
ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>&& scopedObject)
noexcept;
279template <
typename T,
void (*tReleaseFunction)(T)>
282template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
285 *
this = std::move(scopedObject);
288template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
290 object_(std::move(
object)),
291 releaseFunction_(std::move(releaseFunction))
296template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
298 object_(std::move(
object))
300 if (useReleaseFunction)
302 releaseFunction_ = std::move(releaseFunction);
306template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
309 releaseFunction_(std::move(releaseFunction))
314template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
318 if (useReleaseFunction)
320 releaseFunction_ = std::move(releaseFunction);
324template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
330template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
333 return bool(releaseFunction_);
336template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
342template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
345 ocean_assert(isValid());
350template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
353 ocean_assert(isValid());
358template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
361 if (releaseFunction_)
363 releaseFunction_(TReleaseValue(object_));
366 releaseFunction_ = TReleaseFunction();
370template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
373 if (
this != &scopedObject)
377 object_ = std::move(scopedObject.object_);
378 scopedObject.object_ = T();
380 releaseFunction_ = std::move(scopedObject.releaseFunction_);
381 scopedObject.releaseFunction_ = TReleaseFunction();
387template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
388ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>::ScopedObjectCompileTimeT(
ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>&& scopedObject)
noexcept
390 *
this = std::move(scopedObject);
393template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
395 object_(std::move(
object))
397 needsRelease_ = object_ != tInvalidValue;
400template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
402 object_(std::move(
object)),
403 needsRelease_(needsRelease)
408template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
415template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
418 needsRelease_(needsRelease)
423template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
429template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
432 return object_ != tInvalidValue;
435template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
441template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
444 ocean_assert(isValid());
449template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
452 ocean_assert(isValid());
457template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
461 object_ = tInvalidValue;
463 needsRelease_ = needsRelease;
468template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
471 if (needsRelease_ && object_ != tInvalidValue)
473 if constexpr (std::is_same<TReleaseReturn, void>::value)
475 tReleaseFunction(TReleaseValue(object_));
479 if constexpr (tCheckReturnValue)
481 const TReleaseReturn returnValue = tReleaseFunction(TReleaseValue(object_));
482 ocean_assert_and_suppress_unused(returnValue == tExpectedReturnValue, returnValue);
486 const TReleaseReturn returnValue = tReleaseFunction(TReleaseValue(object_));
487 OCEAN_SUPPRESS_UNUSED_WARNING(returnValue);
491 object_ = tInvalidValue;
492 needsRelease_ =
false;
496template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
497ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>&
ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>::operator=(
ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>&& scopedObject)
noexcept
499 if (
this != &scopedObject)
503 object_ = std::move(scopedObject.object_);
504 scopedObject.object_ = tInvalidValue;
506 needsRelease_ = scopedObject.needsRelease_;
507 scopedObject.needsRelease_ =
false;
This class wraps an unmanaged object (or reference) which needs to be released after usage.
Definition ScopedObject.h:156
ScopedObjectCompileTimeT & operator=(const ScopedObjectCompileTimeT< T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue > &)=delete
Disabled assign operator.
bool needsRelease_
True, if the wrapped object needs to be released.
Definition ScopedObject.h:269
ScopedObjectCompileTimeT(T &&object, const bool needsRelease) noexcept
Creates a new scoped object.
Definition ScopedObject.h:401
bool isValid() const
Returns whether this scoped object holds a valid object.
Definition ScopedObject.h:430
ScopedObjectCompileTimeT(T &&object) noexcept
Creates a new scoped object.
Definition ScopedObject.h:394
ScopedObjectCompileTimeT(const T &object, const bool needsRelease)
Creates a new scoped object.
Definition ScopedObject.h:416
ScopedObjectCompileTimeT()=default
Default constructor creating an object with invalid object.
const T & object() const
Returns the wrapped object.
Definition ScopedObject.h:436
ScopedObjectCompileTimeT(const ScopedObjectCompileTimeT< T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue > &)=delete
Disabled copy constructor.
void release()
Explicitly releases the wrapped object.
Definition ScopedObject.h:469
ScopedObjectCompileTimeT< T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue > & operator=(ScopedObjectCompileTimeT< T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue > &&scopedObject) noexcept
Move operator.
Definition ScopedObject.h:497
T object_
The wrapped reference.
Definition ScopedObject.h:266
const T & operator*() const
De-reference operator returning the wrapped object.
Definition ScopedObject.h:450
ScopedObjectCompileTimeT(ScopedObjectCompileTimeT< T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue > &&scopedObject) noexcept
Move constructor.
Definition ScopedObject.h:388
~ScopedObjectCompileTimeT()
Destructs this scoped object and releases the internal wrapped object.
Definition ScopedObject.h:424
ScopedObjectCompileTimeT(const T &object)
Creates a new scoped object.
Definition ScopedObject.h:409
T & resetObject(const bool needsRelease=true)
Releases the current wrapped object and returns a new wrapped object.
Definition ScopedObject.h:458
const T & operator->() const
Returns the wrapped object.
Definition ScopedObject.h:442
This class wraps an unmanaged object (or reference) which needs to be released after usage.
Definition ScopedObject.h:28
ScopedObjectT(T &&object, TReleaseFunction &&releaseFunction) noexcept
Creates a new scoped object.
Definition ScopedObject.h:289
ScopedObjectT< T, TReleaseValue, TReleaseFunction > & operator=(const ScopedObjectT< T, TReleaseValue, TReleaseFunction > &)=delete
Disabled assign operator.
ScopedObjectT(T &&object, TReleaseFunction &&releaseFunction, const bool useReleaseFunction) noexcept
Creates a new scoped object.
Definition ScopedObject.h:297
T object_
The wrapped reference.
Definition ScopedObject.h:135
const T & operator*() const
De-reference operator returning the wrapped object.
Definition ScopedObject.h:351
void release()
Explicitly releases the wrapped object.
Definition ScopedObject.h:359
~ScopedObjectT()
Destructs this scoped object and releases the internal wrapped object.
Definition ScopedObject.h:325
ScopedObjectT(const T &object, TReleaseFunction &&releaseFunction, const bool useReleaseFunction) noexcept
Creates a new scoped object.
Definition ScopedObject.h:315
TReleaseFunction releaseFunction_
The function used to release the wrapped object.
Definition ScopedObject.h:138
ScopedObjectT()=default
Default constructor creating an object with invalid object.
const T & operator->() const
Arrow operator returning the wrapped object.
Definition ScopedObject.h:343
ScopedObjectT< T, TReleaseValue, TReleaseFunction > & operator=(ScopedObjectT< T, TReleaseValue, TReleaseFunction > &&scopedObject) noexcept
Move operator.
Definition ScopedObject.h:371
ScopedObjectT(const ScopedObjectT< T, TReleaseValue, TReleaseFunction > &)=delete
Disabled copy constructor.
bool isValid() const
Returns whether this scoped object holds a valid release function (which will be invoked once the obj...
Definition ScopedObject.h:331
const T & object() const
Returns the wrapped object.
Definition ScopedObject.h:337
ScopedObjectT(ScopedObjectT< T, TReleaseValue, TReleaseFunction > &&scopedObject) noexcept
Move constructor.
Definition ScopedObject.h:283
ScopedObjectT(const T &object, TReleaseFunction &&releaseFunction) noexcept
Creates a new scoped object.
Definition ScopedObject.h:307
The namespace covering the entire Ocean framework.
Definition Accessor.h:15