More is aligned tests

This commit is contained in:
Denis Blank 2018-01-29 00:36:08 +01:00
parent 0f3ec412fe
commit c2ad28d702

View File

@ -43,6 +43,13 @@
namespace cti { namespace cti {
namespace detail { namespace detail {
#ifndef _NDEBUG
template <typename T>
bool is_aligned(T const* p) {
return !(reinterpret_cast<uintptr_t>(p) % std::alignment_of<T>::value);
}
#endif // _NDEBUG
namespace util { namespace util {
namespace detail { namespace detail {
enum class slot_t { empty, value, error }; enum class slot_t { empty, value, error };
@ -301,26 +308,34 @@ private:
assert(!is_empty()); assert(!is_empty());
#ifndef _NDEBUG #ifndef _NDEBUG
void* ptr = &this->storage_; {
auto space = sizeof(this->storage_); void* ptr = &this->storage_;
void* aligned = std::align(alignof(V), sizeof(V), ptr, space); auto space = sizeof(this->storage_);
assert(aligned && "Bad alignment!"); void* aligned = std::align(alignof(V), sizeof(V), ptr, space);
assert(aligned && "Bad alignment!");
}
#endif // _NDEBUG #endif // _NDEBUG
return *reinterpret_cast<V*>(&this->storage_); auto ptr = reinterpret_cast<V*>(&this->storage_);
assert(is_aligned(ptr) && "ptr not aligned!");
return *ptr;
} }
template <typename V> template <typename V>
V const& cast() const noexcept { V const& cast() const noexcept {
assert(!is_empty()); assert(!is_empty());
#ifndef _NDEBUG #ifndef _NDEBUG
void* ptr = const_cast<void*>(static_cast<void const*>(&this->storage_)); {
auto space = sizeof(this->storage_); void* ptr = const_cast<void*>(static_cast<void const*>(&this->storage_));
void* aligned = std::align(alignof(V), sizeof(V), ptr, space); auto space = sizeof(this->storage_);
assert(aligned && "Bad alignment!"); void* aligned = std::align(alignof(V), sizeof(V), ptr, space);
assert(aligned && "Bad alignment!");
}
#endif // _NDEBUG #endif // _NDEBUG
return *reinterpret_cast<V const*>(&this->storage_); auto ptr = reinterpret_cast<V const*>(&this->storage_);
assert(is_aligned(ptr) && "ptr not aligned!");
return *ptr;
} }
template <typename V> template <typename V>