#include "mlp.h" #include // static map to hold all the language objects std::map mlp_language::languages ; mlp_language::mlp_language(const char *l) : name(l), handle(NULL){ languages[name] = this ; } mlp_language::~mlp_language(){ } const char *mlp_language::get_name(mlp_context *ctx){ return name.c_str() ; } MLP_EXTERN_C const char *mlp_language_get_name(mlp_context *ctx, mlp_language *lang){ lang->get_name(ctx) ; } void mlp_language::destroy(mlp_context *ctx){ languages[name] = NULL ; if (handle != NULL){ std::string name = this->name ; int rc = dlclose(handle) ; if (rc != 0){ ctx->set_error(MLP_PLUGIN_ERROR, "Can't close shared object %s: %s\n", name.c_str(), dlerror()) ; } } } MLP_EXTERN_C void mlp_language_destroy(mlp_context *ctx, mlp_language *lang){ lang->destroy(ctx) ; } void mlp_language::set_handle(void *handle){ this->handle = handle ; } mlp_language *mlp_language::init(mlp_context *ctx, const char *name){ ctx->clear_error() ; mlp_language *l = languages[std::string(name)] ; if (l != NULL){ // Language already initialized. return l ; } dlerror() ; std::string buf = std::string("libmlp_") + name + ".so" ; void *handle = dlopen(buf.c_str(), RTLD_LAZY) ; if (handle == NULL){ ctx->set_error(MLP_PLUGIN_ERROR, "Can't open shared object %s: %s\n", buf.c_str(), dlerror()) ; return NULL ; } // The fact of loading the .so should have created and registered the language object. // We can now get it from the map and return it. l = languages[std::string(name)] ; if (l == NULL){ ctx->set_error(MLP_PLUGIN_ERROR, "Language plugin %s (%s) did not register a language object", name, buf.c_str()) ; return NULL ; } l->set_handle(handle) ; return l ; } MLP_EXTERN_C mlp_language *mlp_language_init(mlp_context *ctx, const char *name){ return mlp_language::init(ctx, name) ; } MLP_EXTERN_C mlp_runtime *mlp_language_new_runtime(mlp_context *ctx, mlp_language *lang, const char *name){ return lang->new_runtime(ctx, name) ; }