1 /* 2 * Copyright (C) 2018 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 ANDROID_FQINSTANCE_H_ 18 19 #define ANDROID_FQINSTANCE_H_ 20 21 #include <optional> 22 #include <string> 23 #include <utility> 24 25 #include <hidl-util/FQName.h> 26 27 namespace android { 28 29 // A wrapper around FQName to include instance name as well. 30 // FqInstance::setTo also recognizes all FQName formats, including enum names 31 // etc. 32 // Typical usage: 33 // FqInstance fqInstance; 34 // if (!fqInstance.setTo("...")) { 35 // // error handling 36 // } 37 // LOG(WARNING) << fqInstance.string(); 38 class FqInstance { 39 public: 40 const std::string& getPackage() const; 41 size_t getMajorVersion() const; 42 size_t getMinorVersion() const; 43 std::pair<size_t, size_t> getVersion() const; 44 const std::string& getInterface() const; 45 const std::string& getInstance() const; 46 const FQName& getFqName() const; 47 48 bool hasPackage() const; 49 bool hasVersion() const; 50 bool hasInterface() const; 51 bool hasInstance() const; 52 53 // If this is android.hardware@1.0::IFoo 54 // package = "and" -> false 55 // package = "android" -> true 56 // package = "android.hardware@1.0" -> false 57 bool inPackage(const std::string& package) const; 58 59 // Return true if valid: 60 // android.hardware.foo@1.0::IFoo/instance 61 // @1.0::IFoo/instance 62 // IFoo/instance 63 // android.hardware.foo@1.0::IFoo.Type 64 // @1.0::IFoo.Type 65 // android.hardware.foo@1.0 66 // IFoo.Type 67 // Type 68 // android.hardware.foo@1.0::IFoo.Type:MY_ENUM_VALUE 69 // @1.0::IFoo.Type:MY_ENUM_VALUE 70 // IFoo.Type:MY_ENUM_VALUE 71 // 72 // If no "/instance", hasInstance() will return false afterwards. 73 __attribute__((warn_unused_result)) bool setTo(const std::string& s); 74 75 // Convenience method when an FQName and instance are already available. 76 __attribute__((warn_unused_result)) bool setTo(const FQName& fqName, 77 const std::string& instance); 78 79 // Convenience method for the following formats: 80 // android.hardware.foo@1.0 81 // android.hardware.foo@1.0::IFoo 82 // android.hardware.foo@1.0::IFoo/default 83 __attribute__((warn_unused_result)) bool setTo(const std::string& package, size_t majorVer, 84 size_t minorVer, 85 const std::string& interface = "", 86 const std::string& instance = ""); 87 // Convenience method for the following formats: 88 // @1.0::IFoo 89 // @1.0::IFoo/default 90 __attribute__((warn_unused_result)) bool setTo(size_t majorVer, size_t minorVer, 91 const std::string& interface, 92 const std::string& instance = ""); 93 // Convenience method for the following formats: 94 // IFoo/default 95 __attribute__((warn_unused_result)) bool setTo(const std::string& interface, 96 const std::string& instance); 97 98 // Same as creating an FqInstance then call setTo. See setTo for all valid signatures. 99 // If setTo returns false, this function returns nullopt. 100 template <typename... Args> from(Args &&...args)101 static std::optional<FqInstance> from(Args&&... args) { 102 FqInstance fqInstance; 103 if (fqInstance.setTo(std::forward<Args>(args)...)) return fqInstance; 104 return std::nullopt; 105 } 106 107 // undefined behavior if: 108 // - Default constructor is called without setTo(); 109 // - setTo is called but returned false. 110 // Should only be called after setTo() returns true. 111 std::string string() const; 112 bool operator<(const FqInstance& other) const; 113 bool operator==(const FqInstance& other) const; 114 bool operator!=(const FqInstance& other) const; 115 116 private: 117 FQName mFqName; 118 std::string mInstance; 119 120 // helper to setTo() to determine that the FqInstance is actually valid. 121 bool isValid() const; 122 }; 123 124 } // namespace android 125 126 #endif // ANDROID_FQINSTANCE_H_ 127