2019-07-27 13:34:07 +02:00
|
|
|
#pragma once
|
|
|
|
|
2022-06-03 16:54:28 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#include <cassert>
|
2022-06-02 21:37:58 +02:00
|
|
|
#include <cstddef>
|
|
|
|
|
2019-07-27 13:34:07 +02:00
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
|
2022-06-03 16:54:28 +02:00
|
|
|
#include "../util/util.hpp"
|
|
|
|
|
2019-07-28 17:30:52 +02:00
|
|
|
#define F(str) (reinterpret_cast<const ::detail::FlashString *>(PSTR(str)))
|
2020-04-01 03:52:49 +02:00
|
|
|
#define GF(name, str) \
|
|
|
|
const char __##name[] PROGMEM = str; \
|
|
|
|
const auto *name = reinterpret_cast<const ::detail::FlashString *>(__##name)
|
2019-07-27 13:34:07 +02:00
|
|
|
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
// Only used for C++ type safety, because otherwise a PSTR would be indistinguishable from a normal c-string
|
|
|
|
struct FlashString;
|
|
|
|
|
|
|
|
} // namespace detail
|
2022-06-02 21:37:58 +02:00
|
|
|
|
|
|
|
namespace flash {
|
|
|
|
|
|
|
|
template <typename T>
|
2022-06-03 16:54:28 +02:00
|
|
|
static inline T load(const T &object)
|
2022-06-02 21:37:58 +02:00
|
|
|
{
|
2022-06-03 16:54:28 +02:00
|
|
|
T buffer;
|
|
|
|
auto byteBuffer = reinterpret_cast<std::byte *>(&buffer);
|
2022-06-02 21:37:58 +02:00
|
|
|
for (auto i = std::size_t{0}; i < sizeof(T); ++i) {
|
2022-06-03 16:54:28 +02:00
|
|
|
byteBuffer[i] = static_cast<std::byte>(pgm_read_byte(&reinterpret_cast<const std::byte *>(&object)[i]));
|
2022-06-02 21:37:58 +02:00
|
|
|
}
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2022-06-03 16:54:28 +02:00
|
|
|
template <typename T>
|
|
|
|
struct [[gnu::progmem]] Wrapper
|
|
|
|
{
|
|
|
|
using value_type = T;
|
|
|
|
|
|
|
|
inline T operator*() const
|
|
|
|
{
|
|
|
|
return load(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::byte operator[](const std::size_t idx) const
|
|
|
|
{
|
|
|
|
const auto bytePtr = reinterpret_cast<const std::byte *>(&value);
|
|
|
|
return load(bytePtr[idx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
const T value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct RamWrapper {
|
|
|
|
using value_type = T;
|
|
|
|
|
|
|
|
inline constexpr T &operator*()
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline constexpr const T &operator*() const
|
|
|
|
{
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::byte &operator[](const std::size_t idx)
|
|
|
|
{
|
|
|
|
const auto bytePtr = reinterpret_cast<std::byte *>(&value);
|
|
|
|
return bytePtr[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
inline const std::byte &operator[](const std::size_t idx) const
|
|
|
|
{
|
|
|
|
const auto bytePtr = reinterpret_cast<const std::byte *>(&value);
|
|
|
|
return bytePtr[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
T value;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename WrapperT>
|
|
|
|
struct is_flash_wrapper : std::false_type {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct is_flash_wrapper<Wrapper<T>> : std::true_type {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename WrapperT>
|
|
|
|
static inline constexpr auto is_flash_wrapper_v = is_flash_wrapper<WrapperT>::value;
|
|
|
|
|
|
|
|
template <typename WrapperT>
|
|
|
|
struct is_ram_wrapper : std::false_type {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct is_ram_wrapper<RamWrapper<T>> : std::true_type {
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename WrapperT>
|
|
|
|
static inline constexpr auto is_ram_wrapper_v = is_ram_wrapper<WrapperT>::value;
|
|
|
|
|
|
|
|
template <typename WrapperT, typename T>
|
|
|
|
static inline decltype(auto) loadLike(const T &object)
|
|
|
|
{
|
|
|
|
if constexpr (is_ram_wrapper_v<std::remove_cvref_t<WrapperT>>) {
|
|
|
|
return object;
|
|
|
|
} else if constexpr (is_flash_wrapper_v<std::remove_cvref_t<WrapperT>>) {
|
|
|
|
return load(object);
|
|
|
|
} else {
|
|
|
|
static_assert(util::always_false_v<T>, "Invalid wrapper type");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-02 21:37:58 +02:00
|
|
|
} // namespace flash
|