blob: 391ba3dd12f6e22dd220e55c82d9cdd755457f9e [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;
50 std::string version() const;
Iliyan Malchev800273d2016-09-02 15:25:07 -070051
52 // The next two methods return the name part of the FQName, that is, the
53 // part after the version field. For example:
54 //
55 // package android.hardware.tests.foo@1.0;
56 // interface IFoo {
57 // struct bar {
58 // struct baz {
59 // ...
60 // };
61 // };
62 // };
63 //
64 // package android.hardware.tests.bar@1.0;
65 // import android.hardware.tests.foo@1.0;
66 // interface {
67 // struct boo {
68 // IFoo.bar.baz base;
69 // };
70 // }
71 //
72 // The FQName for base is android.hardware.tests.foo@1.0::IFoo.bar.baz; so
73 // FQName::name() will return "IFoo.bar.baz". FQName::names() will return
74 // std::vector<std::string>{"IFoo","bar","baz"}
75
Andreas Huber84f89de2016-07-28 15:39:51 -070076 std::string name() const;
Iliyan Malchev800273d2016-09-02 15:25:07 -070077 std::vector<std::string> names() const;
Andreas Huber84f89de2016-07-28 15:39:51 -070078
Yifan Hongb44a6c82016-09-22 15:50:18 -070079 // The next two methods returns two parts of the FQName, that is,
80 // the first part package + version + name, the second part valueName.
81 FQName typeName() const;
82 std::string valueName() const;
83
Andreas Huber68f24592016-07-29 14:53:48 -070084 bool isFullyQualified() const;
85
Yifan Hongb44a6c82016-09-22 15:50:18 -070086 // true if:
87 // 1. (package)?(version)?(name):(valueName)
88 // 2. (valueName), aka a single identifier
89 bool isValidValueName() const;
90
Andreas Huberda51b8e2016-07-28 16:00:57 -070091 void print() const;
Andreas Huber68f24592016-07-29 14:53:48 -070092 std::string string() const;
93
94 bool operator<(const FQName &other) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070095 bool operator==(const FQName &other) const;
Andreas Huber84f89de2016-07-28 15:39:51 -070096
Steven Moreland197d56c2016-09-09 10:03:58 -070097 // Must be called on an interface
98 // ::android::hardware::Foo::V1_0::IBar
99 // -> Bar
100 std::string getInterfaceBaseName() const;
101
Yifan Hong1977ea32016-10-05 12:49:08 -0700102 // Replace whatever after :: with "types"
103 // android.hardware.foo@1.0::Abc.Type:VALUE
104 // -> android.hardware.foo@1.0::types
105 FQName getTypesForPackage() const;
106
Steven Moreland9c387612016-09-07 09:54:26 -0700107 // the following comments all assume that the FQName
Yifan Hong1977ea32016-10-05 12:49:08 -0700108 // is android.hardware.foo@1.0::IBar.Baz.Bam
Steven Moreland9c387612016-09-07 09:54:26 -0700109
110 // returns highest type in the hidl namespace, i.e.
Yifan Hong1977ea32016-10-05 12:49:08 -0700111 // android.hardware.foo@1.0::IBar
Steven Moreland9c387612016-09-07 09:54:26 -0700112 const FQName getTopLevelType() const;
113
114 // returns an unambiguous fully qualified name which can be
115 // baked into a token, i.e.
116 // android_hardware_Foo_V1_0_IBar_Baz
117 std::string tokenName() const;
118
Andreas Huber0e00de42016-08-03 09:56:02 -0700119 // Returns an absolute C++ namespace prefix, i.e.
Andreas Huber2831d512016-08-15 09:33:47 -0700120 // ::android::hardware::Foo::V1_0.
Andreas Huber0e00de42016-08-03 09:56:02 -0700121 std::string cppNamespace() const;
122
Steven Moreland979e0992016-09-07 09:18:08 -0700123 // Returns a name qualified assuming we are in cppNamespace, i.e.
124 // IBar::Baz.
125 std::string cppLocalName() const;
126
Andreas Huber0e00de42016-08-03 09:56:02 -0700127 // Returns a fully qualified absolute C++ type name, i.e.
Steven Moreland9c387612016-09-07 09:54:26 -0700128 // ::android::hardware::Foo::V1_0::IBar::Baz.
Andreas Huber0e00de42016-08-03 09:56:02 -0700129 std::string cppName() const;
130
Andreas Huber2831d512016-08-15 09:33:47 -0700131 // Returns the java package name, i.e. "android.hardware.Foo.V1_0".
132 std::string javaPackage() const;
133
134 // Returns the fully qualified java type name,
Steven Moreland9c387612016-09-07 09:54:26 -0700135 // i.e. "android.hardware.Foo.V1_0.IBar.Baz"
Andreas Huber2831d512016-08-15 09:33:47 -0700136 std::string javaName() const;
137
Andreas Huber39fa7182016-08-19 14:27:33 -0700138 bool endsWith(const FQName &other) const;
139
Andreas Huber0e00de42016-08-03 09:56:02 -0700140 void getPackageComponents(std::vector<std::string> *components) const;
141
142 void getPackageAndVersionComponents(
143 std::vector<std::string> *components,
144 bool cpp_compatible) const;
145
Martijn Coenena21f1492016-09-08 15:55:14 +0200146 std::string getPackageMajorVersion() const;
147
148 std::string getPackageMinorVersion() const;
Steven Moreland197d56c2016-09-09 10:03:58 -0700149
Zhuoyao Zhang8f492942016-09-28 14:27:56 -0700150 // Returns the version of the package by cutting off the leading '@' prefix.
151 std::string getPackageFullVersion() const;
152
Andreas Huber84f89de2016-07-28 15:39:51 -0700153private:
Yifan Hongf24fa852016-09-23 11:03:15 -0700154 bool mValid;
155 bool mIsIdentifier;
Andreas Huber84f89de2016-07-28 15:39:51 -0700156 std::string mPackage;
157 std::string mVersion;
158 std::string mName;
Yifan Hongb44a6c82016-09-22 15:50:18 -0700159 std::string mValueName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700160};
161
162} // namespace android
163
164#endif // FQNAME_H_