<commit_before>//
// C++ Implementation: CompiledPresetFactory
//
// Description: 
//
//
// Author: <NAME> <<EMAIL>>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <dlfcn.h>
#include "CompiledPresetFactory.hpp"

typedef void Handle;
typedef Preset * CreateFunctor();
typedef void DestroyFunctor(Preset*);

class PresetLibrary {

	public:
		PresetLibrary(Handle * h, CreateFunctor * create, DestroyFunctor * destroy) :
			_handle(h), _createFunctor(create), _destroyFunctor(destroy) {}

		Handle * handle() { return _handle; }
		CreateFunctor * createFunctor() { return _createFunctor; }
		DestroyFunctor * destroyFunctor() { return _destroyFunctor; }

		~PresetLibrary() {
			dlclose(handle());
		}

	private:
		Handle * _handle; 
		CreateFunctor * _createFunctor;
		DestroyFunctor * _destroyFunctor;
	
};

CompiledPresetFactory::CompiledPresetFactory() {}

CompiledPresetFactory::~CompiledPresetFactory() {

for (PresetLibraryMap::iterator pos = _libraries.begin(); pos != _libraries.end(); ++pos)
	delete(pos->second);

}


PresetLibrary * CompiledPresetFactory::loadLibrary(const std::string & url) {

    if (_libraries.count(url))
	return _libraries[url];

    // load the preset library
    void* handle = dlopen(url.c_str(), RTLD_LAZY);
    if (!handle) {
        std::cerr << "[CompiledPresetFactory] Cannot load library: " << dlerror() << '\n';
        return 0;
    }

    // reset errors
    dlerror();

    // load the symbols
    CreateFunctor * create = (CreateFunctor*) dlsym(handle, "create");
    const char* dlsym_error = dlerror();
    if (dlsym_error) {
        std::cerr << "[CompiledPresetFactory] Cannot load symbol create: " << dlsym_error << '\n';
        return 0;
    }
    
    DestroyFunctor * destroy = (DestroyFunctor*) dlsym(handle, "destroy");
    dlsym_error = dlerror();
    if (dlsym_error) {
        std::cerr << "[CompiledPresetFactory] Cannot load symbol destroy: " << dlsym_error << '\n';
        return false;
    }

    PresetLibrary * library = new PresetLibrary(handle, create, destroy);

    _libraries.insert(std::make_pair(url, library));
    return library;
}


std::auto_ptr<Preset> CompiledPresetFactory::allocate(const std::string & url, const std::string & name,
	const std::string & author) {
	
	PresetLibrary * library;
	
	if ((library = loadLibrary(url)) == 0)
		return std::auto_ptr<Preset>(0);
	
	return std::auto_ptr<Preset>(library->createFunctor()());
}<commit_msg>CompiledPresetFactory: return 0 rather than false in loadLibrary()<commit_after>//
// C++ Implementation: CompiledPresetFactory
//
// Description: 
//
//
// Author: <NAME> <<EMAIL>>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <dlfcn.h>
#include "CompiledPresetFactory.hpp"

typedef void Handle;
typedef Preset * CreateFunctor();
typedef void DestroyFunctor(Preset*);

class PresetLibrary {

	public:
		PresetLibrary(Handle * h, CreateFunctor * create, DestroyFunctor * destroy) :
			_handle(h), _createFunctor(create), _destroyFunctor(destroy) {}

		Handle * handle() { return _handle; }
		CreateFunctor * createFunctor() { return _createFunctor; }
		DestroyFunctor * destroyFunctor() { return _destroyFunctor; }

		~PresetLibrary() {
			dlclose(handle());
		}

	private:
		Handle * _handle; 
		CreateFunctor * _createFunctor;
		DestroyFunctor * _destroyFunctor;
	
};

CompiledPresetFactory::CompiledPresetFactory() {}

CompiledPresetFactory::~CompiledPresetFactory() {

for (PresetLibraryMap::iterator pos = _libraries.begin(); pos != _libraries.end(); ++pos)
	delete(pos->second);

}


PresetLibrary * CompiledPresetFactory::loadLibrary(const std::string & url) {

    if (_libraries.count(url))
	return _libraries[url];

    // load the preset library
    void* handle = dlopen(url.c_str(), RTLD_LAZY);
    if (!handle) {
        std::cerr << "[CompiledPresetFactory] Cannot load library: " << dlerror() << '\n';
        return 0;
    }

    // reset errors
    dlerror();

    // load the symbols
    CreateFunctor * create = (CreateFunctor*) dlsym(handle, "create");
    const char* dlsym_error = dlerror();
    if (dlsym_error) {
        std::cerr << "[CompiledPresetFactory] Cannot load symbol create: " << dlsym_error << '\n';
        return 0;
    }
    
    DestroyFunctor * destroy = (DestroyFunctor*) dlsym(handle, "destroy");
    dlsym_error = dlerror();
    if (dlsym_error) {
        std::cerr << "[CompiledPresetFactory] Cannot load symbol destroy: " << dlsym_error << '\n';
        return 0;
    }

    PresetLibrary * library = new PresetLibrary(handle, create, destroy);

    _libraries.insert(std::make_pair(url, library));
    return library;
}


std::auto_ptr<Preset> CompiledPresetFactory::allocate(const std::string & url, const std::string & name,
	const std::string & author) {
	
	PresetLibrary * library;
	
	if ((library = loadLibrary(url)) == 0)
		return std::auto_ptr<Preset>(0);
	
	return std::auto_ptr<Preset>(library->createFunctor()());
}
{'fraction_non_alphanumeric': 0.14239935910274384, 'fraction_numerical': 0.0036050470658922492, 'mean_word_length': 4.054655870445344, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3094269', 'n_tokens_mistral': 1703, 'n_tokens_neox': 1593, 'n_words': 369}