blob: deb533b0adda4800c7822d14d9062e21ed09c91d [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 Hongf24fa852016-09-23 11:03:15 -070036 FQName(const FQName& other);
37
Andreas Huber84f89de2016-07-28 15:39:51 -070038 bool isValid() const;
Yifan Hongb44a6c82016-09-22 15:50:18 -070039 bool isIdentifier() const;
Andreas Huber84f89de2016-07-28 15:39:51 -070040 bool setTo(const std::string &s);
41
42 void applyDefaults(
43 const std::string &defaultPackage,
44 const std::string &defaultVersion);
45
46 std::string package() const;
47 std::string version() const;
Iliyan Malchev800273d2016-09-02 15:25:07 -070048
49 // The next two methods return the name part of the FQName, that is, the
50 // part after the version field. For example:
51 //
52 // package android.hardware.tests.foo@1.0;
53 // interface IFoo {
54 // struct bar {
55 // struct baz {
56 // ...
57 // };
58 // };
59 // };
60 //
61 // package android.hardware.tests.bar@1.0;
62 // import android.hardware.tests.foo@1.0;
63 // interface {
64 // struct boo {
65 // IFoo.bar.baz base;
66 // };
67 // }
68 //
69 // The FQName for base is android.hardware.tests.foo@1.0::IFoo.bar.baz; so
70 // FQName::name() will return "IFoo.bar.baz". FQName::names() will return
71 // std::vector<std::string>{"IFoo","bar","baz"}
72
Andreas Huber84f89de2016-07-28 15:39:51 -070073 std::string name() const;
Iliyan Malchev800273d2016-09-02 15:25:07 -070074 std::vector<std::string> names() const;
Andreas Huber84f89de2016-07-28 15:39:51 -070075
Yifan Hongb44a6c82016-09-22 15:50:18 -070076 // The next two methods returns two parts of the FQName, that is,
77 // the first part package + version + name, the second part valueName.
78 FQName typeName() const;
79 std::string valueName() const;
80
Andreas Huber68f24592016-07-29 14:53:48 -070081 bool isFullyQualified() const;
82
Yifan Hongb44a6c82016-09-22 15:50:18 -070083 // true if:
84 // 1. (package)?(version)?(name):(valueName)
85 // 2. (valueName), aka a single identifier
86 bool isValidValueName() const;
87
Andreas Huberda51b8e2016-07-28 16:00:57 -070088 void print() const;
Andreas Huber68f24592016-07-29 14:53:48 -070089 std::string string() const;
90
91 bool operator<(const FQName &other) const;
Andreas Huberd2943e12016-08-05 11:59:31 -070092 bool operator==(const FQName &other) const;
Andreas Huber84f89de2016-07-28 15:39:51 -070093
Steven Moreland197d56c2016-09-09 10:03:58 -070094 // Must be called on an interface
95 // ::android::hardware::Foo::V1_0::IBar
96 // -> Bar
97 std::string getInterfaceBaseName() const;
98
Steven Moreland9c387612016-09-07 09:54:26 -070099 // the following comments all assume that the FQName
100 // is ::android::hardware::Foo::V1_0::IBar::Baz
101
102 // returns highest type in the hidl namespace, i.e.
103 // ::android::hardware::Foo::V1_0::IBar
104 const FQName getTopLevelType() const;
105
106 // returns an unambiguous fully qualified name which can be
107 // baked into a token, i.e.
108 // android_hardware_Foo_V1_0_IBar_Baz
109 std::string tokenName() const;
110
Andreas Huber0e00de42016-08-03 09:56:02 -0700111 // Returns an absolute C++ namespace prefix, i.e.
Andreas Huber2831d512016-08-15 09:33:47 -0700112 // ::android::hardware::Foo::V1_0.
Andreas Huber0e00de42016-08-03 09:56:02 -0700113 std::string cppNamespace() const;
114
Steven Moreland979e0992016-09-07 09:18:08 -0700115 // Returns a name qualified assuming we are in cppNamespace, i.e.
116 // IBar::Baz.
117 std::string cppLocalName() const;
118
Andreas Huber0e00de42016-08-03 09:56:02 -0700119 // Returns a fully qualified absolute C++ type name, i.e.
Steven Moreland9c387612016-09-07 09:54:26 -0700120 // ::android::hardware::Foo::V1_0::IBar::Baz.
Andreas Huber0e00de42016-08-03 09:56:02 -0700121 std::string cppName() const;
122
Andreas Huber2831d512016-08-15 09:33:47 -0700123 // Returns the java package name, i.e. "android.hardware.Foo.V1_0".
124 std::string javaPackage() const;
125
126 // Returns the fully qualified java type name,
Steven Moreland9c387612016-09-07 09:54:26 -0700127 // i.e. "android.hardware.Foo.V1_0.IBar.Baz"
Andreas Huber2831d512016-08-15 09:33:47 -0700128 std::string javaName() const;
129
Andreas Huber39fa7182016-08-19 14:27:33 -0700130 bool endsWith(const FQName &other) const;
131
Andreas Huber0e00de42016-08-03 09:56:02 -0700132 void getPackageComponents(std::vector<std::string> *components) const;
133
134 void getPackageAndVersionComponents(
135 std::vector<std::string> *components,
136 bool cpp_compatible) const;
137
Martijn Coenena21f1492016-09-08 15:55:14 +0200138 std::string getPackageMajorVersion() const;
139
140 std::string getPackageMinorVersion() const;
Steven Moreland197d56c2016-09-09 10:03:58 -0700141
Andreas Huber84f89de2016-07-28 15:39:51 -0700142private:
Yifan Hongf24fa852016-09-23 11:03:15 -0700143 bool mValid;
144 bool mIsIdentifier;
Andreas Huber84f89de2016-07-28 15:39:51 -0700145 std::string mPackage;
146 std::string mVersion;
147 std::string mName;
Yifan Hongb44a6c82016-09-22 15:50:18 -0700148 std::string mValueName;
Andreas Huber84f89de2016-07-28 15:39:51 -0700149};
150
151} // namespace android
152
153#endif // FQNAME_H_