From 1891313b03ff0c375f00ef3da674f2f925a0ae56 Mon Sep 17 00:00:00 2001 From: Sergey Magidovich Date: Tue, 23 Aug 2016 14:04:00 +0300 Subject: [PATCH] Add simple output. --- base/base_tests/newtype_test.cpp | 15 +++++++++++++++ base/newtype.hpp | 26 +++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/base/base_tests/newtype_test.cpp b/base/base_tests/newtype_test.cpp index 0a5741eb11..e912b557d1 100644 --- a/base/base_tests/newtype_test.cpp +++ b/base/base_tests/newtype_test.cpp @@ -96,4 +96,19 @@ UNIT_TEST(NewTypeMember_Operations) a ^= Int(7); TEST_EQUAL(a, Int(10 ^ 7), ()); } + +namespace test_output +{ +NEWTYPE_SIMPLE_OUTPUT(Int); +} // namespace test_output + +UNIT_TEST(NewType_SimpleOutPut) +{ + using namespace test_output; + TEST_EQUAL(test_output::DebugPrint(Int(10)), "10", ()); + + ostringstream sstr; + sstr << Int(20); + TEST_EQUAL(sstr.str(), "20", ()); +} } // namespace diff --git a/base/newtype.hpp b/base/newtype.hpp index 0405e9866a..5d435eda24 100644 --- a/base/newtype.hpp +++ b/base/newtype.hpp @@ -1,5 +1,6 @@ #pragma once +#include "std/iostream.hpp" #include "std/type_traits.hpp" namespace my @@ -18,11 +19,15 @@ class NewType "NewType can be used only with integral and floating point type."); public: + using RepType = Type; + template = nullptr> - explicit NewType(V const & v) : m_value(v) + constexpr explicit NewType(V const & v) : m_value(v) { } + constexpr NewType() = default; + template = nullptr> NewType & Set(V const & v) { @@ -135,8 +140,27 @@ public: private: Type m_value; }; + +namespace newtype_default_output +{ +template +string SimpleDebugPrint(NewType const & nt) +{ + return DebugPrint(nt.Get()); +} +} // namespace newtype_default_output } // namespace my #define NEWTYPE(REPR, NAME) \ struct NAME ## _tag; \ using NAME = my::NewType + +#define NEWTYPE_SIMPLE_OUTPUT(NAME) \ + inline string DebugPrint(NAME const & nt) \ + { \ + return my::newtype_default_output::SimpleDebugPrint(nt); \ + } \ + inline ostream & operator<<(ostream & ost, NAME const & nt) \ + { \ + return ost << my::newtype_default_output::SimpleDebugPrint(nt); \ + }