From f94f0ec7c91f937af59a6ee3e295d26cfe95d8e3 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Sat, 2 Apr 2016 13:37:58 +1100 Subject: [PATCH] colour: add istream operator --- colour.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ colour.hpp | 3 +++ 2 files changed, 50 insertions(+) diff --git a/colour.cpp b/colour.cpp index a60b8259..9eade608 100644 --- a/colour.cpp +++ b/colour.cpp @@ -391,6 +391,53 @@ util::operator<< (std::ostream &os, util::colour c) { //----------------------------------------------------------------------------- +template +std::istream& +util::operator>> (std::istream &is, util::colour &c) +{ + std::array< + std::conditional_t< + sizeof(T) == 1, + uint16_t, + T + >,S + > v; + + static_assert (S > 0, "current implementation requires strictly positive components"); + char comma; + + for (size_t i = 0; i < S - 1; ++i) { + is >> v[i] >> comma; + + if (comma != ',' || is.eof ()) { + is.setstate (std::ios_base::failbit); + return is; + } + } + + is >> v[S-1]; + + if (!std::is_same::value) { + if (std::any_of (std::cbegin (v), + std::cend (v), + [] (auto i) { + return i > std::numeric_limits::max (); + })) { + is.setstate (std::ios_base::failbit); + return is; + } + } + + std::copy (std::cbegin (v), + std::cend (v), + std::begin (c)); + + return is; +} + +template std::istream& util::operator>> (std::istream&, util::colour<3,uint8_t>&); + +/////////////////////////////////////////////////////////////////////////////// #define INSTANTIATE_S_T(S,T) \ template struct util::colour; \ template std::ostream& util::operator<< (std::ostream&, util::colour); diff --git a/colour.hpp b/colour.hpp index df491a3a..d0df406e 100644 --- a/colour.hpp +++ b/colour.hpp @@ -61,6 +61,9 @@ namespace util { template std::ostream& operator<< (std::ostream&, util::colour); + + template + std::istream& operator>> (std::istream&, util::colour&); } #include "colour.ipp"