Skip to content

Instantly share code, notes, and snippets.

@ThePhD
Created July 6, 2014 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThePhD/561f327267719882a329 to your computer and use it in GitHub Desktop.
Save ThePhD/561f327267719882a329 to your computer and use it in GitHub Desktop.
#pragma once
#include <Furrovine++/numeric_integer_def.h>
#include <type_traits>
namespace Furrovine {
template <ulword size, ulword align = sizeof( ulword )>
struct raw_storage {
typedef typename std::aligned_storage<size, align>::type buffer_t;
const static ulword storage_size = sizeof( buffer_t );
buffer_t buffer;
template <typename T>
T& get( ulword n ) {
return *static_cast<T*>( static_cast<void*>( static_cast<byte*>( static_cast<void*>( std::addressof( this->buffer ) ) ) + n ) );
}
template <typename T>
const T& get( ulword n ) const {
return *static_cast<const T*>( static_cast<const void*>( static_cast<const byte*>( static_cast<const void*>( std::addressof( this->buffer ) ) ) + n ) );
}
template <typename T, ulword n = 0>
T& get( ) {
return *static_cast<T*>( static_cast<void*>( static_cast<byte*>( static_cast<void*>( std::addressof( this->buffer ) ) ) + n ) );
}
template <typename T, ulword n = 0>
const T& get( ) const {
return *static_cast<const T*>( static_cast<const void*>( static_cast<const byte*>( static_cast<const void*>( std::addressof( this->buffer ) ) ) + n ) );
}
};
}
template <typename T, ulword type_size = sizeof( T ), ulword type_align = std::alignment_of<T>::value>
struct uninitialized {
typedef raw_storage<type_size, type_align> storage_t;
storage_t res;
template <typename... Tn>
void construct( Tn&&... argn ) {
new ( std::addressof( get( ) ) )T( std::forward<Tn>( argn )... );
}
template <typename... Tn>
void reconstruct( Tn&&... argn ) {
destruct( );
construct( std::forward<Tn>( argn )... );
}
T&& move( ) {
return std::move( get( ) );
}
T& get( ) {
return res.template get<T>( );
}
const T& get( ) const {
return res.template get<T>( );
}
T& operator*( ) {
return get( );
}
const T& operator*( ) const {
return get( );
}
operator T&& ( ) {
return move( );
}
operator T& ( ) {
return get( );
}
operator const T& ( ) const {
return get( );
}
T* operator->( ) {
return std::addressof( get( ) );
}
const T* operator->( ) const {
return std::addressof( get( ) );
}
void destruct( ) {
get( ).~T( );
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment