| Yifan Hong | d5c53cc | 2018-03-05 16:38:28 -0800 | [diff] [blame] | 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 | #include "FqInstance.h" |
| 18 | |
| 19 | #include <sstream> |
| 20 | |
| 21 | namespace android { |
| 22 | |
| 23 | static const char INSTANCE_SEP = '/'; |
| 24 | |
| 25 | const std::string& FqInstance::getPackage() const { |
| 26 | return mFqName.package(); |
| 27 | } |
| 28 | |
| 29 | bool FqInstance::hasPackage() const { |
| 30 | return !getPackage().empty(); |
| 31 | } |
| 32 | |
| 33 | size_t FqInstance::getMajorVersion() const { |
| 34 | return hasVersion() ? mFqName.getPackageMajorVersion() : 0; |
| 35 | } |
| 36 | |
| 37 | size_t FqInstance::getMinorVersion() const { |
| 38 | return hasVersion() ? mFqName.getPackageMinorVersion() : 0; |
| 39 | } |
| 40 | |
| 41 | std::pair<size_t, size_t> FqInstance::getVersion() const { |
| Steven Moreland | 58ad987 | 2018-10-29 14:16:43 -0700 | [diff] [blame] | 42 | return mFqName.getVersion(); |
| Yifan Hong | d5c53cc | 2018-03-05 16:38:28 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | bool FqInstance::hasVersion() const { |
| 46 | return mFqName.hasVersion(); |
| 47 | } |
| 48 | |
| 49 | const std::string& FqInstance::getInterface() const { |
| 50 | return mFqName.getInterfaceName(); |
| 51 | } |
| 52 | |
| 53 | bool FqInstance::hasInterface() const { |
| 54 | return mFqName.isInterfaceName(); |
| 55 | } |
| 56 | |
| 57 | const std::string& FqInstance::getInstance() const { |
| 58 | return mInstance; |
| 59 | } |
| 60 | |
| 61 | bool FqInstance::hasInstance() const { |
| 62 | return !mInstance.empty(); |
| 63 | } |
| 64 | |
| Daniel Norman | a35528e | 2019-07-15 15:10:24 -0700 | [diff] [blame] | 65 | const FQName& FqInstance::getFqName() const { |
| 66 | return mFqName; |
| 67 | } |
| 68 | |
| Yifan Hong | d40a576 | 2018-03-08 18:01:46 -0800 | [diff] [blame] | 69 | bool FqInstance::isValid() const { |
| Yifan Hong | d5c53cc | 2018-03-05 16:38:28 -0800 | [diff] [blame] | 70 | bool hasPkg = hasPackage(); |
| 71 | bool hasVer = hasVersion(); |
| 72 | bool hasIntf = hasInterface(); |
| 73 | bool hasInst = hasInstance(); |
| 74 | |
| 75 | // android.hardware.foo@1.0::IFoo/default |
| 76 | if (hasPkg && hasVer && hasIntf && hasInst) { |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | // @1.0::IFoo/default |
| 81 | if (!hasPkg && hasVer && hasIntf && hasInst) { |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | // IFoo/default |
| 86 | if (!hasPkg && !hasVer && hasIntf && hasInst) { |
| 87 | return true; |
| 88 | } |
| 89 | |
| 90 | // Other cases are covered by FQName::setTo, but instance name should be empty. |
| 91 | return !hasInst; |
| 92 | } |
| 93 | |
| Yifan Hong | d40a576 | 2018-03-08 18:01:46 -0800 | [diff] [blame] | 94 | bool FqInstance::setTo(const std::string& s) { |
| 95 | auto pos = s.find(INSTANCE_SEP); |
| 96 | if (!mFqName.setTo(s.substr(0, pos))) return false; |
| 97 | mInstance = pos == std::string::npos ? std::string{} : s.substr(pos + 1); |
| 98 | |
| 99 | return isValid(); |
| 100 | } |
| 101 | |
| Yifan Hong | d5c53cc | 2018-03-05 16:38:28 -0800 | [diff] [blame] | 102 | bool FqInstance::setTo(const std::string& package, size_t majorVer, size_t minorVer, |
| 103 | const std::string& interface, const std::string& instance) { |
| Yifan Hong | d40a576 | 2018-03-08 18:01:46 -0800 | [diff] [blame] | 104 | if (!mFqName.setTo(package, majorVer, minorVer, interface)) return false; |
| 105 | mInstance = instance; |
| 106 | return isValid(); |
| Yifan Hong | d5c53cc | 2018-03-05 16:38:28 -0800 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool FqInstance::setTo(size_t majorVer, size_t minorVer, const std::string& interface, |
| 110 | const std::string& instance) { |
| 111 | return setTo("", majorVer, minorVer, interface, instance); |
| 112 | } |
| 113 | |
| 114 | bool FqInstance::setTo(const std::string& interface, const std::string& instance) { |
| Yifan Hong | d40a576 | 2018-03-08 18:01:46 -0800 | [diff] [blame] | 115 | return setTo(0u, 0u, interface, instance); |
| Yifan Hong | d5c53cc | 2018-03-05 16:38:28 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | std::string FqInstance::string() const { |
| 119 | std::string ret = mFqName.string(); |
| 120 | if (hasInstance()) ret += INSTANCE_SEP + mInstance; |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | bool FqInstance::operator<(const FqInstance& other) const { |
| 125 | return string() < other.string(); |
| 126 | } |
| 127 | |
| 128 | bool FqInstance::operator==(const FqInstance& other) const { |
| 129 | return string() == other.string(); |
| 130 | } |
| 131 | |
| 132 | bool FqInstance::operator!=(const FqInstance& other) const { |
| 133 | return !(*this == other); |
| 134 | } |
| 135 | |
| Yifan Hong | ea12c05 | 2018-05-01 15:29:31 -0700 | [diff] [blame] | 136 | bool FqInstance::inPackage(const std::string& package) const { |
| 137 | return mFqName.inPackage(package); |
| 138 | } |
| 139 | |
| Yifan Hong | d5c53cc | 2018-03-05 16:38:28 -0800 | [diff] [blame] | 140 | } // namespace android |