434 lines
9.8 KiB
C++
434 lines
9.8 KiB
C++
/**
|
|
* @file include/ActivOneApi/ScopedHandle.h
|
|
*
|
|
* @brief Header for ScopedHandle definition.
|
|
*/
|
|
|
|
#if (!defined ACTIV_ONE_API_SCOPED_HANDLE_H)
|
|
#define ACTIV_ONE_API_SCOPED_HANDLE_H
|
|
|
|
#include "ActivOneApi/External.h"
|
|
|
|
#include "ActivOneApi/TypeDefinitions.h"
|
|
|
|
#include "ActivMiddleware/ActivBase/Exception.h"
|
|
#include "ActivMiddleware/CxxYZ/functional.h"
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
//! OneApi namespace.
|
|
namespace OneApi
|
|
{
|
|
|
|
class Session;
|
|
|
|
//! ScopedHandle class. A movable only scoped object for managing the lifetime of a Handle.
|
|
class ScopedHandle
|
|
{
|
|
public:
|
|
//! Default constructor.
|
|
ScopedHandle();
|
|
|
|
/**
|
|
* @brief Move constructor.
|
|
*
|
|
* @param rhs
|
|
*
|
|
* @throw Exception
|
|
* @throw std::exception
|
|
*/
|
|
ScopedHandle(ACTIV_BOOST_RV_REF(ScopedHandle) rhs) :
|
|
m_handle(HANDLE_UNDEFINED)
|
|
{
|
|
InternalMove(rhs);
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param handle
|
|
*/
|
|
explicit ScopedHandle(const Handle handle);
|
|
|
|
//! Destructor.
|
|
~ScopedHandle();
|
|
|
|
/**
|
|
* @brief Move assignment operator.
|
|
*
|
|
* @param rhs
|
|
*
|
|
* @return a reference to this object.
|
|
*
|
|
* @throw Exception
|
|
* @throw std::exception
|
|
*/
|
|
ScopedHandle &operator=(ACTIV_BOOST_RV_REF(ScopedHandle) rhs)
|
|
{
|
|
InternalMove(rhs);
|
|
|
|
return *this;
|
|
}
|
|
|
|
/**
|
|
* @brief Assignment operator (from handle).
|
|
*
|
|
* @param handle to assign from.
|
|
*
|
|
* @return *this.
|
|
*
|
|
* @throw Exception
|
|
* @throw std::exception
|
|
*/
|
|
ScopedHandle &operator=(const Handle handle);
|
|
|
|
/**
|
|
* @brief Is the handle initialized?
|
|
*
|
|
* @retval true if this object is initialized.
|
|
*/
|
|
bool IsInitialized() const;
|
|
|
|
/**
|
|
* @brief Reset the scoped handle.
|
|
*
|
|
* The Handle being managed is closed.
|
|
*
|
|
* @throw Exception
|
|
* @throw std::exception
|
|
*/
|
|
void Reset();
|
|
|
|
/**
|
|
* @brief Release the scoped handle.
|
|
*
|
|
* The Handle being managed is released and is now managed by the client. The Handle must be closed using
|
|
* Session::CloseHandle().
|
|
*
|
|
* @return The Handle being released.
|
|
*/
|
|
Handle Release();
|
|
|
|
/**
|
|
* @brief Get the handle.
|
|
*
|
|
* The Handle being managed is returned but still managed by this object.
|
|
*
|
|
* @return The Handle being managed.
|
|
*/
|
|
Handle Get() const;
|
|
|
|
/**
|
|
* @brief Equality test operator.
|
|
*
|
|
* @param rhs ScopedHandle to compare with.
|
|
*
|
|
* @return Boolean.
|
|
*/
|
|
bool operator==(const ScopedHandle &rhs) const;
|
|
|
|
/**
|
|
* @brief Equality test operator.
|
|
*
|
|
* @param rhs Handle to compare with.
|
|
*
|
|
* @return Boolean.
|
|
*/
|
|
bool operator==(const Handle &rhs) const;
|
|
|
|
/**
|
|
* @brief Inequality test operator.
|
|
*
|
|
* @param rhs ScopedHandle to compare with.
|
|
*
|
|
* @return Boolean.
|
|
*/
|
|
bool operator!=(const ScopedHandle &rhs) const;
|
|
|
|
/**
|
|
* @brief Inequality test operator.
|
|
*
|
|
* @param rhs Handle to compare with.
|
|
*
|
|
* @return Boolean.
|
|
*/
|
|
bool operator!=(const Handle &rhs) const;
|
|
|
|
/**
|
|
* @brief '<' operator.
|
|
*
|
|
* @param rhs ScopedHandle to compare with.
|
|
*
|
|
* @return Boolean.
|
|
*/
|
|
bool operator<(const ScopedHandle &rhs) const;
|
|
|
|
/**
|
|
* @brief '<' operator.
|
|
*
|
|
* @param rhs Handle to compare with.
|
|
*
|
|
* @return Boolean.
|
|
*/
|
|
bool operator<(const Handle &rhs) const;
|
|
|
|
/**
|
|
* @brief '!' operator.
|
|
*
|
|
* @return Boolean.
|
|
*/
|
|
bool operator!() const;
|
|
|
|
//! Handle conversion operator.
|
|
operator Handle() const;
|
|
|
|
//! Bool conversion operator.
|
|
operator bool() const;
|
|
|
|
private:
|
|
ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(ScopedHandle)
|
|
|
|
/**
|
|
* @brief Internal move.
|
|
*
|
|
* @param rhs
|
|
*
|
|
* @throw Exception
|
|
* @throw std::exception
|
|
*/
|
|
void InternalMove(ScopedHandle &rhs);
|
|
|
|
/**
|
|
* @brief Internal reset.
|
|
*
|
|
* @throw Exception
|
|
* @throw std::exception
|
|
*/
|
|
ACTIV_ONE_API void InternalReset();
|
|
|
|
//! Log usage exception.
|
|
template <typename E>
|
|
ACTIV_ONE_API void LogUsageException(const E &exception) ACTIV_BOOST_NOEXCEPT_OR_NOTHROW;
|
|
|
|
Handle m_handle;
|
|
|
|
friend class Session;
|
|
friend struct CxxYZ::hash<ScopedHandle>;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedHandle::ScopedHandle() :
|
|
m_handle(HANDLE_UNDEFINED)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedHandle::ScopedHandle(const Handle handle) :
|
|
m_handle(handle)
|
|
{
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedHandle::~ScopedHandle()
|
|
{
|
|
try
|
|
{
|
|
Reset();
|
|
}
|
|
catch (const Exception &e)
|
|
{
|
|
LogUsageException(e);
|
|
assert(false);
|
|
}
|
|
catch (const std::exception &e)
|
|
{
|
|
LogUsageException(e);
|
|
assert(false);
|
|
}
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedHandle &ScopedHandle::operator=(const Handle handle)
|
|
{
|
|
InternalReset();
|
|
|
|
m_handle = handle;
|
|
|
|
return *this;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool ScopedHandle::IsInitialized() const
|
|
{
|
|
return (HANDLE_UNDEFINED != m_handle);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void ScopedHandle::Reset()
|
|
{
|
|
InternalReset();
|
|
|
|
m_handle = HANDLE_UNDEFINED;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline Handle ScopedHandle::Release()
|
|
{
|
|
const Handle handle = m_handle;
|
|
|
|
m_handle = HANDLE_UNDEFINED;
|
|
|
|
return handle;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline Handle ScopedHandle::Get() const
|
|
{
|
|
return m_handle;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool ScopedHandle::operator==(const ScopedHandle &rhs) const
|
|
{
|
|
return (m_handle == rhs.m_handle);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool ScopedHandle::operator==(const Handle &rhs) const
|
|
{
|
|
return (m_handle == rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool ScopedHandle::operator!=(const ScopedHandle &rhs) const
|
|
{
|
|
return !(*this == rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool ScopedHandle::operator!=(const Handle &rhs) const
|
|
{
|
|
return !(*this == rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool ScopedHandle::operator<(const ScopedHandle &rhs) const
|
|
{
|
|
return (m_handle < rhs.m_handle);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool ScopedHandle::operator<(const Handle &rhs) const
|
|
{
|
|
return (m_handle < rhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline bool ScopedHandle::operator!() const
|
|
{
|
|
return !IsInitialized();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedHandle::operator Handle() const
|
|
{
|
|
return m_handle;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline ScopedHandle::operator bool() const
|
|
{
|
|
return IsInitialized();
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
inline void ScopedHandle::InternalMove(ScopedHandle &rhs)
|
|
{
|
|
InternalReset();
|
|
|
|
m_handle = rhs.m_handle;
|
|
rhs.m_handle = HANDLE_UNDEFINED;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! @private
|
|
inline bool operator!=(const Handle &lhs, const ScopedHandle &rhs)
|
|
{
|
|
return (rhs != lhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! @private
|
|
inline bool operator==(const Handle &lhs, const ScopedHandle &rhs)
|
|
{
|
|
return (rhs == lhs);
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//! @private
|
|
inline bool operator<(const Handle &lhs, const ScopedHandle &rhs)
|
|
{
|
|
return (lhs < rhs.Get());
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief hash_value
|
|
*
|
|
* @param scopedHandle
|
|
*
|
|
* @return The hashed value of @a scopedHandle
|
|
*/
|
|
inline std::size_t hash_value(const ScopedHandle &scopedHandle)
|
|
{
|
|
return Activ::CxxYZ::hash<Activ::OneApi::Handle>()(scopedHandle.Get());
|
|
}
|
|
|
|
} // namespace OneApi
|
|
|
|
} // namespace Activ
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
namespace ACTIV_CXXYZ_HASH_NAMESPACE
|
|
{
|
|
|
|
template<>
|
|
struct hash<Activ::OneApi::ScopedHandle>
|
|
{
|
|
size_t operator()(const Activ::OneApi::ScopedHandle &scopedHandle) const
|
|
{
|
|
return hash_value(scopedHandle);
|
|
}
|
|
};
|
|
|
|
} // namespace ACTIV_CXXYZ_HASH_NAMESPACE
|
|
|
|
#endif // (!defined ACTIV_ONE_API_SCOPED_HANDLE_H)
|