blob: e8ca0ba83626fb1e3d8c729b589e4c36a437c46f [file] [log] [blame]
Kevin DuBoisa41be6a2019-09-09 11:06:33 -07001/*
2 * Copyright 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18namespace android {
19
20template <typename T, template <typename> class AbilityType>
21struct Ability {
22 T& base() { return static_cast<T&>(*this); }
23 T const& base() const { return static_cast<T const&>(*this); }
24};
25
26template <typename T>
27struct Add : Ability<T, Add> {
28 inline T operator+(T const& other) const { return T(this->base().value() + other.value()); }
29 inline T& operator++() {
30 ++this->base().value();
31 return this->base();
32 };
33 inline T operator++(int) {
34 T tmp(this->base());
35 operator++();
36 return tmp;
37 };
38 inline T& operator+=(T const& other) {
39 this->base().value() += other.value();
40 return this->base();
41 };
42};
43
44template <typename T>
45struct Compare : Ability<T, Compare> {
46 inline bool operator==(T const& other) const { return this->base().value() == other.value(); };
47 inline bool operator<(T const& other) const { return this->base().value() < other.value(); }
48 inline bool operator<=(T const& other) const { return (*this < other) || (*this == other); }
49 inline bool operator!=(T const& other) const { return !(*this == other); }
50 inline bool operator>=(T const& other) const { return !(*this < other); }
51 inline bool operator>(T const& other) const { return !(*this < other || *this == other); }
52};
53
Ady Abraham2139f732019-11-13 18:56:40 -080054template <typename T>
55struct Hash : Ability<T, Hash> {
56 [[nodiscard]] std::size_t hash() const {
57 return std::hash<typename std::remove_const<
58 typename std::remove_reference<decltype(this->base().value())>::type>::type>{}(
59 this->base().value());
60 }
61};
62
Kevin DuBoisa41be6a2019-09-09 11:06:33 -070063template <typename T, typename W, template <typename> class... Ability>
64struct StrongTyping : Ability<StrongTyping<T, W, Ability...>>... {
65 StrongTyping() : mValue(0) {}
66 explicit StrongTyping(T const& value) : mValue(value) {}
67 StrongTyping(StrongTyping const&) = default;
68 StrongTyping& operator=(StrongTyping const&) = default;
Ady Abraham2139f732019-11-13 18:56:40 -080069 explicit inline operator T() const { return mValue; }
Kevin DuBoisa41be6a2019-09-09 11:06:33 -070070 T const& value() const { return mValue; }
71 T& value() { return mValue; }
72
73private:
74 T mValue;
75};
76} // namespace android
Ady Abraham2139f732019-11-13 18:56:40 -080077
78namespace std {
79template <typename T, typename W, template <typename> class... Ability>
80struct hash<android::StrongTyping<T, W, Ability...>> {
81 std::size_t operator()(android::StrongTyping<T, W, Ability...> const& k) const {
82 return k.hash();
83 }
84};
85} // namespace std