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 <string> 22 #include <utility> 23 24 #include <hidl-util/FQName.h> 25 26 namespace android { 27 28 // A wrapper around FQName to include instance name as well. 29 // FqInstance::setTo also recognizes all FQName formats, including enum names 30 // etc. 31 // Typical usage: 32 // FqInstance fqInstance; 33 // if (!fqInstance.setTo("...")) { 34 // // error handling 35 // } 36 // LOG(WARNING) << fqInstance.string(); 37 class FqInstance { 38 public: 39 const std::string& getPackage() const; 40 size_t getMajorVersion() const; 41 size_t getMinorVersion() const; 42 std::pair<size_t, size_t> getVersion() const; 43 const std::string& getInterface() const; 44 const std::string& getInstance() const; 45 46 bool hasPackage() const; 47 bool hasVersion() const; 48 bool hasInterface() const; 49 bool hasInstance() const; 50 51 // Return true if valid: 52 // android.hardware.foo (at) 1.0::IFoo/instance 53 // @1.0::IFoo/instance 54 // IFoo/instance 55 // android.hardware.foo (at) 1.0::IFoo.Type 56 // @1.0::IFoo.Type 57 // android.hardware.foo (at) 1.0 58 // IFoo.Type 59 // Type 60 // android.hardware.foo (at) 1.0::IFoo.Type:MY_ENUM_VALUE 61 // @1.0::IFoo.Type:MY_ENUM_VALUE 62 // IFoo.Type:MY_ENUM_VALUE 63 // 64 // If no "/instance", hasInstance() will return false afterwards. 65 // TODO(b/73774955): deprecate this and use std::optional. 66 __attribute__((warn_unused_result)) bool setTo(const std::string& s); 67 68 // Convenience method for the following formats: 69 // android.hardware.foo (at) 1.0::IFoo/default 70 // @1.0::IFoo/default 71 // IFoo/default 72 __attribute__((warn_unused_result)) bool setTo(const std::string& package, size_t majorVer, 73 size_t minorVer, const std::string& interface, 74 const std::string& instance); 75 __attribute__((warn_unused_result)) bool setTo(size_t majorVer, size_t minorVer, 76 const std::string& interface, 77 const std::string& instance); 78 __attribute__((warn_unused_result)) bool setTo(const std::string& interface, 79 const std::string& instance); 80 81 // undefined behavior if: 82 // - Default constructor is called without setTo(); 83 // - setTo is called but returned false. 84 // Should only be called after setTo() returns true. 85 std::string string() const; 86 bool operator<(const FqInstance& other) const; 87 bool operator==(const FqInstance& other) const; 88 bool operator!=(const FqInstance& other) const; 89 90 private: 91 FQName mFqName; 92 std::string mInstance; 93 }; 94 95 } // namespace android 96 97 #endif // ANDROID_FQINSTANCE_H_ 98