summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-08-29 12:54:18 +0100
committerDan Goodliffe <dan@randomdan.homeip.net>2022-08-29 12:54:18 +0100
commit8dfe3d339dc1542ee7bd59c000302765648e972c (patch)
tree791e61bfe13cdee69206478317cfcf1170412ff0 /lib
parentUse utf8_string_view for processing text to renderings (diff)
downloadilt-8dfe3d339dc1542ee7bd59c000302765648e972c.tar.bz2
ilt-8dfe3d339dc1542ee7bd59c000302765648e972c.tar.xz
ilt-8dfe3d339dc1542ee7bd59c000302765648e972c.zip
Generic solution for streaming collections that can be spanned
Diffstat (limited to 'lib')
-rw-r--r--lib/stream_support.cpp11
-rw-r--r--lib/stream_support.hpp18
2 files changed, 19 insertions, 10 deletions
diff --git a/lib/stream_support.cpp b/lib/stream_support.cpp
new file mode 100644
index 0000000..2054873
--- /dev/null
+++ b/lib/stream_support.cpp
@@ -0,0 +1,11 @@
+#include "stream_support.hpp"
+
+#include <array>
+#include <vector>
+
+static_assert(spanable<std::vector<int>>);
+// static_assert(spanable<std::vector<char>>);
+static_assert(spanable<std::array<int, 1>>);
+// static_assert(spanable<std::array<char, 1>>);
+static_assert(!spanable<std::string>);
+static_assert(!spanable<std::string_view>);
diff --git a/lib/stream_support.hpp b/lib/stream_support.hpp
index 0552b9d..3f0165c 100644
--- a/lib/stream_support.hpp
+++ b/lib/stream_support.hpp
@@ -5,6 +5,11 @@
#include <maths.h>
#include <span>
#include <sstream>
+#include <type_traits>
+
+template<typename T>
+concept spanable = std::is_constructible_v<std::span<const typename T::value_type>, T> && !std::is_same_v<char,
+ std::decay_t<typename T::value_type>>;
namespace std {
template<typename T, std::size_t L>
@@ -34,16 +39,9 @@ namespace std {
return (s << std::span {&v[0], L});
}
- template<typename T, std::size_t L>
- std::ostream &
- operator<<(std::ostream & s, const array<T, L> & v)
- {
- return (s << std::span {v});
- }
-
- template<typename T>
+ template<spanable T>
std::ostream &
- operator<<(std::ostream & s, const vector<T> & v)
+ operator<<(std::ostream & s, const T & v)
{
return (s << std::span {v});
}
@@ -61,7 +59,7 @@ streamed_string(const T & v)
{
std::stringstream ss;
ss << v;
- return ss.str();
+ return std::move(ss).str();
}
#define CLOG(x) std::cerr << #x " : " << x << "\n";