blob: 5760d80933adeffe8c8a3049afd7388a44725245 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
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 Huberc9410c72016-07-28 12:18:40 -070017#ifndef METHOD_H_
18
19#define METHOD_H_
20
21#include <android-base/macros.h>
Yifan Hong10fe0b52016-10-19 14:20:17 -070022#include <functional>
23#include <hidl-util/Formatter.h>
Martijn Coenen115d4282016-12-19 05:14:04 +010024#include <map>
Yifan Hong7763ab32016-12-13 17:42:11 -080025#include <set>
Andreas Huberc9410c72016-07-28 12:18:40 -070026#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070027#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070028
29namespace android {
30
Andreas Huber3599d922016-08-09 10:42:57 -070031struct Annotation;
Andreas Huberc9410c72016-07-28 12:18:40 -070032struct Formatter;
Iliyan Malchev40d474a2016-08-16 06:20:17 -070033struct ScalarType;
Andreas Huberc9410c72016-07-28 12:18:40 -070034struct Type;
Andreas Huber31629bc2016-08-03 09:06:40 -070035struct TypedVar;
Yifan Hong7763ab32016-12-13 17:42:11 -080036struct TypedVarVector;
Andreas Huberc9410c72016-07-28 12:18:40 -070037
Martijn Coenen115d4282016-12-19 05:14:04 +010038enum MethodImplType {
Steven Moreland937408a2017-03-20 09:54:18 -070039 IMPL_INTERFACE,
Martijn Coenen115d4282016-12-19 05:14:04 +010040 IMPL_PROXY,
Yifan Hongcd2ae452017-01-31 14:33:40 -080041 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 Zhangdd85c5c2017-01-03 17:30:24 -080043 IMPL_PASSTHROUGH,
Martijn Coenen115d4282016-12-19 05:14:04 +010044};
45
46using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>;
Yifan Hong10fe0b52016-10-19 14:20:17 -070047
Andreas Huberc9410c72016-07-28 12:18:40 -070048struct Method {
49 Method(const char *name,
Andreas Huber881227d2016-08-02 14:20:21 -070050 std::vector<TypedVar *> *args,
Andreas Huber3599d922016-08-09 10:42:57 -070051 std::vector<TypedVar *> *results,
Iliyan Malchev639bff82016-08-13 14:24:11 -070052 bool oneway,
Steven Morelandd537ab02016-09-12 10:32:01 -070053 std::vector<Annotation *> *annotations);
Andreas Huberc9410c72016-07-28 12:18:40 -070054
Andreas Huber881227d2016-08-02 14:20:21 -070055 std::string name() const;
56 const std::vector<TypedVar *> &args() const;
57 const std::vector<TypedVar *> &results() const;
Iliyan Malchev639bff82016-08-13 14:24:11 -070058 bool isOneway() const { return mOneway; }
Martijn Coenen115d4282016-12-19 05:14:04 +010059 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 Hong10fe0b52016-10-19 14:20:17 -070063 bool isHidlReserved() const { return mIsHidlReserved; }
Andreas Huber37065d62017-02-07 14:36:54 -080064 bool isHiddenFromJava() const;
Steven Morelandd537ab02016-09-12 10:32:01 -070065 const std::vector<Annotation *> &annotations() const;
Andreas Huber881227d2016-08-02 14:20:21 -070066
Yifan Hongffa91392017-01-31 13:41:23 -080067 // Make a copy with the same name, args, results, oneway, annotations.
68 // Implementations, serial are not copied.
69 Method *copySignature() const;
70
Steven Morelandef1a9fe2016-10-06 17:19:09 -070071 void setSerialId(size_t serial);
72 size_t getSerialId() const;
73
Yifan Hongffa91392017-01-31 13:41:23 -080074 // 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 Morelanda7a421a2016-09-07 08:35:18 -070081 void generateCppSignature(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -070082 const std::string &className = "",
83 bool specifyNamespaces = true) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -070084
Yifan Hong932464e2017-03-30 15:40:22 -070085 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;
Andreas Huber881227d2016-08-02 14:20:21 -070089
Iliyan Malchev40d474a2016-08-16 06:20:17 -070090 const TypedVar* canElideCallback() const;
91
Andreas Huber3599d922016-08-09 10:42:57 -070092 void dumpAnnotations(Formatter &out) const;
93
Andreas Huber70a59e12016-08-16 12:57:01 -070094 bool isJavaCompatible() const;
95
Andreas Huberc9410c72016-07-28 12:18:40 -070096private:
97 std::string mName;
Steven Morelandef1a9fe2016-10-06 17:19:09 -070098 size_t mSerial = 0;
Andreas Huber881227d2016-08-02 14:20:21 -070099 std::vector<TypedVar *> *mArgs;
100 std::vector<TypedVar *> *mResults;
Iliyan Malchev639bff82016-08-13 14:24:11 -0700101 bool mOneway;
Steven Morelandd537ab02016-09-12 10:32:01 -0700102 std::vector<Annotation *> *mAnnotations;
Andreas Huberc9410c72016-07-28 12:18:40 -0700103
Yifan Hong10fe0b52016-10-19 14:20:17 -0700104 bool mIsHidlReserved = false;
Martijn Coenen115d4282016-12-19 05:14:04 +0100105 // The following fields have no meaning if mIsHidlReserved is false.
Yifan Hong10fe0b52016-10-19 14:20:17 -0700106 // hard-coded implementation for HIDL reserved methods.
Martijn Coenen115d4282016-12-19 05:14:04 +0100107 MethodImpl mCppImpl;
108 MethodImpl mJavaImpl;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700109
Andreas Huberc9410c72016-07-28 12:18:40 -0700110 DISALLOW_COPY_AND_ASSIGN(Method);
111};
112
Andreas Huber31629bc2016-08-03 09:06:40 -0700113struct TypedVar {
114 TypedVar(const char *name, Type *type);
115
116 std::string name() const;
117 const Type &type() const;
118
Andreas Huber70a59e12016-08-16 12:57:01 -0700119 bool isJavaCompatible() const;
120
Andreas Huber31629bc2016-08-03 09:06:40 -0700121private:
122 std::string mName;
123 Type *mType;
124
125 DISALLOW_COPY_AND_ASSIGN(TypedVar);
126};
127
Yifan Hong7763ab32016-12-13 17:42:11 -0800128struct TypedVarVector : public std::vector<TypedVar *> {
129 TypedVarVector() = default;
130
131 bool add(TypedVar *v);
132private:
133 std::set<std::string> mNames;
134};
135
Andreas Huberc9410c72016-07-28 12:18:40 -0700136} // namespace android
137
138#endif // METHOD_H_
139