OGLplus  (0.59.0) a C++ wrapper for rendering APIs

str_format.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_STR_FORMAT_HPP
10 #define EAGINE_STR_FORMAT_HPP
11 
12 #include "config/basic.hpp"
13 #include "integer_range.hpp"
14 #include "span.hpp"
15 #include <array>
16 #include <string>
17 
18 namespace eagine {
19 //------------------------------------------------------------------------------
20 class format_string_and_list_base {
21 
22 protected:
23  format_string_and_list_base(std::string&& fmt_str) noexcept
24  : _fmt_str(std::move(fmt_str)) {}
25 
26  format_string_and_list_base(format_string_and_list_base& that) noexcept
27  : _fmt_str(std::move(that._fmt_str)) {}
28 
29  format_string_and_list_base(format_string_and_list_base&&) noexcept =
30  default;
31  format_string_and_list_base(const format_string_and_list_base&) = default;
32 
33  auto operator=(format_string_and_list_base&&) noexcept
34  -> format_string_and_list_base& = default;
35 
36  auto operator=(const format_string_and_list_base&)
37  -> format_string_and_list_base& = default;
38 
39  ~format_string_and_list_base() noexcept = default;
40 
41  auto _fmt(span<const std::string> values) const -> std::string;
42 
43 private:
44  std::string _fmt_str;
45 };
46 //------------------------------------------------------------------------------
47 template <span_size_t N>
49 
50 template <>
51 class format_string_and_list<0> : public format_string_and_list_base {
52 public:
53  format_string_and_list(std::string& fmt_str) noexcept
54  : format_string_and_list_base(std::move(fmt_str)) {}
55 
56  operator std::string() const {
57  return _fmt({});
58  }
59 };
60 //------------------------------------------------------------------------------
64 template <span_size_t N>
65 class format_string_and_list : public format_string_and_list_base {
66 
67 public:
70  std::string&& val) noexcept
71  : format_string_and_list_base(prev) {
72  for(auto i : integer_range(N - 1)) {
73  _list[i] = std::move(prev._list[i]);
74  }
75  _list[N - 1] = std::move(val);
76  }
77 
79  operator std::string() const {
80  return _fmt(view(_list));
81  }
82 
83 public:
84  std::array<std::string, N> _list{};
85 };
86 //------------------------------------------------------------------------------
87 template <>
88 class format_string_and_list<1> : public format_string_and_list_base {
89 
90 public:
91  format_string_and_list(
92  format_string_and_list<0>&& prev,
93  std::string&& val) noexcept
94  : format_string_and_list_base(prev) {
95  _list[0] = std::move(val);
96  }
97 
98  operator std::string() const {
99  return _fmt(view(_list));
100  }
101 
102 public:
103  std::array<std::string, 1> _list{};
104 };
105 //------------------------------------------------------------------------------
109 template <span_size_t N>
110 static inline auto
111 operator%(format_string_and_list<N>&& fsal, std::string&& val) noexcept
113  return {std::move(fsal), std::move(val)};
114 }
115 //------------------------------------------------------------------------------
118 static inline auto format(std::string&& fmt_str) noexcept
120  return {fmt_str};
121 }
122 //------------------------------------------------------------------------------
123 } // namespace eagine
124 
125 #if !EAGINE_LINK_LIBRARY || defined(EAGINE_IMPLEMENTING_LIBRARY)
126 #include <eagine/str_format.inl>
127 #endif
128 
129 #endif // EAGINE_STR_FORMAT_HPP
std::ptrdiff_t span_size_t
Signed span size type used by eagine.
Definition: types.hpp:36
Common code is placed in this namespace.
Definition: eagine.hpp:21
static constexpr auto view(T *addr, S size) noexcept -> const_span< T >
Creates a view starting at the specified pointer and specified length.
Definition: span.hpp:458
basic_span< T, T *, S > span
Default alias for basic memory spans with native pointer type.
Definition: span.hpp:415
Helper class used in implementation of string variable substitution.
Definition: str_format.hpp:48
static auto format(std::string &&fmt_str) noexcept -> format_string_and_list< 0 >
Function taking a format string, returning an object for variable specification.
Definition: str_format.hpp:118
static auto operator%(format_string_and_list< N > &&fsal, std::string &&val) noexcept -> format_string_and_list< N+1 >
Operator for adding variable name and value for string formatting.
Definition: str_format.hpp:111
integer_range(B, E) -> integer_range< std::common_type_t< B, E >>
Deduction guide for integer_range.

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).