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