1 /** 2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef _PANDA_VERIFIER_ABSTRACT_INDEX_HPP 17 #define _PANDA_VERIFIER_ABSTRACT_INDEX_HPP 18 19 #include "index.h" 20 #include "util/hash.h" 21 22 #include <limits> 23 24 namespace panda::verifier { 25 template <typename Int, typename Friend> 26 class AbstractIndex : private Index<Int> { 27 using Base = Index<Int>; 28 29 public: 30 AbstractIndex() = default; 31 AbstractIndex(const AbstractIndex &) = default; 32 AbstractIndex(AbstractIndex &&) = default; 33 AbstractIndex &operator=(const AbstractIndex &) = default; 34 AbstractIndex &operator=(AbstractIndex &&) = default; 35 ~AbstractIndex() = default; 36 IsValid()37 bool IsValid() const 38 { 39 return Base::IsValid(); 40 } 41 42 bool operator==(const AbstractIndex &rhs) const 43 { 44 return static_cast<Int>(static_cast<const Base &>(*this)) == static_cast<Int>(static_cast<const Base &>(rhs)); 45 } 46 47 bool operator!=(const AbstractIndex &rhs) const 48 { 49 return !operator==(rhs); 50 } 51 52 bool operator<(const AbstractIndex &rhs) const 53 { 54 return static_cast<Int>(static_cast<const Base &>(*this)) < static_cast<Int>(static_cast<const Base &>(rhs)); 55 } 56 57 bool operator<=(const AbstractIndex &rhs) const 58 { 59 return static_cast<Int>(static_cast<const Base &>(*this)) <= static_cast<Int>(static_cast<const Base &>(rhs)); 60 } 61 62 private: AbstractIndex(Int val)63 AbstractIndex(Int val) : Base {val} {} 64 65 AbstractIndex &operator=(Int val) 66 { 67 Base::operator=(val); 68 return *this; 69 } 70 Invalidate()71 void Invalidate() 72 { 73 Base::Invalidate(); 74 } 75 Int()76 operator Int() const 77 { 78 return Base::operator Int(); 79 } 80 81 template <typename T> 82 friend struct std::hash; 83 84 friend Friend; 85 friend struct std::hash<panda::verifier::AbstractIndex<Int, Friend>>; 86 }; 87 } // namespace panda::verifier 88 89 namespace std { 90 template <typename Int, typename Friend> 91 struct hash<panda::verifier::AbstractIndex<Int, Friend>> { 92 size_t operator()(const panda::verifier::AbstractIndex<Int, Friend> &i) const noexcept 93 { 94 return panda::verifier::StdHash(i.operator Int()); 95 } 96 }; 97 } // namespace std 98 99 #endif // !_PANDA_VERIFIER_ABSTRACT_INDEX_HPP 100