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