blob: c0793f7961fd12ec47caa25a6a1954177edb0b2b [file] [log] [blame]
Yifan Hongd5c53cc2018-03-05 16:38:28 -08001/*
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
21namespace android {
22
23static const char INSTANCE_SEP = '/';
24
25const std::string& FqInstance::getPackage() const {
26 return mFqName.package();
27}
28
29bool FqInstance::hasPackage() const {
30 return !getPackage().empty();
31}
32
33size_t FqInstance::getMajorVersion() const {
34 return hasVersion() ? mFqName.getPackageMajorVersion() : 0;
35}
36
37size_t FqInstance::getMinorVersion() const {
38 return hasVersion() ? mFqName.getPackageMinorVersion() : 0;
39}
40
41std::pair<size_t, size_t> FqInstance::getVersion() const {
Steven Moreland58ad9872018-10-29 14:16:43 -070042 return mFqName.getVersion();
Yifan Hongd5c53cc2018-03-05 16:38:28 -080043}
44
45bool FqInstance::hasVersion() const {
46 return mFqName.hasVersion();
47}
48
49const std::string& FqInstance::getInterface() const {
50 return mFqName.getInterfaceName();
51}
52
53bool FqInstance::hasInterface() const {
54 return mFqName.isInterfaceName();
55}
56
57const std::string& FqInstance::getInstance() const {
58 return mInstance;
59}
60
61bool FqInstance::hasInstance() const {
62 return !mInstance.empty();
63}
64
Yifan Hongd40a5762018-03-08 18:01:46 -080065bool FqInstance::isValid() const {
Yifan Hongd5c53cc2018-03-05 16:38:28 -080066 bool hasPkg = hasPackage();
67 bool hasVer = hasVersion();
68 bool hasIntf = hasInterface();
69 bool hasInst = hasInstance();
70
71 // android.hardware.foo@1.0::IFoo/default
72 if (hasPkg && hasVer && hasIntf && hasInst) {
73 return true;
74 }
75
76 // @1.0::IFoo/default
77 if (!hasPkg && hasVer && hasIntf && hasInst) {
78 return true;
79 }
80
81 // IFoo/default
82 if (!hasPkg && !hasVer && hasIntf && hasInst) {
83 return true;
84 }
85
86 // Other cases are covered by FQName::setTo, but instance name should be empty.
87 return !hasInst;
88}
89
Yifan Hongd40a5762018-03-08 18:01:46 -080090bool FqInstance::setTo(const std::string& s) {
91 auto pos = s.find(INSTANCE_SEP);
92 if (!mFqName.setTo(s.substr(0, pos))) return false;
93 mInstance = pos == std::string::npos ? std::string{} : s.substr(pos + 1);
94
95 return isValid();
96}
97
Yifan Hongd5c53cc2018-03-05 16:38:28 -080098bool FqInstance::setTo(const std::string& package, size_t majorVer, size_t minorVer,
99 const std::string& interface, const std::string& instance) {
Yifan Hongd40a5762018-03-08 18:01:46 -0800100 if (!mFqName.setTo(package, majorVer, minorVer, interface)) return false;
101 mInstance = instance;
102 return isValid();
Yifan Hongd5c53cc2018-03-05 16:38:28 -0800103}
104
105bool FqInstance::setTo(size_t majorVer, size_t minorVer, const std::string& interface,
106 const std::string& instance) {
107 return setTo("", majorVer, minorVer, interface, instance);
108}
109
110bool FqInstance::setTo(const std::string& interface, const std::string& instance) {
Yifan Hongd40a5762018-03-08 18:01:46 -0800111 return setTo(0u, 0u, interface, instance);
Yifan Hongd5c53cc2018-03-05 16:38:28 -0800112}
113
114std::string FqInstance::string() const {
115 std::string ret = mFqName.string();
116 if (hasInstance()) ret += INSTANCE_SEP + mInstance;
117 return ret;
118}
119
120bool FqInstance::operator<(const FqInstance& other) const {
121 return string() < other.string();
122}
123
124bool FqInstance::operator==(const FqInstance& other) const {
125 return string() == other.string();
126}
127
128bool FqInstance::operator!=(const FqInstance& other) const {
129 return !(*this == other);
130}
131
Yifan Hongea12c052018-05-01 15:29:31 -0700132bool FqInstance::inPackage(const std::string& package) const {
133 return mFqName.inPackage(package);
134}
135
Yifan Hongd5c53cc2018-03-05 16:38:28 -0800136} // namespace android