blob: 38f0dd21ad4e54026c8c37fd18877624df1b5331 [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 {
39 IMPL_HEADER,
40 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
Steven Moreland979e0992016-09-07 09:18:08 -070085 static std::string GetArgSignature(const std::vector<TypedVar *> &args,
86 bool specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -070087 static std::string GetJavaArgSignature(const std::vector<TypedVar *> &args);
Andreas Huber881227d2016-08-02 14:20:21 -070088
Iliyan Malchev40d474a2016-08-16 06:20:17 -070089 const TypedVar* canElideCallback() const;
90
Andreas Huber3599d922016-08-09 10:42:57 -070091 void dumpAnnotations(Formatter &out) const;
92
Andreas Huber70a59e12016-08-16 12:57:01 -070093 bool isJavaCompatible() const;
94
Andreas Huberc9410c72016-07-28 12:18:40 -070095private:
96 std::string mName;
Steven Morelandef1a9fe2016-10-06 17:19:09 -070097 size_t mSerial = 0;
Andreas Huber881227d2016-08-02 14:20:21 -070098 std::vector<TypedVar *> *mArgs;
99 std::vector<TypedVar *> *mResults;
Iliyan Malchev639bff82016-08-13 14:24:11 -0700100 bool mOneway;
Steven Morelandd537ab02016-09-12 10:32:01 -0700101 std::vector<Annotation *> *mAnnotations;
Andreas Huberc9410c72016-07-28 12:18:40 -0700102
Yifan Hong10fe0b52016-10-19 14:20:17 -0700103 bool mIsHidlReserved = false;
Martijn Coenen115d4282016-12-19 05:14:04 +0100104 // The following fields have no meaning if mIsHidlReserved is false.
Yifan Hong10fe0b52016-10-19 14:20:17 -0700105 // hard-coded implementation for HIDL reserved methods.
Martijn Coenen115d4282016-12-19 05:14:04 +0100106 MethodImpl mCppImpl;
107 MethodImpl mJavaImpl;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700108
Andreas Huberc9410c72016-07-28 12:18:40 -0700109 DISALLOW_COPY_AND_ASSIGN(Method);
110};
111
Andreas Huber31629bc2016-08-03 09:06:40 -0700112struct TypedVar {
113 TypedVar(const char *name, Type *type);
114
115 std::string name() const;
116 const Type &type() const;
117
Andreas Huber70a59e12016-08-16 12:57:01 -0700118 bool isJavaCompatible() const;
119
Andreas Huber31629bc2016-08-03 09:06:40 -0700120private:
121 std::string mName;
122 Type *mType;
123
124 DISALLOW_COPY_AND_ASSIGN(TypedVar);
125};
126
Yifan Hong7763ab32016-12-13 17:42:11 -0800127struct TypedVarVector : public std::vector<TypedVar *> {
128 TypedVarVector() = default;
129
130 bool add(TypedVar *v);
131private:
132 std::set<std::string> mNames;
133};
134
Andreas Huberc9410c72016-07-28 12:18:40 -0700135} // namespace android
136
137#endif // METHOD_H_
138