From d21cc8b50a7f186c2bf671a41b585dd5f126f608 Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Mon, 22 Jan 2018 17:18:39 +0900 Subject: [PATCH] Make stub disposable Change-Id: Idfbfca6414dc5b245c1b3ee445e6aaf4f263949e Signed-off-by: Junghoon Park --- idlc/cs_gen/cs_stub_gen.cc | 55 +++++++++++++++++++++++++++++++++++++++++++--- idlc/cs_gen/cs_stub_gen.h | 1 + 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/idlc/cs_gen/cs_stub_gen.cc b/idlc/cs_gen/cs_stub_gen.cc index 63ef05a..d0eda24 100644 --- a/idlc/cs_gen/cs_stub_gen.cc +++ b/idlc/cs_gen/cs_stub_gen.cc @@ -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 diff --git a/idlc/cs_gen/cs_stub_gen.h b/idlc/cs_gen/cs_stub_gen.h index 3c84d33..897241a 100644 --- a/idlc/cs_gen/cs_stub_gen.h +++ b/idlc/cs_gen/cs_stub_gen.h @@ -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 -- 2.7.4