DiSMEC++
opaque_int.h
Go to the documentation of this file.
1 // Copyright (c) 2021, Aalto University, developed by Erik Schultheis
2 // All rights reserved.
3 //
4 // SPDX-License-Identifier: MIT
5 
6 #ifndef DISMEC_OPAQUE_INT_H
7 #define DISMEC_OPAQUE_INT_H
8 
9 #include <cstdint>
10 #include <type_traits>
11 #include <iosfwd>
12 
13 namespace dismec {
23  template<class Tag, class T = std::int_fast32_t>
25  public:
26  static_assert(std::is_integral_v<T>, "T needs to be an integral type");
27 
29  constexpr explicit opaque_int_type(T v) noexcept: m_Value(v) {}
30 
32  [[nodiscard]] constexpr T to_index() const { return m_Value; }
33 
34  protected:
36  };
37 
38  // define comparison operators
39  template<class Tag, class T>
41  return a.to_index() == b.to_index();
42  }
43 
44  template<class Tag, class T>
46  return a.to_index() != b.to_index();
47  }
48 
49  template<class Tag, class T>
51  return a.to_index() <= b.to_index();
52  }
53 
54  template<class Tag, class T>
56  return a.to_index() < b.to_index();
57  }
58 
59  template<class Tag, class T>
61  return a.to_index() > b.to_index();
62  }
63 
64  template<class Tag, class T>
66  return a.to_index() >= b.to_index();
67  }
68 
69  template<class Tag, class T>
70  std::ostream& operator<<(std::ostream& stream, opaque_int_type<Tag, T> a) {
71  return stream << a.to_index();
72  }
73 }
74 
75 #endif //DISMEC_OPAQUE_INT_H
An integer-like type that represents categorical values.
Definition: opaque_int.h:24
constexpr opaque_int_type(T v) noexcept
! Explicit constructor from an underlying int.
Definition: opaque_int.h:29
constexpr T to_index() const
! Explicitly convert to an integer.
Definition: opaque_int.h:32
Main namespace in which all types, classes, and functions are defined.
Definition: app.h:15
constexpr bool operator<=(opaque_int_type< Tag, T > a, opaque_int_type< Tag, T > b)
Definition: opaque_int.h:50
std::ostream & operator<<(std::ostream &stream, opaque_int_type< Tag, T > a)
Definition: opaque_int.h:70
constexpr bool operator==(opaque_int_type< Tag, T > a, opaque_int_type< Tag, T > b)
Definition: opaque_int.h:40
constexpr bool operator>(opaque_int_type< Tag, T > a, opaque_int_type< Tag, T > b)
Definition: opaque_int.h:60
constexpr bool operator<(opaque_int_type< Tag, T > a, opaque_int_type< Tag, T > b)
Definition: opaque_int.h:55
constexpr bool operator!=(opaque_int_type< Tag, T > a, opaque_int_type< Tag, T > b)
Definition: opaque_int.h:45
constexpr bool operator>=(opaque_int_type< Tag, T > a, opaque_int_type< Tag, T > b)
Definition: opaque_int.h:65