Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authorVladimir Byko-Ianko <bykoianko@gmail.com>2016-08-23 14:24:06 +0300
committerGitHub <noreply@github.com>2016-08-23 14:24:06 +0300
commitf0a18472ceb132cb50c50fd67a400c1201b68a5c (patch)
treef88fe3a16da499a375f3b79969d6e35e6fb53cf4 /base
parent5fde326cfb29b3ff6271e552d819c2bf16344454 (diff)
parent1891313b03ff0c375f00ef3da674f2f925a0ae56 (diff)
Merge pull request #4133 from mgsergio/strong-typedefs
Add simple output.
Diffstat (limited to 'base')
-rw-r--r--base/base_tests/newtype_test.cpp15
-rw-r--r--base/newtype.hpp26
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); \
+ }