Mir
int_wrapper.h
Go to the documentation of this file.
1 /*
2  * Copyright © 2012, 2013, 2016 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 2 or 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #ifndef MIR_INT_WRAPPER_H_
18 #define MIR_INT_WRAPPER_H_
19 
20 #include <iosfwd>
21 
22 namespace mir
23 {
24 template<typename Tag, typename ValueType=int>
26 {
27 public:
28  constexpr IntWrapper() : value(0) {}
29 
30  explicit constexpr IntWrapper(ValueType value) : value(value) {}
31  ValueType constexpr as_value() const { return value; }
32 
33 private:
34  ValueType value;
35 };
36 
37 template<typename Tag, typename ValueType>
38 std::ostream& operator<<(std::ostream& out, IntWrapper<Tag,ValueType> const& value)
39 {
40  out << value.as_value();
41  return out;
42 }
43 
44 template<typename Tag, typename ValueType>
45 inline constexpr bool operator == (IntWrapper<Tag,ValueType> const& lhs, IntWrapper<Tag,ValueType> const& rhs)
46 {
47  return lhs.as_value() == rhs.as_value();
48 }
49 
50 template<typename Tag, typename ValueType>
51 inline constexpr bool operator != (IntWrapper<Tag,ValueType> const& lhs, IntWrapper<Tag,ValueType> const& rhs)
52 {
53  return lhs.as_value() != rhs.as_value();
54 }
55 
56 template<typename Tag, typename ValueType>
57 inline constexpr bool operator <= (IntWrapper<Tag,ValueType> const& lhs, IntWrapper<Tag,ValueType> const& rhs)
58 {
59  return lhs.as_value() <= rhs.as_value();
60 }
61 
62 template<typename Tag, typename ValueType>
63 inline constexpr bool operator >= (IntWrapper<Tag,ValueType> const& lhs, IntWrapper<Tag,ValueType> const& rhs)
64 {
65  return lhs.as_value() >= rhs.as_value();
66 }
67 
68 template<typename Tag, typename ValueType>
69 inline constexpr bool operator < (IntWrapper<Tag,ValueType> const& lhs, IntWrapper<Tag,ValueType> const& rhs)
70 {
71  return lhs.as_value() < rhs.as_value();
72 }
73 }
74 
75 #include <functional>
76 namespace std
77 {
78 template<typename Tag, typename ValueType>
79 struct hash< ::mir::IntWrapper<Tag,ValueType> >
80 {
81  std::hash<int> self;
82  constexpr std::size_t operator()(::mir::IntWrapper<Tag,ValueType> const& id) const
83  {
84  return self(id.as_value());
85  }
86 };
87 }
88 
89 #endif // MIR_INT_WRAPPER_H_

Copyright © 2012-2022 Canonical Ltd.
Generated on Tue Sep 13 03:20:30 UTC 2022
This documentation is licensed under the GPL version 2 or 3.