emlabcpp
modern opinionated embedded C++ library
string_buffer.h
Go to the documentation of this file.
1 #pragma once
23 
24 #include <algorithm>
25 #include <array>
26 #include <cstring>
27 #include <string_view>
28 
29 #ifdef EMLABCPP_USE_NLOHMANN_JSON
30 #include <nlohmann/json.hpp>
31 #endif
32 
33 namespace emlabcpp
34 {
35 
36 template < std::size_t N >
37 struct string_buffer : std::array< char, N >
38 {
39  static constexpr std::size_t capacity = N;
40  using base_type = std::array< char, N >;
41 
42  constexpr string_buffer()
43  : base_type{}
44  {
45  std::fill_n( this->data(), N, '\0' );
46  }
47 
48  constexpr string_buffer( std::string_view sv )
49  : string_buffer()
50  {
51  std::copy_n( sv.begin(), std::min( sv.size(), N - 1 ), this->begin() );
52  }
53 
54  template < std::size_t M >
55  constexpr string_buffer( char const ( &msg )[M] )
56  : string_buffer()
57  {
58  static_assert( M < N );
59  std::copy_n( msg, M, this->begin() );
60  }
61 
62  template < std::size_t M >
63  constexpr string_buffer( string_buffer< M > const& msg )
64  : string_buffer()
65  {
66  static_assert( M < N );
67  std::copy_n( msg.data(), M, this->begin() );
68  }
69 
70  operator std::string_view() const
71  {
72  return std::string_view( this->data() );
73  }
74 
75  [[nodiscard]] constexpr std::size_t size() const
76  {
77  return strlen( this->data() );
78  }
79 
80  template < std::size_t M >
81  constexpr bool operator==( string_buffer< M > const& other ) const noexcept
82  {
83  return std::string_view{ *this } == std::string_view{ other };
84  }
85 
86  constexpr bool operator==( std::string_view other ) const noexcept
87  {
88  return std::string_view{ *this } == other;
89  }
90 };
91 
92 namespace bits
93 {
94  template < typename T >
96  {
97  static constexpr bool value = false;
98  };
99 
100  template < std::size_t N >
102  {
103  static constexpr bool value = true;
104  };
105 
106 } // namespace bits
107 
108 template < typename T >
110 
111 template < std::size_t N >
112 std::ostream& operator<<( std::ostream& os, string_buffer< N > const& sb )
113 {
114  return os << std::string_view( sb );
115 }
116 
117 #ifdef EMLABCPP_USE_NLOHMANN_JSON
118 
119 template < std::size_t N >
120 void to_json( nlohmann::json& j, string_buffer< N > const& buffer )
121 {
122  j = std::string_view{ buffer };
123 }
124 
125 template < std::size_t N >
126 void from_json( nlohmann::json const& j, string_buffer< N >& buffer )
127 {
128  std::string const s = j;
129  buffer = string_buffer< N >( std::string_view{ s } );
130 }
131 
132 #endif
133 
134 } // namespace emlabcpp
MIT License.
Definition: impl.h:31
constexpr pointer data() noexcept
Returns pointer to first item of the storage.
Definition: static_storage.h:108
concept is_string_buffer_v
Definition: string_buffer.h:109
constexpr Derived const & min(vec_point_base< Derived, N > const &a, vec_point_base< Derived, N > const &b)
Definition: vec_point_base.h:236
std::ostream & operator<<(std::ostream &os, string_buffer< N > const &sb)
Definition: string_buffer.h:112
N
Definition: static_storage.h:97
Definition: string_buffer.h:96
static constexpr bool value
Definition: string_buffer.h:97
Definition: string_buffer.h:38
constexpr string_buffer(std::string_view sv)
Definition: string_buffer.h:48
constexpr bool operator==(string_buffer< M > const &other) const noexcept
Definition: string_buffer.h:81
std::array< char, N > base_type
Definition: string_buffer.h:40
constexpr bool operator==(std::string_view other) const noexcept
Definition: string_buffer.h:86
constexpr string_buffer(char const (&msg)[M])
Definition: string_buffer.h:55
constexpr std::size_t size() const
Definition: string_buffer.h:75
constexpr string_buffer()
Definition: string_buffer.h:42
constexpr string_buffer(string_buffer< M > const &msg)
Definition: string_buffer.h:63
static constexpr std::size_t capacity
Definition: string_buffer.h:39