metaforce/Runtime/RetroTypes.hpp

67 lines
1.6 KiB
C++
Raw Normal View History

2015-08-18 22:48:57 -07:00
#ifndef __RETRO_TYPES_HPP__
#define __RETRO_TYPES_HPP__
2015-08-17 13:33:58 -07:00
2015-08-19 19:52:07 -07:00
#include <vector>
2015-08-17 22:54:43 -07:00
#include <utility>
2015-08-17 13:33:58 -07:00
#include "GCNTypes.hpp"
namespace Retro
{
2015-08-17 13:33:58 -07:00
/**
2015-08-18 22:48:57 -07:00
* @brief singleton static-allocator
2015-08-17 13:33:58 -07:00
*/
template<class T>
class TOneStatic
{
2015-08-17 22:54:43 -07:00
static u8 m_allocspace[sizeof(T)];
2015-08-18 22:48:57 -07:00
static u32 m_refCount;
2015-08-17 13:33:58 -07:00
public:
2015-08-17 22:54:43 -07:00
static T* GetAllocSpace() {return (T*)m_allocspace;}
2015-08-17 13:33:58 -07:00
static u32& ReferenceCount() {return m_refCount;}
2015-08-17 22:54:43 -07:00
T* operator->() const {return (T*)m_allocspace;}
2015-08-18 16:30:23 -07:00
T& operator*() const {return *(T*)m_allocspace;}
2015-08-17 22:54:43 -07:00
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)
2015-08-18 12:45:28 -07:00
{++ReferenceCount(); new (m_allocspace) T();}
2015-08-17 22:54:43 -07:00
template<typename... Args> TOneStatic(Args&&... args)
2015-08-18 12:45:28 -07:00
{++ReferenceCount(); new (m_allocspace) T(std::forward<Args>(args)...);}
2015-08-17 22:54:43 -07:00
~TOneStatic() {--ReferenceCount();}
template<typename... Args> void reset(Args&&... args)
2015-08-18 12:45:28 -07:00
{new (m_allocspace) T(std::forward<Args>(args)...);}
2015-08-17 13:33:58 -07:00
};
2015-08-17 22:54:43 -07:00
template<class T> u8 TOneStatic<T>::m_allocspace[sizeof(T)];
2015-08-17 13:33:58 -07:00
template<class T> u32 TOneStatic<T>::m_refCount;
2015-08-19 19:52:07 -07:00
using TUniqueId = u16;
2015-08-18 22:48:57 -07:00
using TEditorId = u32;
using TAreaId = u32;
2015-08-19 19:52:07 -07:00
#define kInvalidEditorId TEditorId(-1)
#define kInvalidUniqueId TUniqueId(-1)
#define kInvalidAreaId TAreaId(-1)
}
namespace rstl
{
template <class T, size_t N>
class reserved_vector : public std::vector<T>
{
public:
reserved_vector() {this->reserve(N);}
};
}
2015-08-18 22:48:57 -07:00
#endif // __RETRO_TYPES_HPP__