-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
714586b
commit 4c7e1f1
Showing
2 changed files
with
133 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |