From 2ebb5d337b085a4c7f21ffcd0e63f969272285ce Mon Sep 17 00:00:00 2001 From: Felix Morgner Date: Sat, 28 Dec 2019 19:35:55 +0100 Subject: new_type: implement stream io operators --- include/newtype/new_type.hpp | 51 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/newtype/new_type.hpp b/include/newtype/new_type.hpp index 17959ba..781c7a1 100644 --- a/include/newtype/new_type.hpp +++ b/include/newtype/new_type.hpp @@ -5,6 +5,8 @@ #include "newtype/deriving.hpp" #include "newtype/type.hpp" +#include +#include #include namespace nt @@ -44,7 +46,7 @@ namespace nt return m_value; } - private: + protected: BaseType m_value{}; }; @@ -80,7 +82,7 @@ namespace nt return m_value; } - private: + protected: BaseType m_value; }; @@ -94,15 +96,29 @@ namespace nt template class new_type : public impl::new_type_storage { - using impl::new_type_storage::new_type_storage; + using storage = impl::new_type_storage; + + using storage::storage; using base_type = BaseType; using tag_type = TagType; auto constexpr static derivations = DerivationClause; + template + auto friend operator>>(std::basic_istream & input, + new_type & target) noexcept(noexcept(std::declval() >> + std::declval())) + -> std::basic_istream &; + public: - using impl::new_type_storage::decay; + using storage::decay; /** * Convert this instance into the equivalent base type value @@ -217,6 +233,33 @@ namespace nt return lhs.decay() >= rhs.decay(); } + template, + typename StreamType = std::basic_ostream> + auto operator<<(std::basic_ostream & output, + new_type const & source) noexcept(noexcept(std::declval() + << std::declval())) + -> std::basic_ostream & + { + return output << source.decay(); + } + + template, + typename StreamType = std::basic_istream> + auto operator>>(std::basic_istream & input, new_type & target) noexcept( + noexcept(std::declval() >> std::declval())) -> std::basic_istream & + { + return input >> target.m_value; + } } // namespace nt #endif -- cgit v1.2.3