#pragma once #ifndef TCG_ALIGNMENT_H #define TCG_ALIGNMENT_H #include "macros.h" /*! \file alignment.h This file contains C++ utilities about types alignment. */ namespace tcg { //************************************************************************** // Private stuff //************************************************************************** // From http://stackoverflow.com/questions/6959261/how-can-i-simulate-alignast union _MaxAlign { int i; long l; long long ll; long double ld; double d; void *p; void (*pf)(); _MaxAlign *ps; }; //--------------------------------------------------------------- template struct _AlignTypeDetail; template struct _AlignTypeDetail { typedef T type; }; template struct _AlignTypeDetail { typedef char type; }; template struct _AlignType { typedef typename _AlignTypeDetail::type type; }; template struct _Aligner { char c; T t; }; //************************************************************************** // TCG alignment unions //************************************************************************** template union aligner_type { private: typename _AlignType::type c; typename _AlignType::type s; typename _AlignType::type i; typename _AlignType::type l; typename _AlignType::type ll; typename _AlignType::type f; typename _AlignType::type d; typename _AlignType::type ld; typename _AlignType::type pc; typename _AlignType::type ps; typename _AlignType::type pf; }; //--------------------------------------------------------------- template union aligned_buffer { typedef aligner_type) - sizeof(T)> aligner_type; aligner_type m_aligner; char m_buf[sizeof(T)]; private: TCG_STATIC_ASSERT(sizeof(_Aligner) - sizeof(T) == sizeof(_Aligner) - sizeof(aligner_type)); }; //************************************************************************** // TCG alignment traits //************************************************************************** template struct alignment_traits { static const int alignment = sizeof(_Aligner) - sizeof(T); typedef aligner_type aligner_type; typedef aligned_buffer buffer_type; }; } // namespace tcg #endif // TCG_ALIGNMENT_H