#ifndef _MLP_H_ #define _MLP_H_ #include #define MLP_VERSION "0.01" /* MLP error codes */ enum _mlp_error { MLP_NOT_SUPPORTED = -1000, /* Language does not support this feature */ MLP_NOT_IMPLEMENTED = -1001, /* Language supports this feature but plugin doesn't implement it yet... */ MLP_OK = 0, MLP_PLUGIN_ERROR, MLP_LANGUAGE_ERROR, MLP_LIBRARY_ERROR, MLP_RUNTIME_ERROR, MLP_FUNCTION_ERROR, MLP_NO_SUCH_FUNCTION, MLP_NO_SUCH_CLASS, MLP_INVALID_ARGUMENT, MLP_INVALID_RETURN_VALUE, MLP_INTERNAL_ERROR } ; typedef enum _mlp_error mlp_error ; /* MLP supported data types */ enum _mlp_type { MLP_INT, MLP_LONG, MLP_DOUBLE, MLP_STRING, MLP_OBJECT } ; typedef enum _mlp_type mlp_type ; #ifndef __cplusplus #define MLP_EXTERN_C struct _mlp_context ; typedef struct _mlp_context mlp_context ; struct _mlp_language ; typedef struct _mlp_language mlp_language ; struct _mlp_runtime ; typedef struct _mlp_runtime mlp_runtime ; struct _mlp_library ; typedef struct _mlp_library mlp_library ; struct _mlp_function ; typedef struct _mlp_function mlp_function ; struct _mlp_object ; typedef struct _mlp_object mlp_object ; struct _mlp_value ; typedef struct _mlp_value mlp_value ; #else #include #include #include #define MLP_EXTERN_C extern "C" class mlp_context ; class mlp_language ; class mlp_runtime ; class mlp_library ; class mlp_function ; class mlp_object ; class mlp_value ; typedef std::vector mlp_prototype ; typedef std::vector mlp_arguments ; /* MLP Context */ class mlp_context { mlp_error err ; char *err_msg ; std::map runtime_data ; public: mlp_context() ; int has_error() ; mlp_error get_error() ; const char *get_error_msg() ; void set_verror(mlp_error e, const char *fmt, va_list) ; void set_error(mlp_error e, const char *fmt, ...) ; void clear_error() ; void set_runtime_data(mlp_runtime *rt, const void *data) ; const void *get_runtime_data(mlp_runtime *rt) ; ~mlp_context() ; } ; /* MLP Language */ class mlp_language { std::string name ; static std::map languages ; void *handle ; protected: mlp_language(const char *name) ; ~mlp_language() ; private: void set_handle(void *handle) ; public: static mlp_language *init(mlp_context *ctx, const char *name) ; virtual mlp_runtime *new_runtime(mlp_context *ctx, const void *opts) = 0 ; // virtual runtime *attach_runtime(context *ctx, const void *) = 0 ; const char *get_name(mlp_context *ctx) ; void destroy(mlp_context *ctx) ; } ; /* MLP Runtime */ class mlp_runtime { mlp_language *lang ; protected: mlp_runtime(mlp_context *ctx, mlp_language *lang, const void *opts) ; virtual ~mlp_runtime() ; public: virtual mlp_library *load_library(mlp_context *ctx, const char *name, const void *opts) = 0 ; virtual void setup_context(mlp_context *ctx) = 0 ; //virtual value *call_function(context *ctx, const char *name, int nb_args, const value **args) = 0 ; //virtual object *new_object(context *ctx, const clazz *cls, int nb_args, const value **args) = 0 ; mlp_language *get_language(mlp_context *ctx) ; virtual void destroy(mlp_context *ctx) ; } ; /* MLP Library */ class mlp_library { protected: std::string name ; mlp_runtime *rt ; protected: mlp_library(mlp_context *ctx, mlp_runtime *rt, const char *name, const void *opts) ; virtual ~mlp_library() ; public: virtual mlp_function *get_function(mlp_context *ctx, mlp_type rtype, const char *name, mlp_prototype proto) = 0 ; const char *get_name(mlp_context *ctx) ; mlp_runtime *get_runtime(mlp_context *ctx) ; virtual void destroy(mlp_context *ctx) ; } ; /* MLP Function */ class mlp_function { protected: std::string name ; mlp_runtime *rt ; mlp_library *lib ; mlp_type rtype ; mlp_prototype proto ; protected: mlp_function(mlp_context *ctx, mlp_runtime *rt, mlp_library *lib, mlp_type rtype, const char *name, mlp_prototype) ; virtual ~mlp_function() ; public: virtual mlp_value *call(mlp_context *ctx, mlp_arguments args) = 0 ; const char *get_name(mlp_context *ctx) ; mlp_runtime *get_runtime(mlp_context *ctx) ; mlp_library *get_library(mlp_context *ctx) ; mlp_type get_return_type(mlp_context *ctx) ; virtual void destroy(mlp_context *ctx) ; } ; /* MLP Value */ class mlp_value { mlp_type t ; union { int i ; long l ; double d ; char *s ; const mlp_object *o ; } v ; bool thrown ; char *stringy ; private: mlp_value(mlp_type t) ; public: mlp_value(const mlp_value &v) ; mlp_value(int val) ; mlp_value(long val) ; mlp_value(double val) ; mlp_value(const char *val) ; mlp_value(const mlp_object *val) ; int to_int() ; long to_long() ; double to_double() ; const char *to_string() ; const mlp_object *to_object() ; mlp_type get_type() ; virtual ~mlp_value() ; } ; /* A class */ class mlp_class { char *name ; public: const char *get_name() ; } ; /* An object */ class mlp_object { mlp_class *cls ; public: mlp_class *get_class() ; } ; #endif MLP_EXTERN_C mlp_context *mlp_context_new() ; MLP_EXTERN_C void mlp_context_delete(mlp_context *ctx) ; MLP_EXTERN_C int mlp_context_has_error(mlp_context *ctx) ; MLP_EXTERN_C mlp_error mlp_context_get_error(mlp_context *ctx) ; MLP_EXTERN_C const char *mlp_context_get_error_info(mlp_context *ctx) ; MLP_EXTERN_C void mlp_context_set_verror(mlp_context *ctx, mlp_error e, const char *fmt, va_list va) ; MLP_EXTERN_C void mlp_context_set_error(mlp_context *ctx, mlp_error e, const char *fmt, ...) ; MLP_EXTERN_C void mlp_context_clear_error(mlp_context *ctx) ; MLP_EXTERN_C mlp_language *mlp_language_init(mlp_context *ctx, const char *name) ; MLP_EXTERN_C void mlp_language_destroy(mlp_context *ctx, mlp_language *lang) ; MLP_EXTERN_C mlp_value *mlp_value_new_from_long(long val) ; MLP_EXTERN_C mlp_value *mlp_value_new_from_double(double val) ; MLP_EXTERN_C mlp_value *mlp_value_new_from_string(const char *val) ; MLP_EXTERN_C mlp_value *mlp_value_new_from_object(mlp_object *val) ; MLP_EXTERN_C void mlp_value_delete(mlp_value *val) ; MLP_EXTERN_C long mlp_value_to_long(mlp_value *val) ; MLP_EXTERN_C double mlp_value_to_double(mlp_value *val) ; MLP_EXTERN_C const char *mlp_value_to_string(mlp_value *val) ; MLP_EXTERN_C const mlp_object *mlp_value_to_object(mlp_value *val) ; MLP_EXTERN_C void mlp_debug(int level, const char *fmt, ...) ; #endif