|
Basic usage.To use the classes, include the header file ltmm/loader.hh into the application, and get a handle to the singleton ltmm::loader object.
#ifndef __IOSTREAM__ #include <iostream> #endif #ifndef __STDEXCEPT__ #include <stdexcept> #endif #ifndef LTMM_loader #include <ltmm/loader.hh> #endif #ifdef __CYGWIN__ double cos(double x) { return 1; } #endif int main() { try { std::map<std::string,ltmm::symbol_list>& preloaded = ltmm::loader<>::preloaded(); ltmm::loader<>& loader = ltmm::loader<>::instance();
Note that everything that has to do with the ltmm classes are put into a
#if defined(_MSC_VER) || defined(__CYGWIN__) ltmm::handle<>& handle = loader.load(""); #else ltmm::handle<>& handle = loader.load("libm.so"); #endif If the ltmm::loader<>::load member function somehow fails, it throws and ltmm::exception that contains the appropiate error message. The reason that the member function returns a reference rather than a value or a pointer, is to emphaise that the client code will always get the same handle when asking for the same library. Having a handle on the library, the application can then get a reference to any symbol the library exports:
ltmm::symbol* symbol = handle.find_symbol("cos"); Here the application gets a reference to the cosine function in the C maths library. Note, that ltmm::handle<>::find_symbol, always returns a new ltmm::symbol object, and it's up to the application to free the memory associated with the symbol. If caching of symbols is desired, the application can overload the ltmm::handle<> class (or wrap it) to do that. The function referenced by the ltmm::symbol can then straight forwardly be executed, and the result printed:
ltmm::function1<double,double> cos_func(symbol); double result = cos_func(0); std::cout << "cos(0) = " << result << std::endl; Finally, any errors from the classes must be caught:
} catch (std::exception& e) { std::cerr << e.what() << std::endl; return 1; } return 0; } // // EOF
Last update Mon Jun 27 13:25:17 2005 Christian Holm Created by DoxyGen 1.4.3-20050530 |