Hello Dave and all,

Prior to Dave posting the example below of how to initialize
Xalan/Xerces before instantiating library objects as member data, I
figured out a way of doing it that also works, described below.  I'd
like to know if anyone has any thoughts regarding using a static member
function vs. as separate class as Dave describes below.  I'm not so far
along that I couldn't switch if my method isn't good for some reason.
I'm aware of usability issues such as the static function requiring the
user to explicitly get/delete as opposed to just instantiating the
classes and letting them go out of scope.  I'm more concerned with any
pitfalls this may cause in using the libraries, threading issues, and
the like.  The get/delete methods do implement a counter and I'm
intending to go back and pthread_mutex them for thread safety.  But
before I spend much more effort on this I'd like to know if I'm heading
down a dangerous path.

Thank you everyone for all your help these past couple of days.

-will 

-----Original Message-----
From: Will Sappington [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, February 14, 2007 6:10 PM
To: xalan-c-users@xml.apache.org
Subject: RE: Problem evaluating XPath expressions

Dave, thank you very much for taking the time to do this.  I followed up
my post below with another one later last night about an idea I hit upon
that is essentially what you are saying below.  I'm using a static
member function, getInstance() that initializes the X libraries only on
the first call and then instantiates a Profile class with 'new' and
returns a pointer to the instance.  As with your method below, the X
library initialization occurs before the parserLiaison constructs as
member data and everyone is happy.  It appears to be working fine.  I'll
read your suggestions below more thoroughly for things I could do to
improve my implementation.

Thanks again for all your help,

-will

-----Original Message-----
From: David Bertoni [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, February 14, 2007 4:28 PM
To: xalan-c-users@xml.apache.org
Subject: Re: Problem evaluating XPath expressions

Will Sappington wrote:
<snip>

Dave Bertoni wrote:

If you're going to do Xerces-C and Xalan-C initialization in the 
constructor of your class, you'll need to create a simple class that 
initializes Xerces-C and Xalan-C in the right order, and make that class
a 
member of your class, ensuring that it appears before any other Xerces-C
or 
Xalan-C class in the class definition.  Here's an example, in
pseudo-code:

class XercesXalanInit
{
public:

     XercesXalanInit()
     {
         if (++s_initCount == 1)
         {
             PlatformUtils::Initialize();
             XalanTransformer::initialize();
         }
     }

     ~XercesXalanInit()
     {
         if (--s_initCount == 0)
         {
             XalanTransformer::terminate();
             PlatformUtils::Terminate();
         }
     }

     static unsigned long   s_initCount;
};

XercesXalanInit::s_initCount = 0;

class myClass
{
public:

     myClass() :
         m_init(),
         m_liason()
     {
     }

     // all my public member functions here...

private:

     XercesXalanInit    m_init;

     XalanSourceTreeParserLiaison   m_liason;
};


Ultimately, it's much better if you initialize Xerces-C and Xalan-C
_once_, 
when your application starts, and not when each instance is created.
Also, 
you'll notice that I put some counting in the XercesXalanInit init
class, 
because you should only call them each once.  Finally, if your
application 
will be multi-threaded, you will either need to initialize the libraries

once, before any threads start, or make the counting in XercesXalanInit 
thread-safe.

Dave

Reply via email to