Method.h revision 7763ab3046cb998ac8a32b8d2312981631886813
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 <set> 25#include <string> 26#include <vector> 27 28namespace android { 29 30struct Annotation; 31struct Formatter; 32struct ScalarType; 33struct Type; 34struct TypedVar; 35struct TypedVarVector; 36 37using MethodImpl = std::function<void(Formatter &)>; 38 39struct Method { 40 Method(const char *name, 41 std::vector<TypedVar *> *args, 42 std::vector<TypedVar *> *results, 43 bool oneway, 44 std::vector<Annotation *> *annotations); 45 Method(const char *name, 46 std::vector<TypedVar *> *args, 47 std::vector<TypedVar *> *results, 48 bool oneway, 49 std::vector<Annotation *> *annotations, 50 size_t serial, 51 MethodImpl cppImpl, 52 MethodImpl javaImpl); 53 54 std::string name() const; 55 const std::vector<TypedVar *> &args() const; 56 const std::vector<TypedVar *> &results() const; 57 bool isOneway() const { return mOneway; } 58 void cppImpl(Formatter &out) const; 59 void javaImpl(Formatter &out) const; 60 bool isHidlReserved() const { return mIsHidlReserved; } 61 const std::vector<Annotation *> &annotations() const; 62 63 void setSerialId(size_t serial); 64 size_t getSerialId() const; 65 66 void generateCppSignature(Formatter &out, 67 const std::string &className = "", 68 bool specifyNamespaces = true) const; 69 70 static std::string GetArgSignature(const std::vector<TypedVar *> &args, 71 bool specifyNamespaces); 72 static std::string GetJavaArgSignature(const std::vector<TypedVar *> &args); 73 74 const TypedVar* canElideCallback() const; 75 76 void dumpAnnotations(Formatter &out) const; 77 78 bool isJavaCompatible() const; 79 80private: 81 std::string mName; 82 size_t mSerial = 0; 83 std::vector<TypedVar *> *mArgs; 84 std::vector<TypedVar *> *mResults; 85 bool mOneway; 86 std::vector<Annotation *> *mAnnotations; 87 88 bool mIsHidlReserved = false; 89 // The following fields has no meaning if mIsHidlReserved is false. 90 // hard-coded implementation for HIDL reserved methods. 91 MethodImpl mCppImpl = nullptr; 92 MethodImpl mJavaImpl = nullptr; 93 94 DISALLOW_COPY_AND_ASSIGN(Method); 95}; 96 97struct TypedVar { 98 TypedVar(const char *name, Type *type); 99 100 std::string name() const; 101 const Type &type() const; 102 103 bool isJavaCompatible() const; 104 105private: 106 std::string mName; 107 Type *mType; 108 109 DISALLOW_COPY_AND_ASSIGN(TypedVar); 110}; 111 112struct TypedVarVector : public std::vector<TypedVar *> { 113 TypedVarVector() = default; 114 115 bool add(TypedVar *v); 116private: 117 std::set<std::string> mNames; 118}; 119 120} // namespace android 121 122#endif // METHOD_H_ 123 124