Merge pull request #8394 from Pokechu22/misc-di-gpio

Various DI improvements
This commit is contained in:
JosJuice
2020-01-13 17:17:24 +01:00
committed by GitHub
20 changed files with 1554 additions and 517 deletions

View File

@ -8,6 +8,7 @@
#include <climits>
#include <cstddef>
#include <cstring>
#include <initializer_list>
#include <type_traits>
namespace Common
@ -299,4 +300,44 @@ void SetBit(T& value, size_t bit_number, bool bit_value)
value &= ~(T{1} << bit_number);
}
template <typename T>
class FlagBit
{
public:
FlagBit(std::underlying_type_t<T>& bits, T bit) : m_bits(bits), m_bit(bit) {}
explicit operator bool() const
{
return (m_bits & static_cast<std::underlying_type_t<T>>(m_bit)) != 0;
}
FlagBit& operator=(const bool rhs)
{
if (rhs)
m_bits |= static_cast<std::underlying_type_t<T>>(m_bit);
else
m_bits &= ~static_cast<std::underlying_type_t<T>>(m_bit);
return *this;
}
private:
std::underlying_type_t<T>& m_bits;
T m_bit;
};
template <typename T>
class Flags
{
public:
constexpr Flags() = default;
constexpr Flags(std::initializer_list<T> bits)
{
for (auto bit : bits)
{
m_hex |= static_cast<std::underlying_type_t<T>>(bit);
}
}
FlagBit<T> operator[](T bit) { return FlagBit(m_hex, bit); }
std::underlying_type_t<T> m_hex = 0;
};
} // namespace Common

View File

@ -19,6 +19,7 @@
#include <deque>
#include <list>
#include <map>
#include <optional>
#include <set>
#include <string>
#include <type_traits>
@ -144,6 +145,36 @@ public:
Do(x.second);
}
template <typename T>
void Do(std::optional<T>& x)
{
bool present = x.has_value();
Do(present);
switch (mode)
{
case MODE_READ:
if (present)
{
x = std::make_optional<T>();
Do(x.value());
}
else
{
x = std::nullopt;
}
break;
case MODE_WRITE:
case MODE_MEASURE:
case MODE_VERIFY:
if (present)
Do(x.value());
break;
}
}
template <typename T, std::size_t N>
void DoArray(std::array<T, N>& x)
{