emlabcpp
modern opinionated embedded C++ library
page.h
Go to the documentation of this file.
1 #pragma once
23 
24 #include "../../result.h"
25 #include "./base.h"
26 
27 #include <cstdint>
28 #include <optional>
29 
30 namespace emlabcpp::cfg
31 {
32 
33 template < typename T >
34 using opt = std::optional< T >;
35 
36 enum class hdr_state : uint8_t
37 {
38  A = 0x40,
39  B = A + 0x40,
40  C = B + 0x40,
41 };
42 
43 inline hdr_state next( hdr_state cs ) noexcept
44 {
45  switch ( cs ) {
46  case hdr_state::A:
47  return hdr_state::B;
48  case hdr_state::B:
49  return hdr_state::C;
50  case hdr_state::C:
51  return hdr_state::A;
52  default:
53  return cs;
54  }
55 }
56 
58 {
59  switch ( b ) {
60  case std::byte{ static_cast< uint8_t >( hdr_state::A ) }:
61  return hdr_state::A;
62  case std::byte{ static_cast< uint8_t >( hdr_state::B ) }:
63  return hdr_state::B;
64  case std::byte{ static_cast< uint8_t >( hdr_state::C ) }:
65  return hdr_state::C;
66  default:
67  return {};
68  }
69 }
70 
71 inline opt< hdr_state > hdr_to_hdr_state( std::span< std::byte, cell_size > b ) noexcept
72 {
73  if ( b[0] != ~b[1] )
74  return {};
75  return byte_to_hdr( b[0] );
76 }
77 
78 inline std::array< std::byte, 2 > get_hdr( hdr_state hst ) noexcept
79 {
80  std::array< std::byte, 2 > b;
81  b[0] = static_cast< std::byte >( hst );
82  b[1] = ~static_cast< std::byte >( hst );
83  return b;
84 }
85 
86 } // namespace emlabcpp::cfg
MIT License.
Definition: base.h:25
std::optional< T > opt
Definition: handler.h:37
std::array< std::byte, 2 > get_hdr(hdr_state hst) noexcept
Definition: page.h:78
hdr_state
Definition: page.h:37
hdr_state next(hdr_state cs) noexcept
Definition: page.h:43
opt< hdr_state > hdr_to_hdr_state(std::span< std::byte, cell_size > b) noexcept
Definition: page.h:71
opt< hdr_state > byte_to_hdr(std::byte b) noexcept
Definition: page.h:57
physical_quantity< 0, 0, 0, 0, 0, 0, 0, 0, 1 > byte
Definition: physical_quantity.h:118