• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #pragma once
18 
19 #include <string>
20 #include <vector>
21 
22 namespace android::vintf::details {
23 
24 struct FQName {
25     __attribute__((warn_unused_result)) static bool parse(const std::string& s, FQName* into);
26 
27     explicit FQName();
28 
29     FQName(const std::string& package, const std::string& version, const std::string& name = "");
30 
31     // Returns false if string isn't a valid FQName object.
32     __attribute__((warn_unused_result)) bool setTo(const std::string& s);
33     __attribute__((warn_unused_result)) bool setTo(const std::string& package, size_t majorVer,
34                                                    size_t minorVer, const std::string& name = "");
35 
36     const std::string& package() const;
37     // Return version in the form "1.0" if it is present, otherwise empty string.
38     std::string version() const;
39     // Return true only if version is present.
40     bool hasVersion() const;
41     // Return pair of (major, minor) version. Defaults to 0, 0.
42     std::pair<size_t, size_t> getVersion() const;
43 
44     // The next method return the name part of the FQName, that is, the
45     // part after the version field.  For example:
46     //
47     // package android.hardware.tests.foo@1.0;
48     // interface IFoo {
49     //    struct bar {
50     //        struct baz {
51     //            ...
52     //        };
53     //    };
54     // };
55     //
56     // package android.hardware.tests.bar@1.0;
57     // import android.hardware.tests.foo@1.0;
58     // interface {
59     //    struct boo {
60     //        IFoo.bar.baz base;
61     //    };
62     // }
63     //
64     // The FQName for base is android.hardware.tests.foo@1.0::IFoo.bar.baz; so
65     // FQName::name() will return "IFoo.bar.baz".
66     const std::string& name() const;
67 
68     // Interface names start with 'I'
69     bool isInterfaceName() const;
70 
71     std::string string() const;
72 
73     bool operator<(const FQName& other) const;
74     bool operator==(const FQName& other) const;
75     bool operator!=(const FQName& other) const;
76 
77     // Must be called on an interface
78     // android.hardware.foo@1.0::IBar
79     // -> IBar
80     const std::string& getInterfaceName() const;
81 
82     // android.hardware.foo@1.0::Abc.Type:VALUE
83     // -> android.hardware.foo@1.0
84     FQName getPackageAndVersion() const;
85 
86     // If this is android.hardware@1.0::IFoo
87     // package = "and" -> false
88     // package = "android" -> true
89     // package = "android.hardware@1.0" -> false
90     bool inPackage(const std::string& package) const;
91 
92     // return major and minor version if they exist, else abort program.
93     // Existence of version can be checked via hasVersion().
94     size_t getPackageMajorVersion() const;
95     size_t getPackageMinorVersion() const;
96 
97    private:
98     bool mIsIdentifier;
99     std::string mPackage;
100     // mMajor == 0 means empty.
101     size_t mMajor = 0;
102     size_t mMinor = 0;
103     std::string mName;
104 
105     void clear();
106 
107     __attribute__((warn_unused_result)) bool setVersion(const std::string& v);
108     __attribute__((warn_unused_result)) bool parseVersion(const std::string& majorStr,
109                                                           const std::string& minorStr);
110     __attribute__((warn_unused_result)) static bool parseVersion(const std::string& majorStr,
111                                                                  const std::string& minorStr,
112                                                                  size_t* majorVer,
113                                                                  size_t* minorVer);
114     __attribute__((warn_unused_result)) static bool parseVersion(const std::string& v,
115                                                                  size_t* majorVer,
116                                                                  size_t* minorVer);
117     static void clearVersion(size_t* majorVer, size_t* minorVer);
118 
119     void clearVersion();
120 
121     bool isIdentifier() const;
122     // Return version in the form "@1.0" if it is present, otherwise empty string.
123     std::string atVersion() const;
124 
125     std::vector<std::string> getPackageComponents() const;
126 };
127 
128 }  // namespace android::vintf::details
129