blob: d4601ab7e631c65006c16fa6ad9bc548d9f7e80e [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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
Andreas Huber84f89de2016-07-28 15:39:51 -070017#ifndef FQNAME_H_
18
19#define FQNAME_H_
20
21#include <android-base/macros.h>
22#include <string>
Andreas Huber0e00de42016-08-03 09:56:02 -070023#include <vector>
Andreas Huber84f89de2016-07-28 15:39:51 -070024
25namespace android {
26
27struct FQName {
Andreas Huberda51b8e2016-07-28 16:00:57 -070028 explicit FQName();
Andreas Huber84f89de2016-07-28 15:39:51 -070029 explicit FQName(const std::string &s);
30
Andreas Huber68f24592016-07-29 14:53:48 -070031 FQName(const std::string &package,
32 const std::string &version,
Yifan Hongb44a6c82016-09-22 15:50:18 -070033 const std::string &name,
34 const std::string &valueName = "");
Andreas Huber68f24592016-07-29 14:53:48 -070035
Yifan Hong327cfe12016-10-03 10:29:42 -070036 // a synonym to FQName(names.join("."))
37 FQName(const std::vector<std::string> &names);
38
Yifan Hongf24fa852016-09-23 11:03:15 -070039 FQName(const FQName& other);
40
Andreas Huber84f89de2016-07-28 15:39:51 -070041 bool isValid() const;
Yifan Hongb44a6c82016-09-22 15:50:18 -070042 bool isIdentifier() const;
Andreas Huber84f89de2016-07-28 15:39:51 -070043 bool setTo(const std::string &s);
44
45 void applyDefaults(
46 const std::string &defaultPackage,
47 const std::string &defaultVersion);
48
49 std::string package() const;
Yifan Hong90ea87f2016-11-01 14:25:47 -070050 // Return version in the form "@1.0" if it is present, otherwise empty string.
51 std::string atVersion() const;
52 // Return version in the form "1.0" if it is present, otherwise empty string.
Andreas Huber84f89de2016-07-28 15:39:51 -070053 std::string version() const;
Yifan Hong97288ac2016-12-12 16:03:51 -080054 // Return version in the form "V1_0" if it is present, otherwise empty string.
55 std::string sanitizedVersion() const;
Iliyan Malchev800273d2016-09-02 15:25:07 -070056
57 // The next two methods return the name part of the FQName, that is, the
58 // part after the version field. For example:
59 //
60 // package android.hardware.tests.foo@1.0;
61 // interface IFoo {
62 // struct bar {
63 // struct baz {
64 // ...
65 // };
66 // };
67 // };
68 //
69 // package android.hardware.tests.bar@1.0;
70 // import android.hardware.tests.foo@1.0;
71 // interface {
72 // struct boo {
73 // IFoo.bar.baz base;
74 // };
75 // }
76 //
77 // The FQName for base is android.hardware.tests.foo@1.0::IFoo.bar.baz; so
78 // FQName::name() will return "IFoo.bar.baz". FQName::names() will return
79 // std::vector<std::string>{"IFoo","bar","baz"}
80
Andreas Huber84f89de2016-07-28 15:39:51 -070081 std::string name() const;
Iliyan Malchev800273d2016-09-02 15:25:07 -070082 std::vector<std::string> names() const;
Andreas Huber84f89de2016-07-28 15:39:51 -070083
Yifan Hongb44a6c82016-09-22 15:50:18 -070084 // The next two methods returns two parts of the FQName, that is,
85 // the first part package + version + name, the second part valueName.
86 FQName typeName() const;
87 std::string valueName() const;
88
Andreas Huber68f24592016-07-29 14:53:48 -070089 bool isFullyQualified() const;
90
Yifan Hongb44a6c82016-09-22 15:50:18 -070091 // true if:
92 // 1. (package)?(version)?(name):(valueName)
93 // 2. (valueName), aka a single identifier
94 bool isValidValueName() const;
95
Andreas Huberda51b8e2016-07-28 16:00:57 -070096 void print() const;
Andreas Huber68f24592016-07-29 14:53:48 -070097 std::string string() const;
98
99 bool operator<(const FQName &other) const;
Andreas Huberd2943e12016-08-05 11:59:31 -0700100 bool operator==(const FQName &other) const;
Yifan Hongc8934042016-11-17 17:10:52 -0800101 bool operator!=(const FQName &other) const;
Andreas Huber84f89de2016-07-28 15:39:51 -0700102
Steven Moreland197d56c2016-09-09 10:03:58 -0700103 // Must be called on an interface
104 // ::android::hardware::Foo::V1_0::IBar
105 // -> Bar
106 std::string getInterfaceBaseName() const;
107
Yifan Hong1977ea32016-10-05 12:49:08 -0700108 // Replace whatever after :: with "types"
109 // android.hardware.foo@1.0::Abc.Type:VALUE
110 // -> android.hardware.foo@1.0::types
111 FQName getTypesForPackage() const;
112
Steven Moreland9c387612016-09-07 09:54:26 -0700113 // the following comments all assume that the FQName
Yifan Hong1977ea32016-10-05 12:49:08 -0700114 // is android.hardware.foo@1.0::IBar.Baz.Bam
Steven Moreland9c387612016-09-07 09:54:26 -0700115
116 // returns highest type in the hidl namespace, i.e.
Yifan Hong1977ea32016-10-05 12:49:08 -0700117 // android.hardware.foo@1.0::IBar
Steven Moreland9c387612016-09-07 09:54:26 -0700118 const FQName getTopLevelType() const;
119
120 // returns an unambiguous fully qualified name which can be
121 // baked into a token, i.e.
122 // android_hardware_Foo_V1_0_IBar_Baz
123 std::string tokenName() const;
124
Andreas Huber0e00de42016-08-03 09:56:02 -0700125 // Returns an absolute C++ namespace prefix, i.e.
Andreas Huber2831d512016-08-15 09:33:47 -0700126 // ::android::hardware::Foo::V1_0.
Andreas Huber0e00de42016-08-03 09:56:02 -0700127 std::string cppNamespace() const;
128
Steven Moreland979e0992016-09-07 09:18:08 -0700129 // Returns a name qualified assuming we are in cppNamespace, i.e.
130 // IBar::Baz.
131 std::string cppLocalName() const;
132
Andreas Huber0e00de42016-08-03 09:56:02 -0700133 // Returns a fully qualified absolute C++ type name, i.e.
Steven Moreland9c387612016-09-07 09:54:26 -0700134 // ::android::hardware::Foo::V1_0::IBar::Baz.
Andreas Huber0e00de42016-08-03 09:56:02 -0700135 std::string cppName() const;
136
Andreas Huber2831d512016-08-15 09:33:47 -0700137 // Returns the java package name, i.e. "android.hardware.Foo.V1_0".
138 std::string javaPackage() const;
139
140 // Returns the fully qualified java type name,
Steven Moreland9c387612016-09-07 09:54:26 -0700141 // i.e. "android.hardware.Foo.V1_0.IBar.Baz"
Andreas Huber2831d512016-08-15 09:33:47 -0700142 std::string javaName() const;
143
Andreas Huber39fa7182016-08-19 14:27:33 -0700144 bool endsWith(const FQName &other) const;
145
Andreas Huber0e00de42016-08-03 09:56:02 -0700146 void getPackageComponents(std::vector<std::string> *components) const;
147
148 void getPackageAndVersionComponents(
149 std::vector<std::string> *components,
150 bool cpp_compatible) const;
151
Martijn Coenena21f1492016-09-08 15:55:14 +0200152 std::string getPackageMajorVersion() const;
153
154 std::string getPackageMinorVersion() const;
Steven Moreland197d56c2016-09-09 10:03:58 -0700155
Andreas Huber84f89de2016-07-28 15:39:51 -0700156private:
Yifan Hongf24fa852016-09-23 11:03:15 -0700157 bool mValid;
158 bool mIsIdentifier;
Andreas Huber84f89de2016-07-28 15:39:51 -0700159 std::string mPackage;
Yifan Hong90ea87f2016-11-01 14:25:47 -0700160 std::string mMajor;
161 std::string mMinor;
Andreas Huber84f89de2016-07-28 15:39:51 -0700162 std::string mName;
Yifan Hongb44a6c82016-09-22 15:50:18 -0700163 std::string mValueName;
Yifan Hong90ea87f2016-11-01 14:25:47 -0700164
165 void setVersion(const std::string &v);
Andreas Huber84f89de2016-07-28 15:39:51 -0700166};
167
Yifan Hongc8934042016-11-17 17:10:52 -0800168static const FQName gIBaseFqName{"android.hidl.base@1.0::IBase"};
169static const FQName gIBasePackageFqName{gIBaseFqName.package(), gIBaseFqName.version(), ""};
170
Andreas Huber84f89de2016-07-28 15:39:51 -0700171} // namespace android
172
173#endif // FQNAME_H_