diff options
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/slice2cpp/Gen.cpp | 26 | ||||
-rw-r--r-- | cpp/src/slice2cppe/Gen.cpp | 11 |
2 files changed, 35 insertions, 2 deletions
diff --git a/cpp/src/slice2cpp/Gen.cpp b/cpp/src/slice2cpp/Gen.cpp index 8f51fc4173e..d6eae4aa3b3 100644 --- a/cpp/src/slice2cpp/Gen.cpp +++ b/cpp/src/slice2cpp/Gen.cpp @@ -3108,6 +3108,19 @@ Slice::Gen::ObjectVisitor::visitClassDefEnd(const ClassDefPtr& p) C << nl << "extern \"C\" { void " << initfuncname << "() {} }"; C << nl << "#endif"; } + + // + // We add a protected destructor to force heap instantiation of the class. + // + H.dec(); + H << sp << nl << "protected:"; + H.inc(); + H << sp << nl << "virtual ~" << fixKwd(p->name()) << "() {}"; + + if(!p->isAbstract() && !_doneStaticSymbol) + { + H << sp << nl << "friend class " << p->name() << "__staticInit;"; + } } H << eb << ';'; @@ -3115,14 +3128,23 @@ Slice::Gen::ObjectVisitor::visitClassDefEnd(const ClassDefPtr& p) if(!p->isAbstract() && !p->isLocal()) { // - // We need an instance here to trigger initialization if the implementation is in a shared libarry. + // We need an instance here to trigger initialization if the implementation is in a shared library. // But we do this only once per source file, because a single instance is sufficient to initialize // all of the globals in a shared library. + // For a Slice class Foo, we instantiate a dummy class Foo__staticInit instead of using a static + // Foo instance directly because Foo has a protected destructor. // if(!_doneStaticSymbol) { + H << sp << nl << "class " << p->name() << "__staticInit"; + H << sb; + H.dec(); + H << nl << "public:"; + H.inc(); + H << sp << nl << scoped << " _init;"; + H << eb << ';'; _doneStaticSymbol = true; - H << sp << nl << "static " << scoped << " __" << p->name() << "_init;"; + H << sp << nl << "static " << scoped << "__staticInit _" << p->name() << "_init;"; } } diff --git a/cpp/src/slice2cppe/Gen.cpp b/cpp/src/slice2cppe/Gen.cpp index c9e77de1445..8455816dbbc 100644 --- a/cpp/src/slice2cppe/Gen.cpp +++ b/cpp/src/slice2cppe/Gen.cpp @@ -2078,6 +2078,17 @@ Slice::Gen::ObjectVisitor::visitClassDefEnd(const ClassDefPtr& p) } + // + // We add a protected destructor to force heap instantiation of the class. + // + if(!p->isAbstract()) + { + H.dec(); + H << sp << nl << "protected:"; + H.inc(); + H << sp << nl << "virtual ~" << fixKwd(p->name()) << "() {}"; + } + H << eb << ';'; if(!p->isLocal()) |