Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 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 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #ifndef METHOD_H_ |
| 18 | |
| 19 | #define METHOD_H_ |
| 20 | |
| 21 | #include <android-base/macros.h> |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 22 | #include <functional> |
| 23 | #include <hidl-util/Formatter.h> |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 24 | #include <map> |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 25 | #include <set> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 26 | #include <string> |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 27 | #include <vector> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 31 | struct Annotation; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 32 | struct Formatter; |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 33 | struct ScalarType; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 34 | struct Type; |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 35 | struct TypedVar; |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 36 | struct TypedVarVector; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 37 | |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 38 | enum MethodImplType { |
| 39 | IMPL_HEADER, |
| 40 | IMPL_PROXY, |
Zhuoyao Zhang | dd85c5c | 2017-01-03 17:30:24 -0800 | [diff] [blame] | 41 | IMPL_STUB, |
| 42 | IMPL_PASSTHROUGH, |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 46 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 47 | struct Method { |
| 48 | Method(const char *name, |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 49 | std::vector<TypedVar *> *args, |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 50 | std::vector<TypedVar *> *results, |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 51 | bool oneway, |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 52 | std::vector<Annotation *> *annotations); |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 53 | Method(const char *name, |
| 54 | std::vector<TypedVar *> *args, |
| 55 | std::vector<TypedVar *> *results, |
| 56 | bool oneway, |
| 57 | std::vector<Annotation *> *annotations, |
| 58 | size_t serial, |
| 59 | MethodImpl cppImpl, |
| 60 | MethodImpl javaImpl); |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 61 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 62 | std::string name() const; |
| 63 | const std::vector<TypedVar *> &args() const; |
| 64 | const std::vector<TypedVar *> &results() const; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 65 | bool isOneway() const { return mOneway; } |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 66 | bool overridesCppImpl(MethodImplType type) const; |
| 67 | bool overridesJavaImpl(MethodImplType type) const; |
| 68 | void cppImpl(MethodImplType type, Formatter &out) const; |
| 69 | void javaImpl(MethodImplType type, Formatter &out) const; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 70 | bool isHidlReserved() const { return mIsHidlReserved; } |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 71 | const std::vector<Annotation *> &annotations() const; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 72 | |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 73 | void setSerialId(size_t serial); |
| 74 | size_t getSerialId() const; |
| 75 | |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 76 | void generateCppSignature(Formatter &out, |
Yifan Hong | 068c552 | 2016-10-31 14:07:25 -0700 | [diff] [blame] | 77 | const std::string &className = "", |
| 78 | bool specifyNamespaces = true) const; |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 79 | |
Steven Moreland | 979e099 | 2016-09-07 09:18:08 -0700 | [diff] [blame] | 80 | static std::string GetArgSignature(const std::vector<TypedVar *> &args, |
| 81 | bool specifyNamespaces); |
Steven Moreland | a7a421a | 2016-09-07 08:35:18 -0700 | [diff] [blame] | 82 | static std::string GetJavaArgSignature(const std::vector<TypedVar *> &args); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 83 | |
Iliyan Malchev | 40d474a | 2016-08-16 06:20:17 -0700 | [diff] [blame] | 84 | const TypedVar* canElideCallback() const; |
| 85 | |
Andreas Huber | 3599d92 | 2016-08-09 10:42:57 -0700 | [diff] [blame] | 86 | void dumpAnnotations(Formatter &out) const; |
| 87 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 88 | bool isJavaCompatible() const; |
| 89 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 90 | private: |
| 91 | std::string mName; |
Steven Moreland | ef1a9fe | 2016-10-06 17:19:09 -0700 | [diff] [blame] | 92 | size_t mSerial = 0; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 93 | std::vector<TypedVar *> *mArgs; |
| 94 | std::vector<TypedVar *> *mResults; |
Iliyan Malchev | 639bff8 | 2016-08-13 14:24:11 -0700 | [diff] [blame] | 95 | bool mOneway; |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 96 | std::vector<Annotation *> *mAnnotations; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 97 | |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 98 | bool mIsHidlReserved = false; |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 99 | // The following fields have no meaning if mIsHidlReserved is false. |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 100 | // hard-coded implementation for HIDL reserved methods. |
Martijn Coenen | 115d428 | 2016-12-19 05:14:04 +0100 | [diff] [blame] | 101 | MethodImpl mCppImpl; |
| 102 | MethodImpl mJavaImpl; |
Yifan Hong | 10fe0b5 | 2016-10-19 14:20:17 -0700 | [diff] [blame] | 103 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 104 | DISALLOW_COPY_AND_ASSIGN(Method); |
| 105 | }; |
| 106 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 107 | struct TypedVar { |
| 108 | TypedVar(const char *name, Type *type); |
| 109 | |
| 110 | std::string name() const; |
| 111 | const Type &type() const; |
| 112 | |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 113 | bool isJavaCompatible() const; |
| 114 | |
Andreas Huber | 31629bc | 2016-08-03 09:06:40 -0700 | [diff] [blame] | 115 | private: |
| 116 | std::string mName; |
| 117 | Type *mType; |
| 118 | |
| 119 | DISALLOW_COPY_AND_ASSIGN(TypedVar); |
| 120 | }; |
| 121 | |
Yifan Hong | 7763ab3 | 2016-12-13 17:42:11 -0800 | [diff] [blame] | 122 | struct TypedVarVector : public std::vector<TypedVar *> { |
| 123 | TypedVarVector() = default; |
| 124 | |
| 125 | bool add(TypedVar *v); |
| 126 | private: |
| 127 | std::set<std::string> mNames; |
| 128 | }; |
| 129 | |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 130 | } // namespace android |
| 131 | |
| 132 | #endif // METHOD_H_ |
| 133 | |