blob: ac681a5050bfd05c1ef0364e2d9d562a53b4bcc8 [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,
41 IMPL_STUB
42};
43
44using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>;
Yifan Hong10fe0b52016-10-19 14:20:17 -070045
Andreas Huberc9410c72016-07-28 12:18:40 -070046struct Method {
47 Method(const char *name,
Andreas Huber881227d2016-08-02 14:20:21 -070048 std::vector<TypedVar *> *args,
Andreas Huber3599d922016-08-09 10:42:57 -070049 std::vector<TypedVar *> *results,
Iliyan Malchev639bff82016-08-13 14:24:11 -070050 bool oneway,
Steven Morelandd537ab02016-09-12 10:32:01 -070051 std::vector<Annotation *> *annotations);
Yifan Hong10fe0b52016-10-19 14:20:17 -070052 Method(const char *name,
53 std::vector<TypedVar *> *args,
54 std::vector<TypedVar *> *results,
55 bool oneway,
56 std::vector<Annotation *> *annotations,
57 size_t serial,
58 MethodImpl cppImpl,
59 MethodImpl javaImpl);
Andreas Huberc9410c72016-07-28 12:18:40 -070060
Andreas Huber881227d2016-08-02 14:20:21 -070061 std::string name() const;
62 const std::vector<TypedVar *> &args() const;
63 const std::vector<TypedVar *> &results() const;
Iliyan Malchev639bff82016-08-13 14:24:11 -070064 bool isOneway() const { return mOneway; }
Martijn Coenen115d4282016-12-19 05:14:04 +010065 bool overridesCppImpl(MethodImplType type) const;
66 bool overridesJavaImpl(MethodImplType type) const;
67 void cppImpl(MethodImplType type, Formatter &out) const;
68 void javaImpl(MethodImplType type, Formatter &out) const;
Yifan Hong10fe0b52016-10-19 14:20:17 -070069 bool isHidlReserved() const { return mIsHidlReserved; }
Steven Morelandd537ab02016-09-12 10:32:01 -070070 const std::vector<Annotation *> &annotations() const;
Andreas Huber881227d2016-08-02 14:20:21 -070071
Steven Morelandef1a9fe2016-10-06 17:19:09 -070072 void setSerialId(size_t serial);
73 size_t getSerialId() const;
74
Steven Morelanda7a421a2016-09-07 08:35:18 -070075 void generateCppSignature(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -070076 const std::string &className = "",
77 bool specifyNamespaces = true) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -070078
Steven Moreland979e0992016-09-07 09:18:08 -070079 static std::string GetArgSignature(const std::vector<TypedVar *> &args,
80 bool specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -070081 static std::string GetJavaArgSignature(const std::vector<TypedVar *> &args);
Andreas Huber881227d2016-08-02 14:20:21 -070082
Iliyan Malchev40d474a2016-08-16 06:20:17 -070083 const TypedVar* canElideCallback() const;
84
Andreas Huber3599d922016-08-09 10:42:57 -070085 void dumpAnnotations(Formatter &out) const;
86
Andreas Huber70a59e12016-08-16 12:57:01 -070087 bool isJavaCompatible() const;
88
Andreas Huberc9410c72016-07-28 12:18:40 -070089private:
90 std::string mName;
Steven Morelandef1a9fe2016-10-06 17:19:09 -070091 size_t mSerial = 0;
Andreas Huber881227d2016-08-02 14:20:21 -070092 std::vector<TypedVar *> *mArgs;
93 std::vector<TypedVar *> *mResults;
Iliyan Malchev639bff82016-08-13 14:24:11 -070094 bool mOneway;
Steven Morelandd537ab02016-09-12 10:32:01 -070095 std::vector<Annotation *> *mAnnotations;
Andreas Huberc9410c72016-07-28 12:18:40 -070096
Yifan Hong10fe0b52016-10-19 14:20:17 -070097 bool mIsHidlReserved = false;
Martijn Coenen115d4282016-12-19 05:14:04 +010098 // The following fields have no meaning if mIsHidlReserved is false.
Yifan Hong10fe0b52016-10-19 14:20:17 -070099 // hard-coded implementation for HIDL reserved methods.
Martijn Coenen115d4282016-12-19 05:14:04 +0100100 MethodImpl mCppImpl;
101 MethodImpl mJavaImpl;
Yifan Hong10fe0b52016-10-19 14:20:17 -0700102
Andreas Huberc9410c72016-07-28 12:18:40 -0700103 DISALLOW_COPY_AND_ASSIGN(Method);
104};
105
Andreas Huber31629bc2016-08-03 09:06:40 -0700106struct TypedVar {
107 TypedVar(const char *name, Type *type);
108
109 std::string name() const;
110 const Type &type() const;
111
Andreas Huber70a59e12016-08-16 12:57:01 -0700112 bool isJavaCompatible() const;
113
Andreas Huber31629bc2016-08-03 09:06:40 -0700114private:
115 std::string mName;
116 Type *mType;
117
118 DISALLOW_COPY_AND_ASSIGN(TypedVar);
119};
120
Yifan Hong7763ab32016-12-13 17:42:11 -0800121struct TypedVarVector : public std::vector<TypedVar *> {
122 TypedVarVector() = default;
123
124 bool add(TypedVar *v);
125private:
126 std::set<std::string> mNames;
127};
128
Andreas Huberc9410c72016-07-28 12:18:40 -0700129} // namespace android
130
131#endif // METHOD_H_
132