metaforce/Runtime/Common/RetroTemplates.hpp

50 lines
1.3 KiB
C++
Raw Normal View History

2015-08-17 20:33:58 +00:00
#ifndef __RETRO_TEMPLATES_HPP__
#define __RETRO_TEMPLATES_HPP__
2015-08-18 05:54:43 +00:00
#include <utility>
2015-08-17 20:33:58 +00:00
#include "GCNTypes.hpp"
namespace Retro
{
namespace Common
{
2015-08-17 20:33:58 +00:00
/**
* @brief Inheritable singleton static-allocator
*/
template<class T>
class TOneStatic
{
2015-08-18 05:54:43 +00:00
static u8 m_allocspace[sizeof(T)];
2015-08-17 20:33:58 +00:00
static uint32_t m_refCount;
public:
2015-08-18 05:54:43 +00:00
static T* GetAllocSpace() {return (T*)m_allocspace;}
2015-08-17 20:33:58 +00:00
static u32& ReferenceCount() {return m_refCount;}
2015-08-18 05:54:43 +00:00
T* operator->() const {return (T*)m_allocspace;}
void* operator new(size_t) = delete;
void operator delete(void*) = delete;
template<typename U = T>
TOneStatic(typename std::enable_if<!std::is_default_constructible<U>::value>::type* = 0)
{++ReferenceCount();}
template<typename U = T>
TOneStatic(typename std::enable_if<std::is_default_constructible<U>::value>::type* = 0)
{++ReferenceCount(); new (GetAllocSpace()) T();}
template<typename... Args> TOneStatic(Args&&... args)
{++ReferenceCount(); new (GetAllocSpace()) T(std::forward<Args>(args)...);}
~TOneStatic() {--ReferenceCount();}
template<typename... Args> void reset(Args&&... args)
{new (GetAllocSpace()) T(std::forward<Args>(args)...);}
2015-08-17 20:33:58 +00:00
};
2015-08-18 05:54:43 +00:00
template<class T> u8 TOneStatic<T>::m_allocspace[sizeof(T)];
2015-08-17 20:33:58 +00:00
template<class T> u32 TOneStatic<T>::m_refCount;
}
}
2015-08-17 20:33:58 +00:00
#endif // __RETRO_TEMPLATES_HPP__