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