diff options
Diffstat (limited to 'base')
-rw-r--r-- | base/base_tests/newtype_test.cpp | 15 | ||||
-rw-r--r-- | base/newtype.hpp | 26 |
2 files changed, 40 insertions, 1 deletions
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 <typename V, impl::IsConvertibleGuard<V, Type> = nullptr> - explicit NewType(V const & v) : m_value(v) + constexpr explicit NewType(V const & v) : m_value(v) { } + constexpr NewType() = default; + template <typename V, impl::IsConvertibleGuard<V, Type> = nullptr> NewType & Set(V const & v) { @@ -135,8 +140,27 @@ public: private: Type m_value; }; + +namespace newtype_default_output +{ +template <typename Type, typename Tag> +string SimpleDebugPrint(NewType<Type, Tag> const & nt) +{ + return DebugPrint(nt.Get()); +} +} // namespace newtype_default_output } // namespace my #define NEWTYPE(REPR, NAME) \ struct NAME ## _tag; \ using NAME = my::NewType<REPR, NAME ## _tag> + +#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); \ + } |