Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCG_ALIGNMENT_H
Toshihiro Shimizu 890ddd
#define TCG_ALIGNMENT_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "macros.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*! \file alignment.h
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  This file contains C++ utilities about types alignment.
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace tcg {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**************************************************************************
Toshihiro Shimizu 890ddd
//    Private  stuff
Toshihiro Shimizu 890ddd
//**************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// From http://stackoverflow.com/questions/6959261/how-can-i-simulate-alignast
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
union _MaxAlign {
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  long l;
Shinya Kitaoka 120a6e
  long long ll;
Shinya Kitaoka 120a6e
  long double ld;
Shinya Kitaoka 120a6e
  double d;
Shinya Kitaoka 120a6e
  void *p;
Shinya Kitaoka 120a6e
  void (*pf)();
Shinya Kitaoka 120a6e
  _MaxAlign *ps;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename bool="" t,=""></typename>
Toshihiro Shimizu 890ddd
struct _AlignTypeDetail;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename t=""></typename>
Toshihiro Shimizu 890ddd
struct _AlignTypeDetail<t, false=""> {</t,>
Shinya Kitaoka 120a6e
  typedef T type;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename t=""></typename>
Toshihiro Shimizu 890ddd
struct _AlignTypeDetail<t, true=""> {</t,>
Shinya Kitaoka 120a6e
  typedef char type;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <size_t alignment,="" typename="" u=""></size_t>
Toshihiro Shimizu 890ddd
struct _AlignType {
Shinya Kitaoka 120a6e
  typedef typename _AlignTypeDetail<u, (alignment="" <="" sizeof(u))="">::type type;</u,>
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename t=""></typename>
Toshihiro Shimizu 890ddd
struct _Aligner {
Shinya Kitaoka 120a6e
  char c;
Shinya Kitaoka 120a6e
  T t;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**************************************************************************
Toshihiro Shimizu 890ddd
//    TCG alignment  unions
Toshihiro Shimizu 890ddd
//**************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <int alignment=""></int>
Toshihiro Shimizu 890ddd
union aligner_type {
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, char="">::type c;</alignment,>
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, short="">::type s;</alignment,>
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, int="">::type i;</alignment,>
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, long="">::type l;</alignment,>
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, long="">::type ll;</alignment,>
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, float="">::type f;</alignment,>
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, double="">::type d;</alignment,>
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, double="" long="">::type ld;</alignment,>
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, *="" void="">::type pc;</alignment,>
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, *="" _maxalign="">::type ps;</alignment,>
Shinya Kitaoka 120a6e
  typename _AlignType<alignment, (*)()="" void="">::type pf;</alignment,>
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename t=""></typename>
Toshihiro Shimizu 890ddd
union aligned_buffer {
Michał Janiszewski 5387be
  typedef aligner_type<sizeof(_aligner<t>) - sizeof(T)> aligner_type_t;</sizeof(_aligner<t>
Toshihiro Shimizu 890ddd
Michał Janiszewski 5387be
  aligner_type_t m_aligner;
Shinya Kitaoka 120a6e
  char m_buf[sizeof(T)];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Shinya Kitaoka 120a6e
  TCG_STATIC_ASSERT(sizeof(_Aligner<t>) - sizeof(T) ==</t>
Michał Janiszewski 5387be
                    sizeof(_Aligner<aligner_type_t>) - sizeof(aligner_type_t));</aligner_type_t>
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**************************************************************************
Toshihiro Shimizu 890ddd
//    TCG alignment  traits
Toshihiro Shimizu 890ddd
//**************************************************************************
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename t=""></typename>
Toshihiro Shimizu 890ddd
struct alignment_traits {
Shinya Kitaoka 120a6e
  static const int alignment = sizeof(_Aligner<t>) - sizeof(T);</t>
Toshihiro Shimizu 890ddd
Michał Janiszewski 5387be
  typedef aligner_type<alignment> aligner_type_traits;</alignment>
Shinya Kitaoka 120a6e
  typedef aligned_buffer<t> buffer_type;</t>
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace tcg
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TCG_ALIGNMENT_H