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