-
-
Save ThePhD/561f327267719882a329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 ) ); | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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