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     const FQName& getFqName() const;
46 
47     bool hasPackage() const;
48     bool hasVersion() const;
49     bool hasInterface() const;
50     bool hasInstance() const;
51 
52     // If this is android.hardware@1.0::IFoo
53     // package = "and" -> false
54     // package = "android" -> true
55     // package = "android.hardware@1.0" -> false
56     bool inPackage(const std::string& package) const;
57 
58     // Return true if valid:
59     // android.hardware.foo@1.0::IFoo/instance
60     // @1.0::IFoo/instance
61     // IFoo/instance
62     // android.hardware.foo@1.0::IFoo.Type
63     // @1.0::IFoo.Type
64     // android.hardware.foo@1.0
65     // IFoo.Type
66     // Type
67     // android.hardware.foo@1.0::IFoo.Type:MY_ENUM_VALUE
68     // @1.0::IFoo.Type:MY_ENUM_VALUE
69     // IFoo.Type:MY_ENUM_VALUE
70     //
71     // If no "/instance", hasInstance() will return false afterwards.
72     // TODO(b/73774955): deprecate this and use std::optional.
73     __attribute__((warn_unused_result)) bool setTo(const std::string& s);
74 
75     // Convenience method when an FQName and instance are already available.
76     __attribute__((warn_unused_result)) bool setTo(const FQName& fqName,
77                                                    const std::string& instance);
78 
79     // Convenience method for the following formats:
80     // android.hardware.foo@1.0
81     // android.hardware.foo@1.0::IFoo
82     // android.hardware.foo@1.0::IFoo/default
83     __attribute__((warn_unused_result)) bool setTo(const std::string& package, size_t majorVer,
84                                                    size_t minorVer,
85                                                    const std::string& interface = "",
86                                                    const std::string& instance = "");
87     // Convenience method for the following formats:
88     // @1.0::IFoo
89     // @1.0::IFoo/default
90     __attribute__((warn_unused_result)) bool setTo(size_t majorVer, size_t minorVer,
91                                                    const std::string& interface,
92                                                    const std::string& instance = "");
93     // Convenience method for the following formats:
94     // IFoo/default
95     __attribute__((warn_unused_result)) bool setTo(const std::string& interface,
96                                                    const std::string& instance);
97 
98     // undefined behavior if:
99     // - Default constructor is called without setTo();
100     // - setTo is called but returned false.
101     // Should only be called after setTo() returns true.
102     std::string string() const;
103     bool operator<(const FqInstance& other) const;
104     bool operator==(const FqInstance& other) const;
105     bool operator!=(const FqInstance& other) const;
106 
107    private:
108     FQName mFqName;
109     std::string mInstance;
110 
111     // helper to setTo() to determine that the FqInstance is actually valid.
112     bool isValid() const;
113 };
114 
115 }  // namespace android
116 
117 #endif  // ANDROID_FQINSTANCE_H_
118