diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-01-03 18:28:10 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-01-03 18:28:10 +0000 |
commit | b1add835864a5907dc5a674f7eee2f3db038f704 (patch) | |
tree | c2ab5ee08422f5a9bad406e4f2b28534fb3e9cb9 | |
parent | Add transform_array (diff) | |
download | ilt-b1add835864a5907dc5a674f7eee2f3db038f704.tar.bz2 ilt-b1add835864a5907dc5a674f7eee2f3db038f704.tar.xz ilt-b1add835864a5907dc5a674f7eee2f3db038f704.zip |
More complete and tested stream output support
-rw-r--r-- | lib/stream_support.hpp | 52 | ||||
-rw-r--r-- | test/test-text.cpp | 24 |
2 files changed, 64 insertions, 12 deletions
diff --git a/lib/stream_support.hpp b/lib/stream_support.hpp index 449708c..932f7c4 100644 --- a/lib/stream_support.hpp +++ b/lib/stream_support.hpp @@ -3,30 +3,49 @@ #include <glm/glm.hpp> #include <iostream> #include <maths.h> +#include <span> +#include <sstream> namespace std { + template<typename T, std::size_t L> + std::ostream & + operator<<(std::ostream & s, const span<const T, L> v) + { + s << '('; + for (const auto & i : v) { + if (&i != &v.front()) + s << ", "; + s << i; + } + return s << ')'; + } + template<glm::length_t L, glm::length_t R, typename T, glm::qualifier Q> std::ostream & operator<<(std::ostream & s, const glm::mat<L, R, T, Q> & m) { - for (int y = 0; y < m.length(); y++) { - const auto & col = m[y]; - for (int x = 0; x < col.length(); x++) { - s << col[x] << ", "; - } - s << "\n"; - } - return s; + return (s << std::span {&m[0], L}); } template<glm::length_t L, typename T, glm::qualifier Q> std::ostream & operator<<(std::ostream & s, const glm::vec<L, T, Q> & v) { - for (int x = 0; x < L; x++) { - s << v[x] << ", "; - } - return s; + 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> + std::ostream & + operator<<(std::ostream & s, const vector<T> & v) + { + return (s << std::span {v}); } inline std::ostream & @@ -36,4 +55,13 @@ namespace std { } } +template<typename T> +std::string +streamed_string(const T & v) +{ + std::stringstream ss; + ss << v; + return ss.str(); +} + #define CLOG(x) std::clog << #x " : " << x << "\n"; diff --git a/test/test-text.cpp b/test/test-text.cpp index 93a831e..6762b09 100644 --- a/test/test-text.cpp +++ b/test/test-text.cpp @@ -5,7 +5,11 @@ #include <boost/test/unit_test.hpp> #include <stream_support.hpp> +#include <array> +#include <glm/glm.hpp> +#include <span> #include <ui/font.h> +#include <vector> struct FontTest : public Font { FontTest() : Font {"/usr/share/fonts/corefonts/arial.ttf", 48} { } @@ -89,3 +93,23 @@ BOOST_AUTO_TEST_CASE(render_text) } BOOST_AUTO_TEST_SUITE_END() + +BOOST_AUTO_TEST_CASE(stream_vec) +{ + BOOST_CHECK_EQUAL(streamed_string(glm::vec3 {1.2, 2.3, 3.4}), "(1.2, 2.3, 3.4)"); +} + +BOOST_AUTO_TEST_CASE(stream_array) +{ + BOOST_CHECK_EQUAL(streamed_string(std::array {1.2, 2.3, 3.4}), "(1.2, 2.3, 3.4)"); +} + +BOOST_AUTO_TEST_CASE(stream_vector) +{ + BOOST_CHECK_EQUAL(streamed_string(std::vector {1.2, 2.3, 3.4}), "(1.2, 2.3, 3.4)"); +} + +BOOST_AUTO_TEST_CASE(stream_mat) +{ + BOOST_CHECK_EQUAL(streamed_string(glm::mat2 {1.2, 2.3, 3.4, 4.5}), "((1.2, 2.3), (3.4, 4.5))"); +} |