blob: 5e5ef08583fe87ed669be4f72ef640d7453c3514 [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;
Iliyan Malchev800273d2016-09-02 15:25:07 -070054
55 // The next two methods return the name part of the FQName, that is, the
56 // part after the version field. For example:
57 //
58 // package android.hardware.tests.foo@1.0;
59 // interface IFoo {
60 // struct bar {
61 // struct baz {
62 // ...
63 // };
64 // };
65 // };
66 //
67 // package android.hardware.tests.bar@1.0;
68 // import android.hardware.tests.foo@1.0;
69 // interface {
70 // struct boo {
71 // IFoo.bar.baz base;
72 // };
73 // }
74 //
75 // The FQName for base is android.hardware.tests.foo@1.0::IFoo.bar.baz; so
76 // FQName::name() will return "IFoo.bar.baz". FQName::names() will return
77 // std::vector<std::string>{"IFoo","bar","baz"}
78
Andreas Huber84f89de2016-07-28 15:39:51 -070079 std::string name() const;
Iliyan Malchev800273d2016-09-02 15:25:07 -070080 std::vector<std::string> names() const;
Andreas Huber84f89de2016-07-28 15:39:51 -070081
Yifan Hongb44a6c82016-09-22 15:50:18 -070082 // The next two methods returns two parts of the FQName, that is,
83 // the first part package + version + name, the second part valueName.
84 FQName typeName() const;
85 std::string valueName() const;
86
Andreas Huber68f24592016-07-29 14:53:48 -070087 bool isFullyQualified() const;
88
Yifan Hongb44a6c82016-09-22 15:50:18 -070089 // true if:
90 // 1. (package)?(version)?(name):(valueName)
91 // 2. (valueName), aka a single identifier
92 bool isValidValueName() const;
93
Andreas Huberda51b8e2016-07-28 16:00:57 -070094 void print() const;
Andreas Huber68f24592016-07-29 14:53:48 -070095 std::string string() const;
96
97 bool operator<(const FQName &other) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070098 bool operator==(const FQName &other) const;
Yifan Hongc8934042016-11-17 17:10:52 -080099 bool operator!=(const FQName &other) const;
Andreas Huber84f89de2016-07-28 15:39:51 -0700100
Steven Moreland197d56c2016-09-09 10:03:58 -0700101 // Must be called on an interface
102 // ::android::hardware::Foo::V1_0::IBar
103 // -> Bar
104 std::string getInterfaceBaseName() const;
105
Yifan Hong1977ea32016-10-05 12:49:08 -0700106 // Replace whatever after :: with "types"
107 // android.hardware.foo@1.0::Abc.Type:VALUE
108 // -> android.hardware.foo@1.0::types
109 FQName getTypesForPackage() const;
110
Steven Moreland9c387612016-09-07 09:54:26 -0700111 // the following comments all assume that the FQName
Yifan Hong1977ea32016-10-05 12:49:08 -0700112 // is android.hardware.foo@1.0::IBar.Baz.Bam
Steven Moreland9c387612016-09-07 09:54:26 -0700113
114 // returns highest type in the hidl namespace, i.e.
Yifan Hong1977ea32016-10-05 12:49:08 -0700115 // android.hardware.foo@1.0::IBar
Steven Moreland9c387612016-09-07 09:54:26 -0700116 const FQName getTopLevelType() const;
117
118 // returns an unambiguous fully qualified name which can be
119 // baked into a token, i.e.
120 // android_hardware_Foo_V1_0_IBar_Baz
121 std::string tokenName() const;
122
Andreas Huber0e00de42016-08-03 09:56:02 -0700123 // Returns an absolute C++ namespace prefix, i.e.
Andreas Huber2831d512016-08-15 09:33:47 -0700124 // ::android::hardware::Foo::V1_0.
Andreas Huber0e00de42016-08-03 09:56:02 -0700125 std::string cppNamespace() const;
126
Steven Moreland979e0992016-09-07 09:18:08 -0700127 // Returns a name qualified assuming we are in cppNamespace, i.e.
128 // IBar::Baz.
129 std::string cppLocalName() const;
130
Andreas Huber0e00de42016-08-03 09:56:02 -0700131 // Returns a fully qualified absolute C++ type name, i.e.
Steven Moreland9c387612016-09-07 09:54:26 -0700132 // ::android::hardware::Foo::V1_0::IBar::Baz.
Andreas Huber0e00de42016-08-03 09:56:02 -0700133 std::string cppName() const;
134
Andreas Huber2831d512016-08-15 09:33:47 -0700135 // Returns the java package name, i.e. "android.hardware.Foo.V1_0".
136 std::string javaPackage() const;
137
138 // Returns the fully qualified java type name,
Steven Moreland9c387612016-09-07 09:54:26 -0700139 // i.e. "android.hardware.Foo.V1_0.IBar.Baz"
Andreas Huber2831d512016-08-15 09:33:47 -0700140 std::string javaName() const;
141
Andreas Huber39fa7182016-08-19 14:27:33 -0700142 bool endsWith(const FQName &other) const;
143
Andreas Huber0e00de42016-08-03 09:56:02 -0700144 void getPackageComponents(std::vector<std::string> *components) const;
145
146 void getPackageAndVersionComponents(
147 std::vector<std::string> *components,
148 bool cpp_compatible) const;
149
Martijn Coenena21f1492016-09-08 15:55:14 +0200150 std::string getPackageMajorVersion() const;
151
152 std::string getPackageMinorVersion() const;
Steven Moreland197d56c2016-09-09 10:03:58 -0700153
Andreas Huber84f89de2016-07-28 15:39:51 -0700154private:
Yifan Hongf24fa852016-09-23 11:03:15 -0700155 bool mValid;
156 bool mIsIdentifier;
Andreas Huber84f89de2016-07-28 15:39:51 -0700157 std::string mPackage;
Yifan Hong90ea87f2016-11-01 14:25:47 -0700158 std::string mMajor;
159 std::string mMinor;
Andreas Huber84f89de2016-07-28 15:39:51 -0700160 std::string mName;
Yifan Hongb44a6c82016-09-22 15:50:18 -0700161 std::string mValueName;
Yifan Hong90ea87f2016-11-01 14:25:47 -0700162
163 void setVersion(const std::string &v);
Andreas Huber84f89de2016-07-28 15:39:51 -0700164};
165
Yifan Hongc8934042016-11-17 17:10:52 -0800166static const FQName gIBaseFqName{"android.hidl.base@1.0::IBase"};
167static const FQName gIBasePackageFqName{gIBaseFqName.package(), gIBaseFqName.version(), ""};
168
Andreas Huber84f89de2016-07-28 15:39:51 -0700169} // namespace android
170
171#endif // FQNAME_H_