• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_VERIFICATION_UTIL_INVALID_REF_H_
17 #define PANDA_VERIFICATION_UTIL_INVALID_REF_H_
18 
19 namespace panda::verifier {
20 
21 template <typename T>
22 struct InvalidRef {
23     operator T &() const
24     {
25         union {
26             void *ptr;
27             T &(InvalidRef::*m_ptr)() const;
28         } u;
29         u.m_ptr = &InvalidRef::operator T &;
30         T *t = reinterpret_cast<T *>(u.ptr);
31         return static_cast<T &>(*t);
32     }
33     bool operator==(const T &t) const
34     {
35         return &t == &operator T &();
36     }
37 };
38 
39 template <typename T>
Invalid()40 T &Invalid()
41 {
42     return InvalidRef<T>();
43 }
44 
45 template <typename T>
Invalid(const T & t)46 bool Invalid(const T &t)
47 {
48     return InvalidRef<T>() == t;
49 }
50 
51 template <typename T>
Valid(const T & t)52 bool Valid(const T &t)
53 {
54     return !Invalid<T>(t);
55 }
56 }  // namespace panda::verifier
57 
58 #endif  // PANDA_VERIFICATION_UTIL_INVALID_REF_H_
59