blob: e83004afebb5c04a16359730e4665d23537b78fb [file] [log] [blame]
Timur Iskhakov4b80bc42017-07-28 15:59:06 -07001/*
2 * Copyright (C) 2017 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 REFERENCE_H_
18
19#define REFERENCE_H_
20
21#include <android-base/logging.h>
22#include <hidl-util/FQName.h>
23
24#include "Location.h"
25
26namespace android {
27
28/**
29 * Reference placeholder
30 */
31template <class T>
32struct Reference {
33 Reference() = default;
34
35 Reference(const FQName& fqName, const Location& location)
36 : mResolved(nullptr), mFqName(fqName), mLocation(location) {}
37
38 Reference(T* type, const Location& location) : mResolved(type), mLocation(location) {
39 CHECK(type != nullptr);
40 }
41
42 Reference(const Reference& ref)
43 : mResolved(ref.mResolved), mFqName(ref.mFqName), mLocation(ref.mLocation) {}
44
45 /* Storing type cast, valid only before resolving */
46 template <class OtherT>
47 Reference(const Reference<OtherT>& ref)
48 : mResolved(nullptr), mFqName(ref.mFqName), mLocation(ref.mLocation) {
49 CHECK(!ref.isResolved());
50 }
51
52 /* Returns true iff referred type is resolved
53 Referred type's field might be not resolved */
54 bool isResolved() const { return mResolved != nullptr; }
55
56 operator T*() const { return get(); }
57
58 T* operator->() const { return get(); }
59
60 T* get() const {
61 CHECK(mResolved != nullptr);
62 return mResolved;
63 }
64
65 void set(T* resolved) {
66 CHECK(!isResolved());
67 CHECK(resolved != nullptr);
68 mResolved = resolved;
69 }
70
71 /* Returns true iff this is reference to null:
72 not resolved and has not name for lookup */
73 bool isEmptyReference() const { return !isResolved() && !hasLookupFqName(); }
74
75 const FQName& getLookupFqName() const {
76 CHECK(hasLookupFqName());
77 return mFqName;
78 }
79
80 bool hasLocation() const { return mLocation.isValid(); }
81
82 const Location& getLocation() const {
83 CHECK(hasLocation());
84 return mLocation;
85 }
86
87 private:
88 /* Referred type */
89 T* mResolved = nullptr;
90 /* Reference name for lookup */
91 FQName mFqName;
92 /* Reference location is mainly used for printing errors */
93 Location mLocation;
94
95 bool hasLookupFqName() const {
96 // Valid only while not resolved to prevent confusion when
97 // ref.hasLookupFqName() is false while ref,get()->fqName is valid.
98 CHECK(!isResolved());
99 return mFqName.isValid();
100 }
101
102 template <class OtherT>
103 friend struct Reference;
104};
105
106} // namespace android
107
108#endif // REFERENCE_H_