Dune Core Modules (2.4.1)

alignment.hh
Go to the documentation of this file.
1 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 // vi: set et ts=4 sw=2 sts=2:
3 #ifndef DUNE_ALIGNMENT_HH
4 #define DUNE_ALIGNMENT_HH
5 
6 #include <cstddef>
7 #include <type_traits>
8 
9 namespace Dune
10 {
11 
23  namespace
24  {
25 
30  template<class T>
31  struct AlignmentStruct
32  {
33  char c;
34  T t;
35  void hack();
36  };
37 
42  template<class T, std::size_t N>
43  struct AlignmentHelper
44  {
45  enum { N2 = sizeof(AlignmentStruct<T>) - sizeof(T) - N };
46  char padding1[N];
47  T t;
48  char padding2[N2];
49  };
50 
51 #define ALIGNMENT_MODULO(a, b) (a % b == 0 ? \
52  static_cast<std::size_t>(b) : \
53  static_cast<std::size_t>(a % b))
54 #define ALIGNMENT_MIN(a, b) (static_cast<std::size_t>(a) < \
55  static_cast<std::size_t>(b) ? \
56  static_cast<std::size_t>(a) : \
57  static_cast<std::size_t>(b))
59  template <class T, std::size_t N>
60  struct AlignmentTester
61  {
62  typedef AlignmentStruct<T> s;
63  typedef AlignmentHelper<T, N> h;
64  typedef AlignmentTester<T, N - 1> next;
65  enum
66  {
67  a1 = ALIGNMENT_MODULO(N , sizeof(T)),
68  a2 = ALIGNMENT_MODULO(h::N2 , sizeof(T)),
69  a3 = ALIGNMENT_MODULO(sizeof(h), sizeof(T)),
70  a = sizeof(h) == sizeof(s) ? ALIGNMENT_MIN(a1, a2) : a3,
71  result = ALIGNMENT_MIN(a, next::result)
72  };
73  };
74 
76  template <class T>
77  struct AlignmentTester<T, 0>
78  {
79  enum
80  {
81  result = ALIGNMENT_MODULO(sizeof(AlignmentStruct<T>), sizeof(T))
82  };
83  };
84  } //end anonymous namespace
85 
94  template <class T>
95  struct AlignmentOf
96  {
97 
98  enum
99  {
101  value = std::alignment_of<T>::value
102  };
103  };
104 
106 }
107 #endif
Dune namespace.
Definition: alignment.hh:10
Calculates the alignment requirement of a type.
Definition: alignment.hh:96
@ value
The alignment requirement.
Definition: alignment.hh:101
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 16, 22:29, 2024)