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

newtype.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0405e9866a5668f392c30b35c5247a0c6f8b0ee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#pragma once

#include "std/type_traits.hpp"

namespace my
{
namespace impl
{
template <typename From, typename To>
using IsConvertibleGuard = typename enable_if<is_convertible<From, To>::value>::type *;
}  // namespace impl

/// Creates a typesafe alias to a given numeric Type.
template <typename Type, typename Tag>
class NewType
{
  static_assert(is_integral<Type>::value || is_floating_point<Type>::value,
                "NewType can be used only with integral and floating point type.");

public:
  template <typename V, impl::IsConvertibleGuard<V, Type> = nullptr>
  explicit NewType(V const & v) : m_value(v)
  {
  }

  template <typename V, impl::IsConvertibleGuard<V, Type> = nullptr>
  NewType & Set(V const & v)
  {
    m_value = static_cast<Type>(v);
    return *this;
  }

  Type const & Get() const { return m_value; }
  Type & Get() { return m_value; }

  NewType & operator=(NewType const & v)
  {
    m_value = v.m_value;
    return *this;
  }

  NewType & operator++()
  {
    ++m_value;
    return *this;
  }

  NewType const operator++(int)
  {
    auto const copy = *this;
    ++m_value;
    return copy;
  }

  NewType & operator--()
  {
    --m_value;
    return *this;
  }

  NewType const operator--(int)
  {
    auto const copy = *this;
    --m_value;
    return copy;
  }

  NewType & operator+=(NewType const & v)
  {
    m_value += v.m_value;
    return *this;
  }

  NewType & operator-=(NewType const & v)
  {
    m_value -= v.m_value;
    return *this;
  }

  NewType & operator*=(NewType const & v)
  {
    m_value *= v.m_value;
    return *this;
  }

  NewType & operator/=(NewType const & v)
  {
    m_value /= v.m_value;
    return *this;
  }

  NewType & operator%=(NewType const & v)
  {
    m_value %= v.m_value;
    return *this;
  }

  NewType & operator^=(NewType const & v)
  {
    m_value ^= v.m_value;
    return *this;
  }

  NewType & operator|=(NewType const & v)
  {
    m_value |= v.m_value;
    return *this;
  }

  NewType & operator&=(NewType const & v)
  {
    m_value &= v.m_value;
    return *this;
  }

  // TODO(mgsergio): Is it meaningful for a newtype to have << operator ?
  // NewType & operator<<=(NewType<V, VTag> const & v)
  // NewType & operator>>=(NewType<V, VTag> const & v)

  bool operator==(NewType const & o) const { return m_value == o.m_value; }
  bool operator!=(NewType const & o) const { return !(m_value == o.m_value); }
  bool operator<(NewType const & o) const { return m_value < o.m_value; }
  bool operator>(NewType const & o) const { return m_value > o.m_value; }
  bool operator<=(NewType const & o) const { return !(m_value > o.m_value); }
  bool operator>=(NewType const & o) const { return !(m_value < o.m_value); }
  NewType operator+(NewType const & o) const { return NewType(m_value + o.m_value); }
  NewType operator-(NewType const & o) const { return NewType(m_value - o.m_value); }
  NewType operator*(NewType const & o) const { return NewType(m_value * o.m_value); }
  NewType operator/(NewType const & o) const { return NewType(m_value / o.m_value); }
  NewType operator%(NewType const & o) const { return NewType(m_value % o.m_value); }
  NewType operator^(NewType const & o) const { return NewType(m_value ^ o.m_value); }
  NewType operator|(NewType const & o) const { return NewType(m_value | o.m_value); }
  NewType operator&(NewType const & o) const { return NewType(m_value & o.m_value); }

private:
  Type m_value;
};
}  // namespace my

#define NEWTYPE(REPR, NAME)                     \
  struct NAME ## _tag;                          \
  using NAME = my::NewType<REPR, NAME ## _tag>