Utilities
Utilities header only library
StrongType.h
1 /*
2  Copyright (C) Skeletex Research, s.r.o. - All Rights Reserved
3  Unauthorized copying of this file, via any medium is strictly prohibited
4  Proprietary and confidential
5 */
6 #ifndef UTILS_STRONG_TYPE_H
7 #define UTILS_STRONG_TYPE_H
8 
9 #include <type_traits>
10 #include <ostream>
11 
12 namespace utils
13 {
27  template <typename ValueType, typename PhantomType>
28  struct StrongType
29  {
30 
31  public:
32  StrongType()
33  : StrongType(static_cast<ValueType>(0))
34  {}
35 
36  explicit StrongType(ValueType value)
37  : value_(value)
38  {}
39 
41  template <typename RT>
42  inline typename std::enable_if_t<std::is_arithmetic_v<RT>, RT> As() const
43  {
44  return static_cast<RT>(value_);
45  }
46 
47  bool operator == (const StrongType &other) const { return (value_ == other.value_); }
48  bool operator != (const StrongType &other) const { return !(value_ == other.value_); }
49  bool operator < (const StrongType &other) const { return (value_ < other.value_); }
50  bool operator > (const StrongType &other) const { return (value_ > other.value_); }
51 
53  const ValueType &operator*() const { return value_; }
54 
55  friend std::ostream &operator<< (std::ostream &out, const StrongType<ValueType, PhantomType> &id)
56  {
57  out << id.value_;
58  return out;
59  }
60 
61  protected:
63  ValueType value_;
64  };
65 }
66 
67 #endif /* !UTILS_STRONG_TYPE_H */
const ValueType & operator*() const
Get underlying value using the * operator.
Definition: StrongType.h:53
Template class implementation of a Named Strong Type.
Definition: StrongType.h:28
std::enable_if_t< std::is_arithmetic_v< RT >, RT > As() const
Cast to a specified type. Only work for arithmetic types.
Definition: StrongType.h:42
ValueType value_
The actual underlying value.
Definition: StrongType.h:63
General namespace for utilities.
Definition: BulletConversions.h:9