Home | History | Annotate | Download | only in hidl
      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 METHOD_H_
     18 
     19 #define METHOD_H_
     20 
     21 #include <android-base/macros.h>
     22 #include <functional>
     23 #include <hidl-util/Formatter.h>
     24 #include <map>
     25 #include <set>
     26 #include <string>
     27 #include <vector>
     28 
     29 namespace android {
     30 
     31 struct Annotation;
     32 struct Formatter;
     33 struct ScalarType;
     34 struct Type;
     35 struct TypedVar;
     36 struct TypedVarVector;
     37 
     38 enum MethodImplType {
     39     IMPL_INTERFACE,
     40     IMPL_PROXY,
     41     IMPL_STUB, // overrides the code in onTransact; IMPL_STUB_IMPL will be ignored
     42     IMPL_STUB_IMPL, // use this->method() instead of mImpl->method()
     43     IMPL_PASSTHROUGH,
     44 };
     45 
     46 using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>;
     47 
     48 struct Method {
     49     Method(const char *name,
     50            std::vector<TypedVar *> *args,
     51            std::vector<TypedVar *> *results,
     52            bool oneway,
     53            std::vector<Annotation *> *annotations);
     54 
     55     std::string name() const;
     56     const std::vector<TypedVar *> &args() const;
     57     const std::vector<TypedVar *> &results() const;
     58     bool isOneway() const { return mOneway; }
     59     bool overridesCppImpl(MethodImplType type) const;
     60     bool overridesJavaImpl(MethodImplType type) const;
     61     void cppImpl(MethodImplType type, Formatter &out) const;
     62     void javaImpl(MethodImplType type, Formatter &out) const;
     63     bool isHidlReserved() const { return mIsHidlReserved; }
     64     bool isHiddenFromJava() const;
     65     const std::vector<Annotation *> &annotations() const;
     66 
     67     // Make a copy with the same name, args, results, oneway, annotations.
     68     // Implementations, serial are not copied.
     69     Method *copySignature() const;
     70 
     71     void setSerialId(size_t serial);
     72     size_t getSerialId() const;
     73 
     74     // Fill implementation for HIDL reserved methods. mIsHidlReserved will be
     75     // set to true.
     76     void fillImplementation(
     77             size_t serial,
     78             MethodImpl cppImpl,
     79             MethodImpl javaImpl);
     80 
     81     void generateCppSignature(Formatter &out,
     82                               const std::string &className = "",
     83                               bool specifyNamespaces = true) const;
     84 
     85     void emitCppArgSignature(Formatter &out, bool specifyNamespaces) const;
     86     void emitCppResultSignature(Formatter &out, bool specifyNamespaces) const;
     87     void emitJavaArgSignature(Formatter &out) const;
     88     void emitJavaResultSignature(Formatter &out) const;
     89 
     90     const TypedVar* canElideCallback() const;
     91 
     92     void dumpAnnotations(Formatter &out) const;
     93 
     94     bool isJavaCompatible() const;
     95 
     96 private:
     97     std::string mName;
     98     size_t mSerial = 0;
     99     std::vector<TypedVar *> *mArgs;
    100     std::vector<TypedVar *> *mResults;
    101     bool mOneway;
    102     std::vector<Annotation *> *mAnnotations;
    103 
    104     bool mIsHidlReserved = false;
    105     // The following fields have no meaning if mIsHidlReserved is false.
    106     // hard-coded implementation for HIDL reserved methods.
    107     MethodImpl mCppImpl;
    108     MethodImpl mJavaImpl;
    109 
    110     DISALLOW_COPY_AND_ASSIGN(Method);
    111 };
    112 
    113 struct TypedVar {
    114     TypedVar(const char *name, Type *type);
    115 
    116     std::string name() const;
    117     const Type &type() const;
    118 
    119     bool isJavaCompatible() const;
    120 
    121 private:
    122     std::string mName;
    123     Type *mType;
    124 
    125     DISALLOW_COPY_AND_ASSIGN(TypedVar);
    126 };
    127 
    128 struct TypedVarVector : public std::vector<TypedVar *> {
    129     TypedVarVector() = default;
    130 
    131     bool add(TypedVar *v);
    132 private:
    133     std::set<std::string> mNames;
    134 };
    135 
    136 }  // namespace android
    137 
    138 #endif  // METHOD_H_
    139 
    140