blob: 4b03c158f2ebdc670fe6e34ab9d5b4893cd98ec5 [file] [log] [blame]
Timur Iskhakov505316c2017-08-05 03:38:59 +00001/*
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;
Timur Iskhakov774ae7f2017-08-13 22:57:55 -070034 virtual ~Reference() {}
Timur Iskhakov505316c2017-08-05 03:38:59 +000035
36 Reference(const FQName& fqName, const Location& location)
37 : mResolved(nullptr), mFqName(fqName), mLocation(location) {}
38
39 Reference(T* type, const Location& location) : mResolved(type), mLocation(location) {
40 CHECK(type != nullptr);
41 }
42
Timur Iskhakov33431e62017-08-21 17:31:23 -070043 template <class OtherT>
44 Reference(const Reference<OtherT>& ref)
Timur Iskhakov505316c2017-08-05 03:38:59 +000045 : mResolved(ref.mResolved), mFqName(ref.mFqName), mLocation(ref.mLocation) {}
46
Timur Iskhakova48a4d92017-09-09 20:34:53 -070047 template <class OtherT>
48 Reference(const Reference<OtherT>& ref, const Location& location)
49 : mResolved(ref.mResolved), mFqName(ref.mFqName), mLocation(location) {}
50
Timur Iskhakov505316c2017-08-05 03:38:59 +000051 /* Returns true iff referred type is resolved
52 Referred type's field might be not resolved */
53 bool isResolved() const { return mResolved != nullptr; }
54
Timur Iskhakov24e605b2017-08-30 14:02:55 -070055 T* operator->() { return get(); }
56 const T* operator->() const { return get(); }
Timur Iskhakov505316c2017-08-05 03:38:59 +000057
Timur Iskhakovdbaed332017-08-31 16:33:41 -070058 /* Returns referenced object.
Andreas Huber4ba5c972017-11-29 11:06:25 -080059 If a type is referenced, all typedefs are unwrapped. */
Timur Iskhakov24e605b2017-08-30 14:02:55 -070060 T* get() {
61 CHECK(mResolved != nullptr);
Timur Iskhakovdbaed332017-08-31 16:33:41 -070062 return mResolved->resolve();
63 }
64 const T* get() const {
65 CHECK(mResolved != nullptr);
66 return mResolved->resolve();
67 }
68
69 /* Returns exact referenced object.
70 If a type is referenced, typedefs are not unwraped. */
71 T* shallowGet() {
72 CHECK(mResolved != nullptr);
Timur Iskhakov24e605b2017-08-30 14:02:55 -070073 return mResolved;
74 }
Timur Iskhakovdbaed332017-08-31 16:33:41 -070075 const T* shallowGet() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +000076 CHECK(mResolved != nullptr);
77 return mResolved;
78 }
79
Timur Iskhakov505316c2017-08-05 03:38:59 +000080 void set(T* resolved) {
81 CHECK(!isResolved());
82 CHECK(resolved != nullptr);
83 mResolved = resolved;
84 }
85
86 /* Returns true iff this is reference to null:
87 not resolved and has not name for lookup */
88 bool isEmptyReference() const { return !isResolved() && !hasLookupFqName(); }
89
90 const FQName& getLookupFqName() const {
91 CHECK(hasLookupFqName());
92 return mFqName;
93 }
94
95 bool hasLocation() const { return mLocation.isValid(); }
96
Timur Iskhakovcec46c42017-08-09 00:22:02 -070097 const Location& location() const {
Timur Iskhakov505316c2017-08-05 03:38:59 +000098 CHECK(hasLocation());
99 return mLocation;
100 }
101
102 private:
103 /* Referred type */
104 T* mResolved = nullptr;
105 /* Reference name for lookup */
106 FQName mFqName;
Timur Iskhakova48a4d92017-09-09 20:34:53 -0700107 /* Reference location is mainly used for printing errors
108 and handling forward reference restrictions */
Timur Iskhakov505316c2017-08-05 03:38:59 +0000109 Location mLocation;
110
111 bool hasLookupFqName() const {
112 // Valid only while not resolved to prevent confusion when
113 // ref.hasLookupFqName() is false while ref,get()->fqName is valid.
114 CHECK(!isResolved());
115 return mFqName.isValid();
116 }
117
118 template <class OtherT>
119 friend struct Reference;
120};
121
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700122template <class T>
Timur Iskhakov774ae7f2017-08-13 22:57:55 -0700123struct NamedReference : public Reference<T> {
Timur Iskhakova48a4d92017-09-09 20:34:53 -0700124 NamedReference(const std::string& name, const Reference<T>& reference, const Location& location)
125 : Reference<T>(reference, location), mName(name) {}
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700126
127 const std::string& name() const { return mName; }
Timur Iskhakov774ae7f2017-08-13 22:57:55 -0700128
129 // TODO(b/64715470) Legacy
130 const T& type() const { return *Reference<T>::get(); }
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700131
132 private:
133 const std::string mName;
Timur Iskhakov7fa79f62017-08-09 11:04:54 -0700134};
135
Timur Iskhakov505316c2017-08-05 03:38:59 +0000136} // namespace android
137
138#endif // REFERENCE_H_