Re: [ROOT] two stupid questions (again)

From: Maksim A. Nikulin (M.A.Nikulin@inp.nsk.su)
Date: Tue Dec 25 2001 - 07:54:17 MET


On Mon, 24 Dec 2001, Jacek M. Holeczek wrote:

> The most common answer to my "question one" was - "create a global static
> instance of some class, its constructor will be run at link time and its
> destructor will be called at unlink time (and make sure the proper
> functions are being called in the constructor and destructor)".
> This is, unfortunately, the solution that I have been using, but I got 
> sick of it. Each time I need to do some special actions while loading
> and/or unloading the library, I need to write a special additional
> "dummy" class, which does nothing except calling routines in the default
> constructor and destructor (I was wondering if there is something more
> elegant in CINT/ROOT foreseen for this purpose ...).
> Another interesting answer was - "add a static data member to the class,
> any static data member of the class is initiated at shared library
> loading time". The problem here is that there is no way to specify the
> function that should be called at "shared library UN-loading time".
> BTW. The linker usually provides a similar solution, often called an
> "entry point" - which can be a user's defined routine which does the
> initialization. But again there is no way to define a routine for the
> "exit".

Did you try something like this?

--- myclass.h --

class myclass {
	static int fN;
	static void init() { ++fN; if( 1 != fN ) {return;} /* ... */ }
	static void finish() { --fN; if( 0 != fN) {return;} /* ... */ }
public:
	myclass() { init(); /* ... */}
	~myclass() { /* ... */ finish(); }
}
--- end myclass.h --
--- myclass.cc --

int myclass::fN = 0;
//...
namespace {
myclass class;
}

--- end myclass.cc --

> Unfortunately this is definitely NOT the question that I meant.
> Here it is (I promised to ask it once I remember it again ...).
> Assume you have a shared library somewhere ... you load it ... now assume
> you need to "read" a configuration file which should be placed in the same
> subdirectory that the library comes from. Is there any easy way to learn
> from which subdirectory the shared library has been loaded ?
> Now, typically, you don't even know the name of the library - the only
> assumption is that - this is the same library that provides the function
> which tries to "read" the configuration file - so, first the function
> should find from which shared library it comes, then from which
> subdirectory it has been loaded, then find the configuration file there.
> Now, if it makes anything simpler - assume that this will be done while
> loading the library - just in the constructor of a "global static instance
> of some class" - as in the answer to my question "one" above.

If you know nothing, library have to know everything!
You can define the directory during compilation. Also function can get it
from the environment.

Maksim Nikulin <M.A.Nikulin@inp.nsk.su>



This archive was generated by hypermail 2b29 : Tue Jan 01 2002 - 17:51:14 MET