Advanced usage.

Advanced usage.

Preloaded symbols.

To register the preloaded symbols (needed on platforms without dynamic loading), the static member function ltmm::loader::preloaded should be called. It returns a map of the preloaded symbols, but the application needn't do anything with it.

Note, that it is important that the application is linked with Libtools -dlopen option for this to work - otherwise there'll be a compile time invalid reference to lt_preloaded_symbols.

    std::map<std::string,symbol_list>& preloaded = loader<>::preloaded();

Search path

The search path used by the loader can be modifed by calls to ltmm::loader::addto_search_path, and ltmm::loader::search_path. See also ltmm::loader class description for more on where libraries are looked for.

    loader<>& l = loader<>::instance();

    // Try the search path stuff 
    std::cout << "Initial search path: '" << l.search_path() 
              << "'" << std::endl;
    l.addto_search_path(".libs");
    std::cout << "More search path: '" << l.search_path() 
              << "'" << std::endl;
    l.search_path(".libs");
    std::cout << "Reset search path: '" << l.search_path() 

Iteration on handles

An application needn't do bookkeeping on the loaded libraries. The loader provides two ways of iterating over the loaded libraries. One using an external functor, and one using an ordinary iteration mechanism.

The first one, using an external counter is illustrated below. The application needs to define a functor that has the member function operator()(ltmm::handle<>&). For example, one could define a functor that simply prints the file name of the loaded modules:

struct print_modules 
{
  int i;
  print_modules() { i = 0; }
  int operator()(handle<>& h) {
    std::cout << "Handle " << i++ << ": " 
              << ltmm_util::basename(h.file_name(), true) << std::endl;
    return 0;
  }
};

Putting it to work, means instantising it, and passing that instance to the ltmm::loader::foreach_handle member function:

    print_modules p;
    l.foreach_handle(p);

The other way, using a (forward) iterator is straight forward. Simply get an iterator pointing at the beginning, and iterate until the end:

#else 
    std::cout << "Handle 0: module2.a" << std::endl
              << "Handle 1: module1.a" << std::endl;
#endif

    // Try the iterator 
    for (loader<>::handle_iterator i = l.begin_handle(); 

Opening self as a module

The classes supports opening the application itself as if it was a library. Suppose the application defined the function foo:
void DLLEXPORT foo() 
{
  std::cout << "Hello from foo in " << ltmm_util::basename(__FILE__)
            << " line " << __LINE__ << std::endl;
}

Then the application can get a handle to itself, by passing the empty string to the ltmm::loader<>::load member function, and that handle can then be used to obtain references to symbols in the application it self.

See also:
super.cc

module1.cc

module2.cc

Top of page
Last update Mon Jun 27 13:25:17 2005
Christian Holm
Created by DoxyGen 1.4.3-20050530