Home | History | Annotate | Download | only in hidl-util
      1 /*
      2  * Copyright (C) 2016 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 FQNAME_H_
     18 
     19 #define FQNAME_H_
     20 
     21 #include <android-base/macros.h>
     22 #include <string>
     23 #include <vector>
     24 
     25 namespace android {
     26 
     27 struct FQName {
     28     explicit FQName();
     29     explicit FQName(const std::string &s);
     30 
     31     FQName(const std::string &package,
     32            const std::string &version,
     33            const std::string &name,
     34            const std::string &valueName = "");
     35 
     36     // a synonym to FQName(names.join("."))
     37     FQName(const std::vector<std::string> &names);
     38 
     39     FQName(const FQName& other);
     40 
     41     bool isValid() const;
     42     bool isIdentifier() const;
     43     bool setTo(const std::string &s);
     44 
     45     void applyDefaults(
     46             const std::string &defaultPackage,
     47             const std::string &defaultVersion);
     48 
     49     std::string package() const;
     50     // Return version in the form "@1.0" if it is present, otherwise empty string.
     51     std::string atVersion() const;
     52     // Return version in the form "1.0" if it is present, otherwise empty string.
     53     std::string version() const;
     54     // Return version in the form "V1_0" if it is present, otherwise empty string.
     55     std::string sanitizedVersion() const;
     56     // Return true only if version is present.
     57     bool hasVersion() const;
     58 
     59     // The next two methods return the name part of the FQName, that is, the
     60     // part after the version field.  For example:
     61     //
     62     // package android.hardware.tests.foo (at) 1.0;
     63     // interface IFoo {
     64     //    struct bar {
     65     //        struct baz {
     66     //            ...
     67     //        };
     68     //    };
     69     // };
     70     //
     71     // package android.hardware.tests.bar (at) 1.0;
     72     // import android.hardware.tests.foo (at) 1.0;
     73     // interface {
     74     //    struct boo {
     75     //        IFoo.bar.baz base;
     76     //    };
     77     // }
     78     //
     79     // The FQName for base is android.hardware.tests.foo (at) 1.0::IFoo.bar.baz; so
     80     // FQName::name() will return "IFoo.bar.baz". FQName::names() will return
     81     // std::vector<std::string>{"IFoo","bar","baz"}
     82 
     83     std::string name() const;
     84     std::vector<std::string> names() const;
     85 
     86     // The next two methods returns two parts of the FQName, that is,
     87     // the first part package + version + name, the second part valueName.
     88     FQName typeName() const;
     89     std::string valueName() const;
     90 
     91     // has package version and name
     92     bool isFullyQualified() const;
     93 
     94     // true if:
     95     // 1. (package)?(version)?(name):(valueName)
     96     // 2. (valueName), aka a single identifier
     97     bool isValidValueName() const;
     98 
     99     void print() const;
    100     std::string string() const;
    101 
    102     bool operator<(const FQName &other) const;
    103     bool operator==(const FQName &other) const;
    104     bool operator!=(const FQName &other) const;
    105 
    106     // Must be called on an interface
    107     // android.hardware.foo (at) 1.0::IBar
    108     // -> Bar
    109     std::string getInterfaceBaseName() const;
    110 
    111     // Must be called on an interface
    112     // android.hardware.foo (at) 1.0::IBar
    113     // -> IBar
    114     std::string getInterfaceName() const;
    115 
    116     // Must be called on an interface
    117     // android.hardware.foo (at) 1.0::IBar
    118     // -> IHwBar
    119     std::string getInterfaceHwName() const;
    120 
    121     // Must be called on an interface
    122     // android.hardware.foo (at) 1.0::IBar
    123     // -> BpBar
    124     std::string getInterfaceProxyName() const;
    125 
    126     // Must be called on an interface
    127     // android.hardware.foo (at) 1.0::IBar
    128     // -> BnBar
    129     std::string getInterfaceStubName() const;
    130 
    131     // Must be called on an interface
    132     // android.hardware.foo (at) 1.0::IBar
    133     // -> BsBar
    134     std::string getInterfacePassthroughName() const;
    135 
    136     // Must be called on an interface
    137     // android.hardware.foo (at) 1.0::IBar
    138     // -> android.hardware.foo (at) 1.0::BpBar
    139     FQName getInterfaceProxyFqName() const;
    140 
    141     // Must be called on an interface
    142     // android.hardware.foo (at) 1.0::IBar
    143     // -> android.hardware.foo (at) 1.0::BnBar
    144     FQName getInterfaceStubFqName() const;
    145 
    146     // Must be called on an interface
    147     // android.hardware.foo (at) 1.0::IBar
    148     // -> android.hardware.foo (at) 1.0::BsBar
    149     FQName getInterfacePassthroughFqName() const;
    150 
    151     // Replace whatever after :: with "types"
    152     // android.hardware.foo (at) 1.0::Abc.Type:VALUE
    153     // -> android.hardware.foo (at) 1.0::types
    154     FQName getTypesForPackage() const;
    155 
    156     // android.hardware.foo (at) 1.0::Abc.Type:VALUE
    157     // -> android.hardware.foo (at) 1.0
    158     FQName getPackageAndVersion() const;
    159 
    160     // the following comments all assume that the FQName
    161     // is android.hardware.foo (at) 1.0::IBar.Baz.Bam
    162 
    163     // returns highest type in the hidl namespace, i.e.
    164     // android.hardware.foo (at) 1.0::IBar
    165     FQName getTopLevelType() const;
    166 
    167     // returns an unambiguous fully qualified name which can be
    168     // baked into a token, i.e.
    169     // android_hardware_Foo_V1_0_IBar_Baz
    170     std::string tokenName() const;
    171 
    172     // Returns an absolute C++ namespace prefix, i.e.
    173     // ::android::hardware::Foo::V1_0.
    174     std::string cppNamespace() const;
    175 
    176     // Returns a name qualified assuming we are in cppNamespace, i.e.
    177     // IBar::Baz.
    178     std::string cppLocalName() const;
    179 
    180     // Returns a fully qualified absolute C++ type name, i.e.
    181     // ::android::hardware::Foo::V1_0::IBar::Baz.
    182     std::string cppName() const;
    183 
    184     // Returns the java package name, i.e. "android.hardware.Foo.V1_0".
    185     std::string javaPackage() const;
    186 
    187     // Returns the fully qualified java type name,
    188     // i.e. "android.hardware.Foo.V1_0.IBar.Baz"
    189     std::string javaName() const;
    190 
    191     bool endsWith(const FQName &other) const;
    192 
    193     // If this is android.hardware (at) 1.0::IFoo
    194     // package = "and" -> false
    195     // package = "android" -> true
    196     // package = "android.hardware (at) 1.0" -> false
    197     bool inPackage(const std::string &package) const;
    198 
    199     void getPackageComponents(std::vector<std::string> *components) const;
    200 
    201     void getPackageAndVersionComponents(
    202             std::vector<std::string> *components,
    203             bool cpp_compatible) const;
    204 
    205     // return major and minor version if they exist, else abort program.
    206     // Existence of version can be checked via hasVersion().
    207     size_t getPackageMajorVersion() const;
    208     size_t getPackageMinorVersion() const;
    209 
    210     // minor-- if result doesn't underflow, else abort.
    211     FQName downRev() const;
    212 
    213 private:
    214     bool mValid;
    215     bool mIsIdentifier;
    216     std::string mPackage;
    217     // mMajor == 0 means empty.
    218     size_t mMajor = 0;
    219     size_t mMinor = 0;
    220     std::string mName;
    221     std::string mValueName;
    222 
    223     void setVersion(const std::string &v);
    224     void clearVersion();
    225     void parseVersion(const std::string &majorStr, const std::string &minorStr);
    226 };
    227 
    228 static const FQName gIBaseFqName = FQName{"android.hidl.base (at) 1.0::IBase"};
    229 static const FQName gIBasePackageFqName = FQName{"android.hidl.base"};
    230 static const FQName gIManagerFqName = FQName{"android.hidl.manager (at) 1.0::IServiceManager"};
    231 static const FQName gIManagerPackageFqName = FQName{"android.hidl.manager"};
    232 
    233 }  // namespace android
    234 
    235 #endif  // FQNAME_H_
    236