568 lines
14 KiB
C++
568 lines
14 KiB
C++
/**
|
|
* @file include/ActivMiddleware/Misc/ScopedHelper.h
|
|
*
|
|
* @brief Helper classes for creating scoped wrappers around other objects.
|
|
*/
|
|
|
|
#if (!defined ACTIV_MISC_SCOPED_HELPER_H)
|
|
#define ACTIV_MISC_SCOPED_HELPER_H
|
|
|
|
#include "ActivMiddleware/ActivMiddleware.h"
|
|
|
|
#include "ActivMiddleware/ActivBase/StatusCodeException.h"
|
|
#include "ActivMiddleware/CxxYZ/memory.h"
|
|
#include "ActivMiddleware/CxxYZ/type_traits.h"
|
|
|
|
#include ACTIV_BOOST_INCLUDE(utility/explicit_operator_bool.hpp)
|
|
|
|
#if (defined ACTIV_PLATFORM_WINDOWS)
|
|
#include <io.h>
|
|
#endif
|
|
|
|
//! Activ namespace.
|
|
namespace Activ
|
|
{
|
|
|
|
namespace Detail
|
|
{
|
|
|
|
template<typename T, typename ENABLE = void>
|
|
class ScopedHelperInvalidValueHelper
|
|
{
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
class ScopedHelperInvalidValueHelper<T, typename CxxYZ::enable_if<CxxYZ::is_pointer<T>::value>::type>
|
|
{
|
|
protected:
|
|
static T InvalidValue()
|
|
{
|
|
// Default implementation for pointer types.
|
|
return ACTIV_NULLPTR;
|
|
}
|
|
};
|
|
|
|
} // namespace Detail
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Base class for various scoped helpers.
|
|
*
|
|
* @tparam T the type to wrap.
|
|
* @tparam D the derived class. Must provide:
|
|
* static void Destroy(const T &);
|
|
* Optionally:
|
|
* static T InvalidValue(); (if T is not a pointer, or nullptr is not the invalid value for T).
|
|
* static bool IsValid(const T &); (e.g. for ScopedHandle with NULL and INVALID_HANDLE_VALUE).
|
|
*/
|
|
template<typename T, typename D>
|
|
class ScopedHelper : public Detail::ScopedHelperInvalidValueHelper<T>, private Boost::noncopyable
|
|
{
|
|
public:
|
|
//! Destructor. Calls D::Destroy() if managed object is valid.
|
|
~ScopedHelper()
|
|
{
|
|
if (!!(*this))
|
|
D::Destroy(m_object);
|
|
}
|
|
|
|
//! Accessor for underlying type T.
|
|
operator typename Boost::call_traits<T>::param_type() const
|
|
{
|
|
return m_object;
|
|
}
|
|
|
|
//! Accessor for underlying type T.
|
|
typename Boost::call_traits<T>::param_type Get() const
|
|
{
|
|
return m_object;
|
|
}
|
|
|
|
//! Invoke cleanup on the currently managed object (if valid).
|
|
void Reset()
|
|
{
|
|
if (!!(*this))
|
|
{
|
|
D::Destroy(m_object);
|
|
m_object = D::InvalidValue();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Invoke cleanup on the currently managed object (if valid) and assign a new value.
|
|
*
|
|
* @param value the new value to manage.
|
|
*/
|
|
void Reset(typename Boost::call_traits<T>::param_type object)
|
|
{
|
|
Reset();
|
|
|
|
this->m_object = object;
|
|
}
|
|
|
|
//! Release the wrapped value.
|
|
T Release()
|
|
{
|
|
T value(CxxYZ::move(m_object));
|
|
|
|
m_object = D::InvalidValue();
|
|
|
|
return ACTIV_MOVE_RETURN(T, value);
|
|
}
|
|
|
|
//! Bool operator.
|
|
ACTIV_BOOST_EXPLICIT_OPERATOR_BOOL()
|
|
|
|
//! Not operator.
|
|
bool operator!() const
|
|
{
|
|
return !D::IsValid(m_object);
|
|
}
|
|
|
|
protected:
|
|
//! Default constructor.
|
|
ScopedHelper() :
|
|
m_object(D::InvalidValue())
|
|
{
|
|
}
|
|
|
|
//! Constructor.
|
|
ScopedHelper(typename Boost::call_traits<T>::param_type object) :
|
|
m_object(object)
|
|
{
|
|
}
|
|
|
|
//! Default IsValid() implementation.
|
|
static bool IsValid(typename Boost::call_traits<T>::param_type object)
|
|
{
|
|
return (object != D::InvalidValue());
|
|
}
|
|
|
|
T m_object;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Scoped memory helper.
|
|
*
|
|
* Templatized by the pointer type, e.g. char *.
|
|
*
|
|
* Will call free() on destruction if the pointer is not NULL.
|
|
*/
|
|
template<typename T>
|
|
class ScopedMemory : public ScopedHelper<T, ScopedMemory<T> >
|
|
{
|
|
public:
|
|
//! Default constructor.
|
|
ScopedMemory()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param pMem memory allocated via malloc() or realloc(), or NULL.
|
|
*/
|
|
ScopedMemory(void * const pMem) :
|
|
ScopedHelper<T, ScopedMemory<T> >(reinterpret_cast<T>(pMem))
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param size size of memory to allocate with malloc().
|
|
*
|
|
* @throw Exception on memory allocation failure.
|
|
*/
|
|
ScopedMemory(const size_t size) :
|
|
ScopedHelper<T, ScopedMemory<T> >(reinterpret_cast<T>(::malloc(size)))
|
|
{
|
|
if (!(*this))
|
|
ACTIV_THROW(StatusCodeException, STATUS_CODE_HEAP_EMPTY);
|
|
}
|
|
|
|
/**
|
|
* @brief Reallocate the memory region being managed.
|
|
*
|
|
* @retval true if the memory was reallocated successfully.
|
|
* @retval false the memory was not reallocated; the original pointer is still valid.
|
|
*/
|
|
bool Realloc(const size_t newSize)
|
|
{
|
|
T pNewBuffer = reinterpret_cast<T>(::realloc(this->m_object, newSize));
|
|
if (NULL == pNewBuffer)
|
|
return false;
|
|
|
|
this->m_object = pNewBuffer;
|
|
|
|
return true;
|
|
}
|
|
|
|
private:
|
|
static void Destroy(const T ptr)
|
|
{
|
|
::free(ptr);
|
|
}
|
|
|
|
friend class ScopedHelper<T, ScopedMemory<T> >;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Scoped FILE helper.
|
|
*
|
|
* Will call fclose() on destruction if the FILE* is not NULL.
|
|
*/
|
|
class ScopedFile : public ScopedHelper<FILE *, ScopedFile>
|
|
{
|
|
public:
|
|
//! Default constructor.
|
|
ScopedFile()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param pFile
|
|
*/
|
|
ScopedFile(FILE * const pFile) :
|
|
ScopedHelper<FILE *, ScopedFile>(pFile)
|
|
{
|
|
}
|
|
|
|
private:
|
|
static void Destroy(FILE * const pFile)
|
|
{
|
|
(void) ::fclose(pFile);
|
|
}
|
|
|
|
friend class ScopedHelper<FILE *, ScopedFile>;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Scoped file-descriptor helper.
|
|
*
|
|
* Will call close() on destruction if the fd is not -1.
|
|
*/
|
|
class ScopedFd : public ScopedHelper<int, ScopedFd>
|
|
{
|
|
public:
|
|
//! Default constructor.
|
|
ScopedFd()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param fd
|
|
*/
|
|
ScopedFd(const int fd) :
|
|
ScopedHelper<int, ScopedFd>(fd)
|
|
{
|
|
}
|
|
|
|
private:
|
|
static int InvalidValue()
|
|
{
|
|
return -1;
|
|
}
|
|
|
|
static void Destroy(const int fd)
|
|
{
|
|
(void) ::close(fd);
|
|
}
|
|
|
|
friend class ScopedHelper<int, ScopedFd>;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Scoped socket helper.
|
|
*
|
|
* Will call closesocket() on destruction if the socket is not INVALID_SOCKET.
|
|
*/
|
|
class ScopedSocket : public ScopedHelper<SOCKET, ScopedSocket>
|
|
{
|
|
public:
|
|
//! Default constructor.
|
|
ScopedSocket()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param socket
|
|
*/
|
|
ScopedSocket(const SOCKET socket) :
|
|
ScopedHelper<SOCKET, ScopedSocket>(socket)
|
|
{
|
|
}
|
|
|
|
private:
|
|
static SOCKET InvalidValue()
|
|
{
|
|
return INVALID_SOCKET;
|
|
}
|
|
|
|
static void Destroy(const SOCKET socket)
|
|
{
|
|
(void) closesocket(socket);
|
|
}
|
|
|
|
friend class ScopedHelper<SOCKET, ScopedSocket>;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
#if (defined ACTIV_PLATFORM_WINDOWS)
|
|
|
|
/**
|
|
* @brief Scoped HANDLE helper.
|
|
*
|
|
* Will call CloseHandle() on destruction if the HANDLE is not NULL or INVALID_HANDLE_VALUE.
|
|
*/
|
|
class ScopedHandle : public ScopedHelper<HANDLE, ScopedHandle>
|
|
{
|
|
public:
|
|
//! Default constructor; sets managed handle to INVALID_HANDLE_VALUE.
|
|
ScopedHandle()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param handle
|
|
*/
|
|
ScopedHandle(const HANDLE handle) :
|
|
ScopedHelper<HANDLE, ScopedHandle>(handle)
|
|
{
|
|
}
|
|
|
|
private:
|
|
static HANDLE InvalidValue()
|
|
{
|
|
return INVALID_HANDLE_VALUE;
|
|
}
|
|
|
|
// Cope with NULL also.
|
|
static bool IsValid(const HANDLE handle)
|
|
{
|
|
return ((NULL != handle) && (INVALID_HANDLE_VALUE != handle));
|
|
}
|
|
|
|
static void Destroy(const HANDLE handle)
|
|
{
|
|
(void) ::CloseHandle(handle);
|
|
}
|
|
|
|
friend class ScopedHelper<HANDLE, ScopedHandle>;
|
|
};
|
|
|
|
#endif // (defined ACTIV_PLATFORM_WINDOWS)
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Scoped cleanup helper.
|
|
*
|
|
* @tparam D a function object to be invoked if Clear() hasn't been called on this object.
|
|
*/
|
|
template<typename D>
|
|
class ScopedCleanup
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param cleanup function object to invoke on destruction. A copy is taken.
|
|
*/
|
|
explicit ScopedCleanup(const D &cleanup) :
|
|
m_isActive(true),
|
|
m_cleanup(cleanup)
|
|
{
|
|
}
|
|
|
|
#if (!defined ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES)
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param cleanup function object to invoke on destruction.
|
|
*/
|
|
explicit ScopedCleanup(D &&cleanup) :
|
|
m_isActive(true),
|
|
m_cleanup(CxxYZ::move(cleanup))
|
|
{
|
|
}
|
|
#endif
|
|
|
|
/**
|
|
* @brief Move constructor.
|
|
*
|
|
* @param scopedCleanup ScopedCleanup to move from.
|
|
*/
|
|
ScopedCleanup(ACTIV_BOOST_RV_REF(ScopedCleanup) scopedCleanup) :
|
|
m_isActive(scopedCleanup.m_isActive),
|
|
m_cleanup(CxxYZ::move(scopedCleanup.m_cleanup))
|
|
{
|
|
// NB intentionally not explicit.
|
|
scopedCleanup.m_isActive = false;
|
|
}
|
|
|
|
//! Destructor. Executes the function passed to the constructor unless Clear() has been called.
|
|
~ScopedCleanup() ACTIV_BOOST_NOEXCEPT
|
|
{
|
|
if (m_isActive)
|
|
m_cleanup();
|
|
}
|
|
|
|
//! Stop the destructor from invoking the cleanup function.
|
|
void Clear() ACTIV_BOOST_NOEXCEPT
|
|
{
|
|
m_isActive = false;
|
|
}
|
|
|
|
private:
|
|
ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(ScopedCleanup)
|
|
|
|
// No move assignment.
|
|
ScopedCleanup &operator=(ACTIV_BOOST_RV_REF(ScopedCleanup)) ACTIV_DELETE;
|
|
|
|
// Insufficient, but try to stop dynamic allocation.
|
|
void *operator new(size_t) ACTIV_DELETE;
|
|
void *operator new(size_t, const std::nothrow_t &) ACTIV_DELETE;
|
|
void *operator new(size_t, void *) ACTIV_DELETE;
|
|
void *operator new[](size_t) ACTIV_DELETE;
|
|
void *operator new[](size_t, const std::nothrow_t &) ACTIV_DELETE;
|
|
void *operator new[](size_t, void *) ACTIV_DELETE;
|
|
|
|
bool m_isActive;
|
|
D m_cleanup;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
#if (!defined ACTIV_BOOST_NO_CXX11_RVALUE_REFERENCES)
|
|
template<typename D>
|
|
ScopedCleanup<typename std::decay<D>::type> MakeScopedCleanup(D &&cleanup)
|
|
{
|
|
return ScopedCleanup<typename std::decay<D>::type>(std::forward<D>(cleanup));
|
|
}
|
|
#else
|
|
template<typename D>
|
|
ScopedCleanup<D> MakeScopedCleanup(const D &cleanup)
|
|
{
|
|
return ScopedCleanup<D>(cleanup);
|
|
}
|
|
#endif
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
/**
|
|
* @brief Scoped container erase helper.
|
|
*
|
|
* @tparam T a container.
|
|
*/
|
|
template<typename T>
|
|
class ScopedErase
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Constructor.
|
|
*
|
|
* @param container the container to erase @a iter from on destruction if Clear() hasn't been called and @a shouldErase true.
|
|
* @param iter the iterator to remove. A copy is taken.
|
|
* @param shouldErase
|
|
*/
|
|
explicit ScopedErase(T &container, const typename T::iterator &iter, const bool shouldErase = true) :
|
|
m_isActive(shouldErase),
|
|
m_container(container),
|
|
m_iter(iter)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* @brief Move constructor.
|
|
*
|
|
* @param scopedErase ScopedErase to move from.
|
|
*/
|
|
ScopedErase(ACTIV_BOOST_RV_REF(ScopedErase) scopedErase) :
|
|
m_isActive(scopedErase.m_isActive),
|
|
m_container(scopedErase.m_container),
|
|
m_iter(CxxYZ::move(scopedErase.m_iter))
|
|
{
|
|
// NB intentionally not explicit.
|
|
scopedErase.m_isActive = false;
|
|
}
|
|
|
|
//! Destructor. Erases the iterator from the container unless Clear() has been called.
|
|
~ScopedErase()
|
|
{
|
|
if (m_isActive)
|
|
m_container.erase(m_iter);
|
|
}
|
|
|
|
//! Stop the destructor from erasing the iterator from the container.
|
|
void Clear() ACTIV_BOOST_NOEXCEPT
|
|
{
|
|
m_isActive = false;
|
|
}
|
|
|
|
private:
|
|
ACTIV_BOOST_MOVABLE_BUT_NOT_COPYABLE(ScopedErase)
|
|
|
|
// No move assignment.
|
|
ScopedErase &operator=(ACTIV_BOOST_RV_REF(ScopedErase)) ACTIV_DELETE;
|
|
|
|
// Insufficient, but try to stop dynamic allocation.
|
|
void *operator new(size_t) ACTIV_DELETE;
|
|
void *operator new(size_t, const std::nothrow_t &) ACTIV_DELETE;
|
|
void *operator new(size_t, void *) ACTIV_DELETE;
|
|
void *operator new[](size_t) ACTIV_DELETE;
|
|
void *operator new[](size_t, const std::nothrow_t &) ACTIV_DELETE;
|
|
void *operator new[](size_t, void *) ACTIV_DELETE;
|
|
|
|
bool m_isActive;
|
|
T & m_container;
|
|
typename T::iterator m_iter;
|
|
};
|
|
|
|
// ---------------------------------------------------------------------------------------------------------------------------------
|
|
|
|
template<typename T>
|
|
ScopedErase<T> MakeScopedErase(T &container, const typename T::iterator &iter, const bool shouldErase = true)
|
|
{
|
|
// We could really implement this in terms of ScopedCleanup for C++14 with return type deduction
|
|
// and do away with the ScopedErase class entirely:
|
|
// return MakeScopedCleanup([&]() { container.erase(iter); });
|
|
|
|
// Also note - iterator not const_iterator for StlPort. Given the intended use is to scope a new insertion, this shouldn't
|
|
// be a problem (a non const_iterator will be available).
|
|
|
|
return ScopedErase<T>(container, iter, shouldErase);
|
|
}
|
|
|
|
} // namespace Activ
|
|
|
|
#endif // (!defined ACTIV_MISC_SCOPED_HELPER_H)
|