Skip to content

Commit

Permalink
Aligned storage
Browse files Browse the repository at this point in the history
  • Loading branch information
alandefreitas committed Feb 18, 2022
1 parent 714586b commit 4c7e1f1
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
37 changes: 37 additions & 0 deletions include/futures/detail/utility/aligned_storage_for.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// Copyright (c) alandefreitas 2/18/22.
// See accompanying file LICENSE
//

#ifndef FUTURES_ALIGNED_STORAGE_FOR_H
#define FUTURES_ALIGNED_STORAGE_FOR_H

#include <futures/detail/utility/byte.hpp>
#include <algorithm>

namespace futures::detail {
template <class... Ts>
struct aligned_storage_for
{
public:
constexpr byte*
data() {
return data_;
}

[[nodiscard]] constexpr byte*
data() const {
return data_;
}

[[nodiscard]] constexpr std::size_t
size() const {
return (std::max)({ sizeof(Ts)... });
}

private:
alignas(Ts...) byte data_[(std::max)({ sizeof(Ts)... })];
};
} // namespace futures::detail

#endif // FUTURES_ALIGNED_STORAGE_FOR_H
96 changes: 96 additions & 0 deletions include/futures/detail/utility/byte.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//
// Copyright (c) alandefreitas 2/18/22.
// See accompanying file LICENSE
//

#ifndef FUTURES_BYTE_HPP
#define FUTURES_BYTE_HPP

#include <type_traits>

namespace futures::detail {
enum class byte : unsigned char
{};

template <
class IntegerType,
std::enable_if_t<std::is_integral_v<IntegerType>, int> = 0>
constexpr IntegerType
to_integer(byte b) noexcept {
return IntegerType(b);
}

template <
class IntegerType,
std::enable_if_t<std::is_integral_v<IntegerType>, int> = 0>
constexpr byte&
operator<<=(byte& b, IntegerType shift) noexcept {
return b = b << shift;
}

template <
class IntegerType,
std::enable_if_t<std::is_integral_v<IntegerType>, int> = 0>
constexpr byte&
operator>>=(byte& b, IntegerType shift) noexcept {
return b = b >> shift;
}

template <
class IntegerType,
std::enable_if_t<std::is_integral_v<IntegerType>, int> = 0>
constexpr byte
operator<<(byte b, IntegerType shift) noexcept {
return byte(static_cast<unsigned int>(b) << shift);
}

template <
class IntegerType,
std::enable_if_t<std::is_integral_v<IntegerType>, int> = 0>
constexpr byte
operator>>(byte b, IntegerType shift) noexcept {
return byte(static_cast<unsigned int>(b) >> shift);
}

constexpr byte
operator|(byte l, byte r) noexcept {
return byte(
static_cast<unsigned int>(l) | static_cast<unsigned int>(r));
}

constexpr byte
operator&(byte l, byte r) noexcept {
return byte(
static_cast<unsigned int>(l) & static_cast<unsigned int>(r));
}

constexpr byte
operator^(byte l, byte r) noexcept {
return byte(
static_cast<unsigned int>(l) ^ static_cast<unsigned int>(r));
}

constexpr byte
operator~(byte b) noexcept {
return byte(~static_cast<unsigned int>(b));
}

constexpr byte&
operator|=(byte& l, byte r) noexcept {
return l = l | r;
}

constexpr byte&
operator&=(byte& l, byte r) noexcept {
return l = l & r;
}

constexpr byte&
operator^=(byte& l, byte r) noexcept {
return l = l ^ r;
}


} // namespace futures::detail

#endif // FUTURES_BYTE_HPP

0 comments on commit 4c7e1f1

Please sign in to comment.