Method.h revision 115d428f9c38c078d679a98942c47ce4a17bd599
1/*
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
17#ifndef METHOD_H_
18
19#define METHOD_H_
20
21#include <android-base/macros.h>
22#include <functional>
23#include <hidl-util/Formatter.h>
24#include <map>
25#include <set>
26#include <string>
27#include <vector>
28
29namespace android {
30
31struct Annotation;
32struct Formatter;
33struct ScalarType;
34struct Type;
35struct TypedVar;
36struct TypedVarVector;
37
38enum MethodImplType {
39    IMPL_HEADER,
40    IMPL_PROXY,
41    IMPL_STUB
42};
43
44using MethodImpl = std::map<MethodImplType, std::function<void(Formatter &)>>;
45
46struct Method {
47    Method(const char *name,
48           std::vector<TypedVar *> *args,
49           std::vector<TypedVar *> *results,
50           bool oneway,
51           std::vector<Annotation *> *annotations);
52    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);
60
61    std::string name() const;
62    const std::vector<TypedVar *> &args() const;
63    const std::vector<TypedVar *> &results() const;
64    bool isOneway() const { return mOneway; }
65    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;
69    bool isHidlReserved() const { return mIsHidlReserved; }
70    const std::vector<Annotation *> &annotations() const;
71
72    void setSerialId(size_t serial);
73    size_t getSerialId() const;
74
75    void generateCppSignature(Formatter &out,
76                              const std::string &className = "",
77                              bool specifyNamespaces = true) const;
78
79    static std::string GetArgSignature(const std::vector<TypedVar *> &args,
80                                       bool specifyNamespaces);
81    static std::string GetJavaArgSignature(const std::vector<TypedVar *> &args);
82
83    const TypedVar* canElideCallback() const;
84
85    void dumpAnnotations(Formatter &out) const;
86
87    bool isJavaCompatible() const;
88
89private:
90    std::string mName;
91    size_t mSerial = 0;
92    std::vector<TypedVar *> *mArgs;
93    std::vector<TypedVar *> *mResults;
94    bool mOneway;
95    std::vector<Annotation *> *mAnnotations;
96
97    bool mIsHidlReserved = false;
98    // The following fields have no meaning if mIsHidlReserved is false.
99    // hard-coded implementation for HIDL reserved methods.
100    MethodImpl mCppImpl;
101    MethodImpl mJavaImpl;
102
103    DISALLOW_COPY_AND_ASSIGN(Method);
104};
105
106struct TypedVar {
107    TypedVar(const char *name, Type *type);
108
109    std::string name() const;
110    const Type &type() const;
111
112    bool isJavaCompatible() const;
113
114private:
115    std::string mName;
116    Type *mType;
117
118    DISALLOW_COPY_AND_ASSIGN(TypedVar);
119};
120
121struct TypedVarVector : public std::vector<TypedVar *> {
122    TypedVarVector() = default;
123
124    bool add(TypedVar *v);
125private:
126    std::set<std::string> mNames;
127};
128
129}  // namespace android
130
131#endif  // METHOD_H_
132
133