예제 #1
0
/**
 * @brief Open library
 * @param libName - [in] library file name
 * @return true - loaded successfully, false otherwise
 */
bool DynamicLibrary::Open( const PDL_CHAR * libName, bool resolveSymbols )
{
	if ( !libName ) { return false; }

	library_ =
#if PLATFORM_WIN32
		::LoadLibraryExA( libName, NULL,
		                  ( resolveSymbols ) ? 0 : DONT_RESOLVE_DLL_REFERENCES );
#elif PLATFORM_POSIX
		dlopen( libName, RTLD_GLOBAL | ( ( resolveSymbols ) ? RTLD_NOW : RTLD_LAZY ) );
#endif

	if ( library_ ) { libraryName_ = libName; }
	else
	{
		const pdl_string nameWithExt( pdl_string( libName ) + pdl_string( GetDefaultExt() ) );
		library_ =
#if PLATFORM_WIN32
			::LoadLibraryExA( nameWithExt.c_str(), NULL,
		                      ( resolveSymbols ) ? 0 : DONT_RESOLVE_DLL_REFERENCES );
#elif PLATFORM_POSIX
			dlopen( nameWithExt.c_str(), RTLD_GLOBAL | ( ( resolveSymbols ) ? RTLD_NOW : RTLD_LAZY ) );
#endif
		if ( library_ ) { libraryName_ = libName; }
	}

	return ( library_ );
}
예제 #2
0
/**
 * @brief Get class instance
 * @param className - [in] class name
 * @return pointer to class instance
 */
DynamicClass * DynamicLibrary::GetInstance( const PDL_CHAR * className )
{
	const InstanceMap::iterator loadedInstance( instances_.find( className ) );
	if ( loadedInstance != instances_.end() ) { return ( loadedInstance -> second ); }
	
	const pdl_string builderName( pdl_string( "Create" ) + pdl_string( className ) );
	
	// Tricky code to prevent compiler error
	// ISO C++ forbids casting between pointer-to-function and pointer-to-object
	DynamicBuilder builder = 0;
	void * symbol = GetSymbolByName( builderName.c_str() );
	( void ) memcpy( &builder, &symbol, sizeof( void * ) );
	
	if ( !builder )
	{
		instances_[ className ] = 0;
		throw LoaderException( pdl_string( "Class `" ) + pdl_string( className ) +
		                       pdl_string( "` not found in " )  + libraryName_ );
	}

	DynamicClass * instance = ( DynamicClass * )( *builder )();
	instances_[ className ] = instance;
	return instance;
}