blob: 6981b727a8a1a0b3df1a7d5093bbbfe14736c69d [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>
Yifan Hong7763ab32016-12-13 17:42:11 -080024#include <set>
Andreas Huberc9410c72016-07-28 12:18:40 -070025#include <string>
Andreas Huber881227d2016-08-02 14:20:21 -070026#include <vector>
Andreas Huberc9410c72016-07-28 12:18:40 -070027
28namespace android {
29
Andreas Huber3599d922016-08-09 10:42:57 -070030struct Annotation;
Andreas Huberc9410c72016-07-28 12:18:40 -070031struct Formatter;
Iliyan Malchev40d474a2016-08-16 06:20:17 -070032struct ScalarType;
Andreas Huberc9410c72016-07-28 12:18:40 -070033struct Type;
Andreas Huber31629bc2016-08-03 09:06:40 -070034struct TypedVar;
Yifan Hong7763ab32016-12-13 17:42:11 -080035struct TypedVarVector;
Andreas Huberc9410c72016-07-28 12:18:40 -070036
Yifan Hong10fe0b52016-10-19 14:20:17 -070037using MethodImpl = std::function<void(Formatter &)>;
38
Andreas Huberc9410c72016-07-28 12:18:40 -070039struct Method {
40 Method(const char *name,
Andreas Huber881227d2016-08-02 14:20:21 -070041 std::vector<TypedVar *> *args,
Andreas Huber3599d922016-08-09 10:42:57 -070042 std::vector<TypedVar *> *results,
Iliyan Malchev639bff82016-08-13 14:24:11 -070043 bool oneway,
Steven Morelandd537ab02016-09-12 10:32:01 -070044 std::vector<Annotation *> *annotations);
Yifan Hong10fe0b52016-10-19 14:20:17 -070045 Method(const char *name,
46 std::vector<TypedVar *> *args,
47 std::vector<TypedVar *> *results,
48 bool oneway,
49 std::vector<Annotation *> *annotations,
50 size_t serial,
51 MethodImpl cppImpl,
52 MethodImpl javaImpl);
Andreas Huberc9410c72016-07-28 12:18:40 -070053
Andreas Huber881227d2016-08-02 14:20:21 -070054 std::string name() const;
55 const std::vector<TypedVar *> &args() const;
56 const std::vector<TypedVar *> &results() const;
Iliyan Malchev639bff82016-08-13 14:24:11 -070057 bool isOneway() const { return mOneway; }
Yifan Hong10fe0b52016-10-19 14:20:17 -070058 void cppImpl(Formatter &out) const;
59 void javaImpl(Formatter &out) const;
60 bool isHidlReserved() const { return mIsHidlReserved; }
Steven Morelandd537ab02016-09-12 10:32:01 -070061 const std::vector<Annotation *> &annotations() const;
Andreas Huber881227d2016-08-02 14:20:21 -070062
Steven Morelandef1a9fe2016-10-06 17:19:09 -070063 void setSerialId(size_t serial);
64 size_t getSerialId() const;
65
Steven Morelanda7a421a2016-09-07 08:35:18 -070066 void generateCppSignature(Formatter &out,
Yifan Hong068c5522016-10-31 14:07:25 -070067 const std::string &className = "",
68 bool specifyNamespaces = true) const;
Steven Morelanda7a421a2016-09-07 08:35:18 -070069
Steven Moreland979e0992016-09-07 09:18:08 -070070 static std::string GetArgSignature(const std::vector<TypedVar *> &args,
71 bool specifyNamespaces);
Steven Morelanda7a421a2016-09-07 08:35:18 -070072 static std::string GetJavaArgSignature(const std::vector<TypedVar *> &args);
Andreas Huber881227d2016-08-02 14:20:21 -070073
Iliyan Malchev40d474a2016-08-16 06:20:17 -070074 const TypedVar* canElideCallback() const;
75
Andreas Huber3599d922016-08-09 10:42:57 -070076 void dumpAnnotations(Formatter &out) const;
77
Andreas Huber70a59e12016-08-16 12:57:01 -070078 bool isJavaCompatible() const;
79
Andreas Huberc9410c72016-07-28 12:18:40 -070080private:
81 std::string mName;
Steven Morelandef1a9fe2016-10-06 17:19:09 -070082 size_t mSerial = 0;
Andreas Huber881227d2016-08-02 14:20:21 -070083 std::vector<TypedVar *> *mArgs;
84 std::vector<TypedVar *> *mResults;
Iliyan Malchev639bff82016-08-13 14:24:11 -070085 bool mOneway;
Steven Morelandd537ab02016-09-12 10:32:01 -070086 std::vector<Annotation *> *mAnnotations;
Andreas Huberc9410c72016-07-28 12:18:40 -070087
Yifan Hong10fe0b52016-10-19 14:20:17 -070088 bool mIsHidlReserved = false;
89 // The following fields has no meaning if mIsHidlReserved is false.
90 // hard-coded implementation for HIDL reserved methods.
91 MethodImpl mCppImpl = nullptr;
92 MethodImpl mJavaImpl = nullptr;
93
Andreas Huberc9410c72016-07-28 12:18:40 -070094 DISALLOW_COPY_AND_ASSIGN(Method);
95};
96
Andreas Huber31629bc2016-08-03 09:06:40 -070097struct TypedVar {
98 TypedVar(const char *name, Type *type);
99
100 std::string name() const;
101 const Type &type() const;
102
Andreas Huber70a59e12016-08-16 12:57:01 -0700103 bool isJavaCompatible() const;
104
Andreas Huber31629bc2016-08-03 09:06:40 -0700105private:
106 std::string mName;
107 Type *mType;
108
109 DISALLOW_COPY_AND_ASSIGN(TypedVar);
110};
111
Yifan Hong7763ab32016-12-13 17:42:11 -0800112struct TypedVarVector : public std::vector<TypedVar *> {
113 TypedVarVector() = default;
114
115 bool add(TypedVar *v);
116private:
117 std::set<std::string> mNames;
118};
119
Andreas Huberc9410c72016-07-28 12:18:40 -0700120} // namespace android
121
122#endif // METHOD_H_
123