8 #ifndef META_OCEAN_BASE_SCOPED_OBJECT_H
9 #define META_OCEAN_BASE_SCOPED_OBJECT_H
26 template <
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;
119 #ifdef OCEAN_ENABLE_CAST_OPERATOR_FOR_SCOPED_OBJECT
125 operator const T&()
const;
164 template <
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()>
258 ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>&
operator=(
ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>&& scopedObject) noexcept;
260 #ifdef OCEAN_ENABLE_CAST_OPERATOR_FOR_SCOPED_OBJECT
266 operator const T&()
const;
299 template <
typename T,
void (*tReleaseFunction)(T)>
302 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
305 *
this = std::move(scopedObject);
308 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
310 object_(std::move(
object)),
311 releaseFunction_(std::move(releaseFunction))
316 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
318 object_(std::move(
object))
320 if (useReleaseFunction)
322 releaseFunction_ = std::move(releaseFunction);
326 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
329 releaseFunction_(std::move(releaseFunction))
334 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
338 if (useReleaseFunction)
340 releaseFunction_ = std::move(releaseFunction);
344 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
350 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
353 return bool(releaseFunction_);
356 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
362 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
365 ocean_assert(isValid());
370 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
373 ocean_assert(isValid());
378 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
381 if (releaseFunction_)
383 releaseFunction_(TReleaseValue(object_));
386 releaseFunction_ = TReleaseFunction();
390 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
393 if (
this != &scopedObject)
397 object_ = std::move(scopedObject.object_);
398 scopedObject.object_ = T();
400 releaseFunction_ = std::move(scopedObject.releaseFunction_);
401 scopedObject.releaseFunction_ = TReleaseFunction();
407 #ifdef OCEAN_ENABLE_CAST_OPERATOR_FOR_SCOPED_OBJECT
409 template <
typename T,
typename TReleaseValue,
typename TReleaseFunction>
417 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
418 ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>::ScopedObjectCompileTimeT(
ScopedObjectCompileTimeT<T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue>&& scopedObject) noexcept
420 *
this = std::move(scopedObject);
423 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
425 object_(std::move(
object))
427 needsRelease_ = object_ != tInvalidValue;
430 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
432 object_(std::move(
object)),
433 needsRelease_(needsRelease)
438 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
445 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
448 needsRelease_(needsRelease)
453 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
459 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
462 return object_ != tInvalidValue;
465 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
471 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
474 ocean_assert(isValid());
479 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
482 ocean_assert(isValid());
487 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
491 object_ = tInvalidValue;
493 needsRelease_ = needsRelease;
498 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
503 if constexpr (std::is_same<TReleaseReturn, void>::value)
505 tReleaseFunction(TReleaseValue(object_));
509 if constexpr (tCheckReturnValue)
511 const TReleaseReturn returnValue = tReleaseFunction(TReleaseValue(object_));
512 ocean_assert_and_suppress_unused(returnValue == tExpectedReturnValue, returnValue);
516 const TReleaseReturn returnValue = tReleaseFunction(TReleaseValue(object_));
517 OCEAN_SUPPRESS_UNUSED_WARNING(returnValue);
521 object_ = tInvalidValue;
522 needsRelease_ =
false;
526 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
527 ScopedObjectCompileTimeT<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
529 if (
this != &scopedObject)
533 object_ = std::move(scopedObject.object_);
534 scopedObject.object_ = tInvalidValue;
536 needsRelease_ = scopedObject.needsRelease_;
537 scopedObject.needsRelease_ =
false;
543 #ifdef OCEAN_ENABLE_CAST_OPERATOR_FOR_SCOPED_OBJECT
545 template <
typename T,
typename TReleaseValue,
typename TReleaseReturn, TReleaseReturn (*tReleaseFunction)(TReleaseValue),
typename NotVo
idTyper<TReleaseReturn>::Type tExpectedReturnValue,
bool tCheckReturnValue, T tInval
idValue>
This class wraps an unmanaged object (or reference) which needs to be released after usage.
Definition: ScopedObject.h:166
bool needsRelease_
True, if the wrapped object needs to be released.
Definition: ScopedObject.h:289
ScopedObjectCompileTimeT & operator=(const ScopedObjectCompileTimeT< T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue > &)=delete
Disabled assign operator.
ScopedObjectCompileTimeT(T &&object, const bool needsRelease) noexcept
Creates a new scoped object.
Definition: ScopedObject.h:431
bool isValid() const
Returns whether this scoped object holds a valid object.
Definition: ScopedObject.h:460
ScopedObjectCompileTimeT(T &&object) noexcept
Creates a new scoped object.
Definition: ScopedObject.h:424
ScopedObjectCompileTimeT(const T &object, const bool needsRelease)
Creates a new scoped object.
Definition: ScopedObject.h:446
ScopedObjectCompileTimeT()=default
Default constructor creating an object with invalid object.
const T & object() const
Returns the wrapped object.
Definition: ScopedObject.h:466
ScopedObjectCompileTimeT(const ScopedObjectCompileTimeT< T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue > &)=delete
Disabled copy constructor.
void release()
Explicitly releases the wrapped object.
Definition: ScopedObject.h:499
ScopedObjectCompileTimeT< T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue > & operator=(ScopedObjectCompileTimeT< T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue > &&scopedObject) noexcept
Move operator.
Definition: ScopedObject.h:527
T object_
The wrapped reference.
Definition: ScopedObject.h:286
const T & operator*() const
De-reference operator returning the wrapped object.
Definition: ScopedObject.h:480
ScopedObjectCompileTimeT(ScopedObjectCompileTimeT< T, TReleaseValue, TReleaseReturn, tReleaseFunction, tExpectedReturnValue, tCheckReturnValue, tInvalidValue > &&scopedObject) noexcept
Move constructor.
Definition: ScopedObject.h:418
~ScopedObjectCompileTimeT()
Destructs this scoped object and releases the internal wrapped object.
Definition: ScopedObject.h:454
ScopedObjectCompileTimeT(const T &object)
Creates a new scoped object.
Definition: ScopedObject.h:439
T & resetObject(const bool needsRelease=true)
Releases the current wrapped object and returns a new wrapped object.
Definition: ScopedObject.h:488
const T & operator->() const
Returns the wrapped object.
Definition: ScopedObject.h:472
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:309
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:317
T object_
The wrapped reference.
Definition: ScopedObject.h:145
const T & operator*() const
De-reference operator returning the wrapped object.
Definition: ScopedObject.h:371
void release()
Explicitly releases the wrapped object.
Definition: ScopedObject.h:379
~ScopedObjectT()
Destructs this scoped object and releases the internal wrapped object.
Definition: ScopedObject.h:345
ScopedObjectT(const T &object, TReleaseFunction &&releaseFunction, const bool useReleaseFunction) noexcept
Creates a new scoped object.
Definition: ScopedObject.h:335
TReleaseFunction releaseFunction_
The function used to release the wrapped object.
Definition: ScopedObject.h:148
ScopedObjectT()=default
Default constructor creating an object with invalid object.
const T & operator->() const
Arrow operator returning the wrapped object.
Definition: ScopedObject.h:363
ScopedObjectT< T, TReleaseValue, TReleaseFunction > & operator=(ScopedObjectT< T, TReleaseValue, TReleaseFunction > &&scopedObject) noexcept
Move operator.
Definition: ScopedObject.h:391
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:351
const T & object() const
Returns the wrapped object.
Definition: ScopedObject.h:357
ScopedObjectT(ScopedObjectT< T, TReleaseValue, TReleaseFunction > &&scopedObject) noexcept
Move constructor.
Definition: ScopedObject.h:303
ScopedObjectT(const T &object, TReleaseFunction &&releaseFunction) noexcept
Creates a new scoped object.
Definition: ScopedObject.h:327
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15