xref: /aosp_15_r20/system/libvintf/include/vintf/FqInstance.h (revision 70a7ec852fcefd15a4fb57f8f183a8b1c3aacb08)
1*70a7ec85SAndroid Build Coastguard Worker /*
2*70a7ec85SAndroid Build Coastguard Worker  * Copyright (C) 2023 The Android Open Source Project
3*70a7ec85SAndroid Build Coastguard Worker  *
4*70a7ec85SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*70a7ec85SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*70a7ec85SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*70a7ec85SAndroid Build Coastguard Worker  *
8*70a7ec85SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*70a7ec85SAndroid Build Coastguard Worker  *
10*70a7ec85SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*70a7ec85SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*70a7ec85SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*70a7ec85SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*70a7ec85SAndroid Build Coastguard Worker  * limitations under the License.
15*70a7ec85SAndroid Build Coastguard Worker  */
16*70a7ec85SAndroid Build Coastguard Worker 
17*70a7ec85SAndroid Build Coastguard Worker #pragma once
18*70a7ec85SAndroid Build Coastguard Worker 
19*70a7ec85SAndroid Build Coastguard Worker #include <optional>
20*70a7ec85SAndroid Build Coastguard Worker #include <string>
21*70a7ec85SAndroid Build Coastguard Worker #include <utility>
22*70a7ec85SAndroid Build Coastguard Worker 
23*70a7ec85SAndroid Build Coastguard Worker #include <vintf/FQName.h>
24*70a7ec85SAndroid Build Coastguard Worker 
25*70a7ec85SAndroid Build Coastguard Worker namespace android::vintf {
26*70a7ec85SAndroid Build Coastguard Worker 
27*70a7ec85SAndroid Build Coastguard Worker // A wrapper around FQName to include instance name as well.
28*70a7ec85SAndroid Build Coastguard Worker // FqInstance::setTo also recognizes all FQName formats
29*70a7ec85SAndroid Build Coastguard Worker // Typical usage:
30*70a7ec85SAndroid Build Coastguard Worker // FqInstance fqInstance;
31*70a7ec85SAndroid Build Coastguard Worker // if (!fqInstance.setTo("...")) {
32*70a7ec85SAndroid Build Coastguard Worker //    // error handling
33*70a7ec85SAndroid Build Coastguard Worker // }
34*70a7ec85SAndroid Build Coastguard Worker // LOG(WARNING) << fqInstance.string();
35*70a7ec85SAndroid Build Coastguard Worker class FqInstance {
36*70a7ec85SAndroid Build Coastguard Worker    public:
37*70a7ec85SAndroid Build Coastguard Worker     const std::string& getPackage() const;
38*70a7ec85SAndroid Build Coastguard Worker     size_t getMajorVersion() const;
39*70a7ec85SAndroid Build Coastguard Worker     size_t getMinorVersion() const;
40*70a7ec85SAndroid Build Coastguard Worker     std::pair<size_t, size_t> getVersion() const;
41*70a7ec85SAndroid Build Coastguard Worker     std::string getInterface() const;
42*70a7ec85SAndroid Build Coastguard Worker     const std::string& getInstance() const;
43*70a7ec85SAndroid Build Coastguard Worker     std::string getFqNameString() const;
44*70a7ec85SAndroid Build Coastguard Worker 
45*70a7ec85SAndroid Build Coastguard Worker     bool hasPackage() const;
46*70a7ec85SAndroid Build Coastguard Worker     bool hasVersion() const;
47*70a7ec85SAndroid Build Coastguard Worker     bool hasInterface() const;
48*70a7ec85SAndroid Build Coastguard Worker     bool hasInstance() const;
49*70a7ec85SAndroid Build Coastguard Worker 
50*70a7ec85SAndroid Build Coastguard Worker     // If this is [email protected]::IFoo
51*70a7ec85SAndroid Build Coastguard Worker     // package = "and" -> false
52*70a7ec85SAndroid Build Coastguard Worker     // package = "android" -> true
53*70a7ec85SAndroid Build Coastguard Worker     // package = "[email protected]" -> false
54*70a7ec85SAndroid Build Coastguard Worker     bool inPackage(const std::string& package) const;
55*70a7ec85SAndroid Build Coastguard Worker 
56*70a7ec85SAndroid Build Coastguard Worker     // Return true if valid:
57*70a7ec85SAndroid Build Coastguard Worker     // [email protected]/instance
58*70a7ec85SAndroid Build Coastguard Worker     // [email protected]::IFoo/instance
59*70a7ec85SAndroid Build Coastguard Worker     // @1.0::IFoo/instance
60*70a7ec85SAndroid Build Coastguard Worker     // @1.0/instance
61*70a7ec85SAndroid Build Coastguard Worker     // IFoo/instance
62*70a7ec85SAndroid Build Coastguard Worker     __attribute__((warn_unused_result)) bool setTo(const std::string& s);
63*70a7ec85SAndroid Build Coastguard Worker 
64*70a7ec85SAndroid Build Coastguard Worker     // Convenience method for the following formats:
65*70a7ec85SAndroid Build Coastguard Worker     // [email protected]::IFoo/default
66*70a7ec85SAndroid Build Coastguard Worker     __attribute__((warn_unused_result)) bool setTo(const std::string& package, size_t majorVer,
67*70a7ec85SAndroid Build Coastguard Worker                                                    size_t minorVer, const std::string& interface,
68*70a7ec85SAndroid Build Coastguard Worker                                                    const std::string& instance);
69*70a7ec85SAndroid Build Coastguard Worker     // Convenience method for the following formats:
70*70a7ec85SAndroid Build Coastguard Worker     // @1.0::IFoo/default
71*70a7ec85SAndroid Build Coastguard Worker     __attribute__((warn_unused_result)) bool setTo(size_t majorVer, size_t minorVer,
72*70a7ec85SAndroid Build Coastguard Worker                                                    const std::string& interface,
73*70a7ec85SAndroid Build Coastguard Worker                                                    const std::string& instance);
74*70a7ec85SAndroid Build Coastguard Worker 
75*70a7ec85SAndroid Build Coastguard Worker     // Convenience method for the following formats:
76*70a7ec85SAndroid Build Coastguard Worker     // IFoo/default
77*70a7ec85SAndroid Build Coastguard Worker     __attribute__((warn_unused_result)) bool setTo(const std::string& interface,
78*70a7ec85SAndroid Build Coastguard Worker                                                    const std::string& instance);
79*70a7ec85SAndroid Build Coastguard Worker 
80*70a7ec85SAndroid Build Coastguard Worker     // Same as creating an FqInstance then call setTo. See setTo for all valid signatures.
81*70a7ec85SAndroid Build Coastguard Worker     // If setTo returns false, this function returns nullopt.
82*70a7ec85SAndroid Build Coastguard Worker     template <typename... Args>
from(Args &&...args)83*70a7ec85SAndroid Build Coastguard Worker     static std::optional<FqInstance> from(Args&&... args) {
84*70a7ec85SAndroid Build Coastguard Worker         FqInstance fqInstance;
85*70a7ec85SAndroid Build Coastguard Worker         if (fqInstance.setTo(std::forward<Args>(args)...)) return fqInstance;
86*70a7ec85SAndroid Build Coastguard Worker         return std::nullopt;
87*70a7ec85SAndroid Build Coastguard Worker     }
88*70a7ec85SAndroid Build Coastguard Worker 
89*70a7ec85SAndroid Build Coastguard Worker     // undefined behavior if:
90*70a7ec85SAndroid Build Coastguard Worker     // - Default constructor is called without setTo();
91*70a7ec85SAndroid Build Coastguard Worker     // - setTo is called but returned false.
92*70a7ec85SAndroid Build Coastguard Worker     // Should only be called after setTo() returns true.
93*70a7ec85SAndroid Build Coastguard Worker     std::string string() const;
94*70a7ec85SAndroid Build Coastguard Worker     bool operator<(const FqInstance& other) const;
95*70a7ec85SAndroid Build Coastguard Worker     bool operator==(const FqInstance& other) const;
96*70a7ec85SAndroid Build Coastguard Worker     bool operator!=(const FqInstance& other) const;
97*70a7ec85SAndroid Build Coastguard Worker 
98*70a7ec85SAndroid Build Coastguard Worker    private:
99*70a7ec85SAndroid Build Coastguard Worker     details::FQName mFqName;
100*70a7ec85SAndroid Build Coastguard Worker     std::string mInstance;
101*70a7ec85SAndroid Build Coastguard Worker 
102*70a7ec85SAndroid Build Coastguard Worker     // helper to setTo() to determine that the FqInstance is actually valid.
103*70a7ec85SAndroid Build Coastguard Worker     bool isValid() const;
104*70a7ec85SAndroid Build Coastguard Worker };
105*70a7ec85SAndroid Build Coastguard Worker 
106*70a7ec85SAndroid Build Coastguard Worker }  // namespace android::vintf
107