summaryrefslogtreecommitdiff
path: root/idlc/cs_gen/cs_proxy_gen.cc
blob: 23e24f3185805dc99cc0d7e4b8f356a7ea1fc587 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "idlc/cs_gen/cs_proxy_gen.h"

namespace {
#include "idlc/cs_gen/cs_proxy_gen_cb.h"
}

namespace tidl {

CsProxyGen::CsProxyGen(std::shared_ptr<Document> doc)
    : CsGeneratorBase(doc) {}

void CsProxyGen::OnInitGen(std::ofstream& stream) {
  GenVersion(stream);
  stream << "using System;" << NLine(1)
         << "using System.Collections.Generic;" << NLine(1)
         << "using System.Threading;" << NLine(1)
         << "using Tizen.Applications;" << NLine(1)
	 << "using Tizen.Applications.RPCPort;" << NLine(1);
  GenNamespace(stream);
}

void CsProxyGen::OnFiniGen(std::ofstream& stream) {
}

void CsProxyGen::GenNamespace(std::ofstream& stream) {
  stream << "namespace RPCPort" << NLine(1);
  GenBrace(stream, 0, [&]() {
    stream << "namespace " << GetFileNamespace() << NLine(1);
    GenBrace(stream, 0, [&]() {
      GenStructures(stream);
      stream << Tab(1) << "namespace Proxy" << NLine(1);
      GenBrace(stream, TAB_SIZE, [&]() {
        GenInterfaces(stream);
      });
    });
  });
}

void CsProxyGen::GenInterfaces(std::ofstream& stream) {
  for (auto& i : GetDocument().GetBlocks()) {
    if (i->GetType() != Block::TYPE_INTERFACE)
      continue;
    Interface& iface = static_cast<Interface&>(*i);
    GenInterface(stream, iface);
    stream << NLine(1);
  }
}

void CsProxyGen::GenInterface(std::ofstream& stream, const Interface& iface) {
  stream << Tab(2) << "public class " << iface.GetID()
         << " : ProxyBase" << NLine(1);
  GenBrace(stream, TAB_SIZE * 2, [&]() {
    stream << CB_DATA_MEMBERS;
    GenCallbacks(stream, iface, true);
    GenDelegateId(stream, iface);
    GenMethodId(stream, iface);
    stream << CB_EVENT_METHODS;
    GenSerializer(stream);
    GenListSerializer(stream);
    GenCtor(stream, iface);
    GenConnectMethod(stream, iface);
    GenMethods(stream, iface);
  });
}

void CsProxyGen::GenCtor(std::ofstream& stream, const Interface& iface) {
  const char* m = "public $$(string appId) => _appId = appId;\n";

  stream << NLine(1);
  GenTemplate(AddIndent(TAB_SIZE * 3, m), stream,
    [&]()->std::string {
      return iface.GetID();
    });
}

void CsProxyGen::GenConnectMethod(std::ofstream& stream,
    const Interface& iface) {
  GenTemplate(CB_CONNECT_METHOD, stream,
    [&]()->std::string {
      return iface.GetID();
    });
  stream << NLine(1);
}

void CsProxyGen::GenMethods(std::ofstream& stream, const Interface& iface) {
  auto& decls = iface.GetDeclarations();

  for (auto& i : decls.GetDecls()) {
    if (i->GetMethodType() == Declaration::MethodType::DELEGATE)
      continue;

    if (!i->GetComments().empty())
      stream << AddIndent(TAB_SIZE * 3, i->GetComments());

    stream << Tab(3) << "public ";
    GenDeclaration(stream, *i, false);
    stream << NLine(1);
    GenBrace(stream, TAB_SIZE * 3, [&]() {
      GenInvocation(stream, *i);
    });
    stream << NLine(1);
  }
}

void CsProxyGen::GenInvocation(std::ofstream& stream, const Declaration& decl) {
  GenTemplate(CB_INVOCATION_PRE, stream,
    [&]()->std::string {
      std::string st;
      st += Tab(5)
         + "p.WriteInt((int)MethodId." + decl.GetID() + ");" + NLine(1);
      std::string m;
      std::string l;
      for (auto& i : decl.GetParameters().GetParams()) {
        auto& pt = i->GetParameterType();
        if (pt.GetDirection() == ParameterType::Direction::OUT)
          continue;
        m += ConvertTypeToSerializer(pt.GetBaseType(), i->GetID(), "p");
        if (IsDelegateType(pt.GetBaseType())) {
          l += "_delegateList.Add(" + i->GetID() + ");\n";
        }
      }

      st += AddIndent(TAB_SIZE * 5, m) + NLine(1);

      if (decl.GetMethodType() == Declaration::MethodType::SYNC)
        st += Tab(5) + "Parcel parcelReceived;" + NLine(1);
      st += Tab(5) + "lock (_lock)" + NLine(1);
      st += Tab(5) + "{" + NLine(1);
        if (!l.empty())
          st += AddIndent(TAB_SIZE * 6, l) + NLine(1);
        st += CB_INVOCATION_MID + NLine(1);
        if (decl.GetMethodType() == Declaration::MethodType::SYNC) {
          st += Tab(6) + "// Receive" + NLine(1);
          st += Tab(6)
             + "ConsumeCommand(out parcelReceived, Port);" + NLine(1);
        }
      st += Tab(5) + "}";

      // Deserialize
      if (decl.GetMethodType() == Declaration::MethodType::ASYNC) {
        return st;
      }

      const char* receive_block =
          "if (parcelReceived == null)\n" \
          "{\n" \
          "    throw new InvalidProtocolException();\n" \
          "}\n";
      st += NLine(1) + AddIndent(TAB_SIZE * 5, receive_block) + NLine(1);

      for (auto& i : decl.GetParameters().GetParams()) {
        if (i->GetParameterType().GetDirection() ==
            ParameterType::Direction::IN) {
          continue;
        }

        std::string c = ConvertTypeToDeserializer(
            i->GetParameterType().GetBaseType(),
            i->GetID(), "parcelReceived", false);
        if (c != "")
          st += AddIndent(TAB_SIZE * 5, c);
      }

      if (decl.GetType().ToString() != "void") {
        st += AddIndent(TAB_SIZE * 5,
                        ConvertTypeToDeserializer(decl.GetType(),
                                                  "ret", "parcelReceived"));
      }

      st += Tab(5) + "parcelReceived.Dispose();" + NLine(1);
      st += Tab(5) + "return ret;";

      return st;
    });
}

}  // namespace tidl