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