[libxml++] [patch] Global initialization code



Christophe,

On Monday 13 June 2005 13:08, Thomas Jarosch wrote:
> > So it looks like a --disable-static-initialisation in the configure
> > script would solve the issue.
> >
> > Any objection ?
>
> Fine with me. Of course we have to add a destructor to the Document::Init
> class and reset the callbacks. The Document::Init constructor needs an
> additional parameter (default: true) if it should call xmlCleanupParser()
> during destruction.
>
> I had another idea:
> Would it be possible to create the global _init object via an include file?
>
> #define LIBXMLPP_DISABLE_STATIC_INIT 1
> #include <libxml++.h>
>
> Multiple applications which use libxml++ could still
> share the same binary library file.

I implemented the code like I wrote above. And it works :-)
It's not ABI compatible as the new library doesn't contain the static init 
object, but it's 100% API compatile. -> It's best to bumb the .so version
for the next release.

Please let me know what you think.

Cheers,
Thomas
diff -u -r -p libxml++-2.10.0/libxml++/document.cc libxml++.nostatic/libxml++/document.cc
--- libxml++-2.10.0/libxml++/document.cc	2005-02-11 12:21:58.000000000 +0100
+++ libxml++.nostatic/libxml++/document.cc	2005-06-16 18:39:15.561013805 +0200
@@ -135,16 +135,39 @@ void on_libxml_destruct(xmlNode* node)
 namespace xmlpp
 {
 
-Document::Init::Init()
+// Initialize static init_done member
+bool Document::Init::init_done = false;
+
+Document::Init::Init(bool do_cleanup)
 {
+   if(init_done)
+      return;
+
    xmlInitParser(); //Not always necessary, but necessary for thread safety.
    xmlRegisterNodeDefault(on_libxml_construct);
    xmlDeregisterNodeDefault(on_libxml_destruct);
    xmlThrDefRegisterNodeDefault(on_libxml_construct);
    xmlThrDefDeregisterNodeDefault(on_libxml_destruct);
+
+   this->do_cleanup = do_cleanup;
+   init_done = true;
 }
 
-Document::Init Document::init_;
+Document::Init::~Init()
+{
+   if (!init_done)
+      return;
+
+   xmlRegisterNodeDefault(NULL);
+   xmlDeregisterNodeDefault(NULL);
+   xmlThrDefRegisterNodeDefault(NULL);
+   xmlThrDefDeregisterNodeDefault(NULL);
+
+   if (do_cleanup)
+      xmlCleanupParser();
+
+   init_done = false;
+}
 
 Document::Document(const Glib::ustring& version)
   : impl_(xmlNewDoc((const xmlChar*)version.c_str()))
diff -u -r -p libxml++-2.10.0/libxml++/document.h libxml++.nostatic/libxml++/document.h
--- libxml++-2.10.0/libxml++/document.h	2004-02-13 21:09:42.000000000 +0100
+++ libxml++.nostatic/libxml++/document.h	2005-06-16 18:41:40.945510317 +0200
@@ -44,17 +44,20 @@ class Document;
  */
 class Document : NonCopyable
 {
-  //Ensure that libxml is properly initialised:
+  friend class DomParser;
+  friend class SaxParser;
+  
+public:
   class Init
   {
   public:
-    Init();
+    Init(bool do_cleanup=true);
+    ~Init();
+  private:
+    bool do_cleanup;
+    static bool init_done;
   };
   
-  friend class DomParser;
-  friend class SaxParser;
-  
-public:
   explicit Document(const Glib::ustring& version = "1.0");
   virtual ~Document();
   
@@ -175,8 +178,6 @@ private:
   virtual Glib::ustring do_write_to_string(const Glib::ustring& encoding, bool format);
   virtual void do_write_to_stream(std::ostream& output, const Glib::ustring& encoding, bool format);
 
-  static Init init_;
-
   Document(_xmlDoc* doc);
   _xmlDoc* impl_;
 };
diff -u -r -p libxml++-2.10.0/libxml++/libxml++.h libxml++.nostatic/libxml++/libxml++.h
--- libxml++-2.10.0/libxml++/libxml++.h	2004-09-12 23:05:07.000000000 +0200
+++ libxml++.nostatic/libxml++/libxml++.h	2005-06-16 18:39:15.565013434 +0200
@@ -22,4 +22,8 @@
 #include <libxml++/validators/validator.h>
 #include <libxml++/validators/dtdvalidator.h>
 
+#ifndef __LIBXMLPP_NO_STATIC_INIT
+  static xmlpp::Document::Init __libxmlpp_init_;
+#endif
+
 #endif //__LIBXMLCPP_H


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]