1 /* 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 17 #ifndef FQNAME_H_ 18 19 #define FQNAME_H_ 20 21 #include <string> 22 #include <vector> 23 24 namespace android { 25 26 struct FQName { 27 __attribute__((warn_unused_result)) static bool parse(const std::string& s, FQName* into); 28 29 explicit FQName(); 30 31 // TODO(b/73774955): delete 32 explicit FQName(const std::string &s); 33 34 FQName(const std::string& package, const std::string& version, const std::string& name = "", 35 const std::string& valueName = ""); 36 37 FQName(const FQName& other); 38 39 bool isValid() const; 40 bool isIdentifier() const; 41 42 // Returns false if string isn't a valid FQName object. 43 __attribute__((warn_unused_result)) bool setTo(const std::string& s); 44 45 void applyDefaults( 46 const std::string &defaultPackage, 47 const std::string &defaultVersion); 48 49 const std::string& package() const; 50 // 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. 53 std::string version() const; 54 // Return version in the form "V1_0" if it is present, otherwise empty string. 55 std::string sanitizedVersion() const; 56 // Return true only if version is present. 57 bool hasVersion() const; 58 59 FQName withVersion(size_t major, size_t minor) const; 60 61 // The next two methods return the name part of the FQName, that is, the 62 // part after the version field. For example: 63 // 64 // package android.hardware.tests.foo@1.0; 65 // interface IFoo { 66 // struct bar { 67 // struct baz { 68 // ... 69 // }; 70 // }; 71 // }; 72 // 73 // package android.hardware.tests.bar@1.0; 74 // import android.hardware.tests.foo@1.0; 75 // interface { 76 // struct boo { 77 // IFoo.bar.baz base; 78 // }; 79 // } 80 // 81 // The FQName for base is android.hardware.tests.foo@1.0::IFoo.bar.baz; so 82 // FQName::name() will return "IFoo.bar.baz". FQName::names() will return 83 // std::vector<std::string>{"IFoo","bar","baz"} 84 85 const std::string& name() const; 86 std::vector<std::string> names() const; 87 88 // The next two methods returns two parts of the FQName, that is, 89 // the first part package + version + name, the second part valueName. 90 FQName typeName() const; 91 const std::string& valueName() const; 92 93 // has package version and name 94 bool isFullyQualified() const; 95 96 // true if: 97 // 1. (package)?(version)?(name):(valueName) 98 // 2. (valueName), aka a single identifier 99 bool isValidValueName() const; 100 101 // Interface names start with 'I' 102 bool isInterfaceName() const; 103 104 std::string string() const; 105 106 bool operator<(const FQName &other) const; 107 bool operator==(const FQName &other) const; 108 bool operator!=(const FQName &other) const; 109 110 // Must be called on an interface 111 // android.hardware.foo@1.0::IBar 112 // -> Bar 113 std::string getInterfaceBaseName() const; 114 115 // Must be called on an interface 116 // android.hardware.foo@1.0::IBar 117 // -> ABar 118 std::string getInterfaceAdapterName() const; 119 120 // Must be called on an interface 121 // android.hardware.foo@1.0::IBar 122 // -> IBar 123 const std::string& getInterfaceName() const; 124 125 // Must be called on an interface 126 // android.hardware.foo@1.0::IBar 127 // -> IHwBar 128 std::string getInterfaceHwName() const; 129 130 // Must be called on an interface 131 // android.hardware.foo@1.0::IBar 132 // -> BpHwBar 133 std::string getInterfaceProxyName() const; 134 135 // Must be called on an interface 136 // android.hardware.foo@1.0::IBar 137 // -> BnHwBar 138 std::string getInterfaceStubName() const; 139 140 // Must be called on an interface 141 // android.hardware.foo@1.0::IBar 142 // -> BsBar 143 std::string getInterfacePassthroughName() const; 144 145 // Must be called on an interface 146 // android.hardware.foo@1.0::IBar 147 // -> android.hardware.foo@1.0::BpBar 148 FQName getInterfaceProxyFqName() const; 149 150 // Must be called on an interface 151 // android.hardware.foo@1.0::IBar 152 // -> android.hardware.foo@1.0::ABar 153 FQName getInterfaceAdapterFqName() const; 154 155 // Must be called on an interface 156 // android.hardware.foo@1.0::IBar 157 // -> android.hardware.foo@1.0::BnBar 158 FQName getInterfaceStubFqName() const; 159 160 // Must be called on an interface 161 // android.hardware.foo@1.0::IBar 162 // -> android.hardware.foo@1.0::BsBar 163 FQName getInterfacePassthroughFqName() const; 164 165 // Replace whatever after :: with "types" 166 // android.hardware.foo@1.0::Abc.Type:VALUE 167 // -> android.hardware.foo@1.0::types 168 FQName getTypesForPackage() const; 169 170 // android.hardware.foo@1.0::Abc.Type:VALUE 171 // -> android.hardware.foo@1.0 172 FQName getPackageAndVersion() const; 173 174 // the following comments all assume that the FQName 175 // is android.hardware.foo@1.0::IBar.Baz.Bam 176 177 // returns highest type in the hidl namespace, i.e. 178 // android.hardware.foo@1.0::IBar 179 FQName getTopLevelType() const; 180 181 // returns an unambiguous fully qualified name which can be 182 // baked into a token, i.e. 183 // android_hardware_Foo_V1_0_IBar_Baz 184 std::string tokenName() const; 185 186 // Returns an absolute C++ namespace prefix, i.e. 187 // ::android::hardware::Foo::V1_0. 188 std::string cppNamespace() const; 189 190 // Returns a name qualified assuming we are in cppNamespace, i.e. 191 // IBar::Baz. 192 std::string cppLocalName() const; 193 194 // Returns a fully qualified absolute C++ type name, i.e. 195 // ::android::hardware::Foo::V1_0::IBar::Baz. 196 std::string cppName() const; 197 198 // Returns the java package name, i.e. "android.hardware.Foo.V1_0". 199 std::string javaPackage() const; 200 201 // Returns the fully qualified java type name, 202 // i.e. "android.hardware.Foo.V1_0.IBar.Baz" 203 std::string javaName() const; 204 205 bool endsWith(const FQName &other) const; 206 207 // If this is android.hardware@1.0::IFoo 208 // package = "and" -> false 209 // package = "android" -> true 210 // package = "android.hardware@1.0" -> false 211 bool inPackage(const std::string &package) const; 212 213 void getPackageComponents(std::vector<std::string> *components) const; 214 215 void getPackageAndVersionComponents( 216 std::vector<std::string> *components, 217 bool cpp_compatible) const; 218 219 // return major and minor version if they exist, else abort program. 220 // Existence of version can be checked via hasVersion(). 221 size_t getPackageMajorVersion() const; 222 size_t getPackageMinorVersion() const; 223 224 // minor-- if result doesn't underflow, else abort. 225 FQName downRev() const; 226 227 private: 228 // TODO(b/73774955): remove 229 bool mValid; 230 231 bool mIsIdentifier; 232 std::string mPackage; 233 // mMajor == 0 means empty. 234 size_t mMajor = 0; 235 size_t mMinor = 0; 236 std::string mName; 237 std::string mValueName; 238 239 void clear(); 240 241 __attribute__((warn_unused_result)) bool setVersion(const std::string& v); 242 __attribute__((warn_unused_result)) bool parseVersion(const std::string& majorStr, 243 const std::string& minorStr); 244 void clearVersion(); 245 }; 246 247 extern const FQName gIBaseFqName; 248 extern const FQName gIManagerFqName; 249 250 } // namespace android 251 252 #endif // FQNAME_H_ 253