Comms CCF
This is a simple communication layer based on COBS, CBOR and FNV-1A
Loading...
Searching...
No Matches
comptime_str.hpp
Go to the documentation of this file.
1
14
15#include <stddef.h>
16
17#include <algorithm>
18#include <array>
19#include <string_view>
20
21template<size_t Size>
22struct CompTimeString
23{
24 static constexpr size_t size = Size;
25
26 operator std::string_view() const { return std::string_view{buf.data(), Size - 1}; }
27
28 constexpr CompTimeString() { }
29 constexpr CompTimeString(const char (&buf_)[Size])
30 {
31 for (size_t i = 0; i < Size; ++i)
32 {
33 buf[i] = buf_[i];
34 }
35 }
36 constexpr CompTimeString(const std::array<char, Size> & buf_)
37 {
38 for (size_t i = 0; i < Size; ++i)
39 {
40 buf[i] = buf_[i];
41 }
42 }
43
44 template<size_t Other>
45 constexpr CompTimeString<Size - 1 + Other>
46 operator+(const CompTimeString<Other> & other) const
47 {
48 CompTimeString<Size - 1 /* null */ + Other> ret{};
49 auto it = std::data(ret.buf);
50 it = std::copy_n(std::data(buf), Size - 1, it);
51 it = std::copy_n(std::data(other.buf), Other - 1, it);
52 ret.buf[decltype(ret)::size - 1] = 0;
53 return ret;
54 }
55
56 template<size_t Other>
57 constexpr CompTimeString<Size - 1 + Other + 2>
58 operator,(const CompTimeString<Other> & other) const
59 {
60 CompTimeString<Size - 1 /* null */ + Other + 2 /* ", " */> ret{};
61 auto it = std::data(ret.buf);
62 it = std::copy_n(std::data(buf), Size - 1, it);
63 it = std::copy_n(std::data(", "), 2, it);
64 it = std::copy_n(std::data(other.buf), Other - 1, it);
65 ret.buf[decltype(ret)::size - 1] = 0;
66 return ret;
67 }
68
69 std::array<char, Size> buf;
70};