-
Notifications
You must be signed in to change notification settings - Fork 2
/
AlignedBase.hpp
36 lines (30 loc) · 1.09 KB
/
AlignedBase.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef ALIGNEDBASE_HPP_INCLUDED
#define ALIGNEDBASE_HPP_INCLUDED
#include <cstddef>
#include <exception>
#include "RoundUp.hpp"
#include "IsPowerOfTwo.hpp"
#define ALIGNMENT_MUST_BE_A_POWER_OF_2_ERROR_MESSAGE "Alignment must be a power of 2"
template<typename T, std::size_t Alignment = -1>
class AlignedBase {
static_assert(IsPowerOfTwo<std::size_t, Alignment>::Result, ALIGNMENT_MUST_BE_A_POWER_OF_2_ERROR_MESSAGE);
protected:
static const std::size_t sizeOfTPaddedToAlignment = RoundUp<std::size_t, sizeof(T), Alignment>::Result;
};
template<typename T>
class AlignedBase<T, -1> {
public:
class AlignmentException : public std::exception {
friend class AlignedBase<T, -1>;
AlignmentException() : std::exception(ALIGNMENT_MUST_BE_A_POWER_OF_2_ERROR_MESSAGE) {}
};
AlignedBase & operator=(AlignedBase const &) = delete;
protected:
std::size_t const sizeOfTPaddedToAlignment;
AlignedBase(std::size_t alignment) : sizeOfTPaddedToAlignment(roundUp(sizeof(T), alignment)) {
if (!isPowerOfTwo(alignment))
throw new AlignmentException();
}
};
#undef ALIGNMENT_MUST_BE_A_POWER_OF_2_ERROR_MESSAGE
#endif