Make stub disposable 25/167825/2
authorJunghoon Park <jh9216.park@samsung.com>
Mon, 22 Jan 2018 08:18:39 +0000 (17:18 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Mon, 22 Jan 2018 08:40:48 +0000 (17:40 +0900)
Change-Id: Idfbfca6414dc5b245c1b3ee445e6aaf4f263949e
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
idlc/cs_gen/cs_stub_gen.cc
idlc/cs_gen/cs_stub_gen.h

index 63ef05a..d0eda24 100644 (file)
@@ -63,7 +63,8 @@ void CsStubGen::GenInterfaces(std::ofstream& stream) {
 }
 
 void CsStubGen::GenInterface(std::ofstream& stream, const Interface& iface) {
-  stream << Tab(2) << "public sealed class " << iface.GetID() << NLine(1);
+  stream << Tab(2) << "public sealed class " << iface.GetID()
+         << " : IDisposable" << NLine(1);
   GenBrace(stream, TAB_SIZE * 2, [&]() {
     stream << Tab(3) << "private IntPtr _stub;" << NLine(1);
     stream << Tab(3)
@@ -87,6 +88,7 @@ void CsStubGen::GenInterface(std::ofstream& stream, const Interface& iface) {
     GenDisconnectedEvent(stream);
     GenCtor(stream, iface);
     GenCommonMethods(stream);
+    GenDisposable(stream, iface);
   });
 }
 
@@ -102,8 +104,9 @@ void CsStubGen::GenServiceBase(std::ofstream& stream, const Interface& iface) {
             "    protected ServiceBase()\n" \
             "    {\n" \
             "    }\n" \
-            "\n";
-
+            "\n" \
+            "    public abstract void OnCreate();\n" \
+            "    public abstract void OnTerminate();\n";
   stream << AddIndent(TAB_SIZE * 3, cls);
   GenDeclarations(stream, iface.GetDeclarations());
   stream << NLine(1);
@@ -250,6 +253,7 @@ void CsStubGen::GenConnectedEvent(std::ofstream& stream) {
       "{\n" \
       "    ServiceBase s = Activator.CreateInstance(_serviceType) as ServiceBase;\n" \
       "    s.Sender = sender;\n" \
+      "    s.OnCreate();\n" \
       "    _services.Add(s);\n" \
       "}\n";
   stream << AddIndent(TAB_SIZE * 3, method) << NLine(1);
@@ -263,6 +267,7 @@ void CsStubGen::GenDisconnectedEvent(std::ofstream& stream) {
       "    {\n" \
       "        if (i.Sender.Equals(sender))\n" \
       "        {\n" \
+      "            i.OnTerminate();\n" \
       "            _services.Remove(i);\n" \
       "            break;\n" \
       "        }\n" \
@@ -319,4 +324,48 @@ void CsStubGen::GenCommonMethods(std::ofstream& stream) {
   stream << AddIndent(TAB_SIZE * 3, method_get_services);
 }
 
+void CsStubGen::GenDisposable(std::ofstream& stream, const Interface& iface) {
+  const char* m =
+    "#region IDisposable Support\n" \
+    "private bool disposedValue = false;\n" \
+    "\n" \
+    "private void Dispose(bool disposing)\n" \
+    "{\n" \
+    "    if (!disposedValue)\n" \
+    "    {\n" \
+    "        if (disposing)\n" \
+    "        {\n" \
+    "        }\n" \
+    "\n" \
+    "        foreach (var i in _services)\n" \
+    "        {\n" \
+    "            i.OnTerminate();\n" \
+    "        }\n" \
+    "\n" \
+    "        _services = null;\n" \
+    "        if (_stub != null)\n" \
+    "            Interop.LibRPCPort.Stub.Destroy(_stub);\n" \
+    "        disposedValue = true;\n" \
+    "    }\n" \
+    "}\n" \
+    "\n" \
+    "~$$() {\n" \
+    "    Dispose(false);\n" \
+    "}\n" \
+    "\n" \
+    "public void Dispose()\n" \
+    "{\n" \
+    "    Dispose(true);\n" \
+    "    GC.SuppressFinalize(this);\n" \
+    "}\n" \
+    "#endregion\n";
+
+  GenTemplate(AddIndent(TAB_SIZE * 3, m), stream,
+    [&]()->std::string {
+      return iface.GetID();
+    }
+  );
+
+}
+
 }  // namespace tidl
index 3c84d33..897241a 100644 (file)
@@ -45,6 +45,7 @@ class CsStubGen : public CsGeneratorBase {
   void GenCommonMethods(std::ofstream& stream);
   void GenDeclarations(std::ofstream& stream, const Declarations& decls);
   void GenInvocation(std::ofstream& stream, const Declaration& decl);
+  void GenDisposable(std::ofstream& stream, const Interface& iface);
 };
 
 }  // namespace tidl