1 /*
2  * Copyright (C) 2016 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 #ifndef ART_RUNTIME_REFLECTIVE_REFERENCE_H_
18 #define ART_RUNTIME_REFLECTIVE_REFERENCE_H_
19 
20 #include "android-base/macros.h"
21 #include "base/macros.h"
22 #include "mirror/object_reference.h"
23 
24 namespace art {
25 
26 class ArtField;
27 class ArtMethod;
28 // A reference to a ArtField or ArtMethod.
29 template <class ReflectiveType>
30 class ReflectiveReference {
31  public:
32   static_assert(std::is_same_v<ReflectiveType, ArtMethod> ||
33                     std::is_same_v<ReflectiveType, ArtField>,
34                 "Uknown type!");
ReflectiveReference()35   ReflectiveReference() : val_(nullptr) {}
ReflectiveReference(ReflectiveType * r)36   explicit ReflectiveReference(ReflectiveType* r) : val_(r) {}
37   ReflectiveReference<ReflectiveType>& operator=(const ReflectiveReference<ReflectiveType>& t) =
38       default;
39 
Ptr()40   ReflectiveType* Ptr() {
41     return val_;
42   }
43 
Assign(ReflectiveType * r)44   void Assign(ReflectiveType* r) {
45     val_ = r;
46   }
47 
IsNull()48   bool IsNull() const {
49     return val_ == nullptr;
50   }
51 
52   bool operator==(const ReflectiveReference<ReflectiveType>& rr) const {
53     return val_ == rr.val_;
54   }
55   bool operator!=(const ReflectiveReference<ReflectiveType>& rr) const {
56     return !operator==(rr);
57   }
58   bool operator==(std::nullptr_t) const {
59     return IsNull();
60   }
61   bool operator!=(std::nullptr_t) const {
62     return !IsNull();
63   }
64 
65  private:
66   ReflectiveType* val_;
67 };
68 
69 }  // namespace art
70 
71 #endif  // ART_RUNTIME_REFLECTIVE_REFERENCE_H_
72