blob: 8a05ffc3de1db79b1408d40b3aa34265d441cbb1 [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,
Zhuoyao Zhangdd85c5c2017-01-03 17:30:24 -080041 IMPL_STUB,
42 IMPL_PASSTHROUGH,
Martijn Coenen115d4282016-12-19 05:14:04 +010043};
44
45using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>;
Yifan Hong10fe0b52016-10-19 14:20:17 -070046
Andreas Huberc9410c72016-07-28 12:18:40 -070047struct Method {
48 Method(const char *name,
Andreas Huber881227d2016-08-02 14:20:21 -070049 std::vector<TypedVar *> *args,
Andreas Huber3599d922016-08-09 10:42:57 -070050 std::vector<TypedVar *> *results,
Iliyan Malchev639bff82016-08-13 14:24:11 -070051 bool oneway,
Steven Morelandd537ab02016-09-12 10:32:01 -070052 std::vector<Annotation *> *annotations);
Yifan Hong10fe0b52016-10-19 14:20:17 -070053 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 Huberc9410c72016-07-28 12:18:40 -070061
Andreas Huber881227d2016-08-02 14:20:21 -070062 std::string name() const;
63 const std::vector<TypedVar *> &args() const;
64 const std::vector<TypedVar *> &results() const;
Iliyan Malchev639bff82016-08-13 14:24:11 -070065 bool isOneway() const { return mOneway; }
Martijn Coenen115d4282016-12-19 05:14:04 +010066 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 Hong10fe0b52016-10-19 14:20:17 -070070 bool isHidlReserved() const { return mIsHidlReserved; }
Steven Morelandd537ab02016-09-12 10:32:01 -070071 const std::vector<Annotation *> &annotations() const;
Andreas Huber881227d2016-08-02 14:20:21 -070072
Steven Morelandef1a9fe2016-10-06 17:19:09 -070073 void setSerialId(size_t serial);
74 size_t getSerialId() const;
75
Steven Morelanda7a421a2016-09-07 08:35:18 -070076 void generateCppSignature(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -070077 const std::string &className = "",
78 bool specifyNamespaces = true) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -070079
Steven Moreland979e0992016-09-07 09:18:08 -070080 static std::string GetArgSignature(const std::vector<TypedVar *> &args,
81 bool specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -070082 static std::string GetJavaArgSignature(const std::vector<TypedVar *> &args);
Andreas Huber881227d2016-08-02 14:20:21 -070083
Iliyan Malchev40d474a2016-08-16 06:20:17 -070084 const TypedVar* canElideCallback() const;
85
Andreas Huber3599d922016-08-09 10:42:57 -070086 void dumpAnnotations(Formatter &out) const;
87
Andreas Huber70a59e12016-08-16 12:57:01 -070088 bool isJavaCompatible() const;
89
Andreas Huberc9410c72016-07-28 12:18:40 -070090private:
91 std::string mName;
Steven Morelandef1a9fe2016-10-06 17:19:09 -070092 size_t mSerial = 0;
Andreas Huber881227d2016-08-02 14:20:21 -070093 std::vector<TypedVar *> *mArgs;
94 std::vector<TypedVar *> *mResults;
Iliyan Malchev639bff82016-08-13 14:24:11 -070095 bool mOneway;
Steven Morelandd537ab02016-09-12 10:32:01 -070096 std::vector<Annotation *> *mAnnotations;
Andreas Huberc9410c72016-07-28 12:18:40 -070097
Yifan Hong10fe0b52016-10-19 14:20:17 -070098 bool mIsHidlReserved = false;
Martijn Coenen115d4282016-12-19 05:14:04 +010099 // The following fields have no meaning if mIsHidlReserved is false.
Yifan Hong10fe0b52016-10-19 14:20:17 -0700100 // hard-coded implementation for HIDL reserved methods.
Martijn Coenen115d4282016-12-19 05:14:04 +0100101 MethodImpl mCppImpl;
102 MethodImpl mJavaImpl;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700103
Andreas Huberc9410c72016-07-28 12:18:40 -0700104 DISALLOW_COPY_AND_ASSIGN(Method);
105};
106
Andreas Huber31629bc2016-08-03 09:06:40 -0700107struct TypedVar {
108 TypedVar(const char *name, Type *type);
109
110 std::string name() const;
111 const Type &type() const;
112
Andreas Huber70a59e12016-08-16 12:57:01 -0700113 bool isJavaCompatible() const;
114
Andreas Huber31629bc2016-08-03 09:06:40 -0700115private:
116 std::string mName;
117 Type *mType;
118
119 DISALLOW_COPY_AND_ASSIGN(TypedVar);
120};
121
Yifan Hong7763ab32016-12-13 17:42:11 -0800122struct TypedVarVector : public std::vector<TypedVar *> {
123 TypedVarVector() = default;
124
125 bool add(TypedVar *v);
126private:
127 std::set<std::string> mNames;
128};
129
Andreas Huberc9410c72016-07-28 12:18:40 -0700130} // namespace android
131
132#endif // METHOD_H_
133