From f1a481eb18f305073bb43ea539593c86e941d620 Mon Sep 17 00:00:00 2001 From: Jean-Francois Remacle <jean-francois.remacle@uclouvain.be> Date: Sun, 4 Mar 2001 19:55:25 +0000 Subject: [PATCH] Gmsh Plugin's --- Plugin/Makefile | 50 ++++++++++++++++++++++++++++ Plugin/Plugin.cpp | 85 +++++++++++++++++++++++++++++++++++++++++++++++ Plugin/Plugin.h | 60 +++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 Plugin/Makefile create mode 100644 Plugin/Plugin.cpp create mode 100644 Plugin/Plugin.h diff --git a/Plugin/Makefile b/Plugin/Makefile new file mode 100644 index 0000000000..8bf2a2e74e --- /dev/null +++ b/Plugin/Makefile @@ -0,0 +1,50 @@ +# $Id: Makefile,v 1.1 2001-03-04 19:55:25 remacle Exp $ +# +# Makefile for "libAdapt.a" +# + +.IGNORE: + +CC = c++ +AR = ar ruvs +RM = rm +RANLIB = ranlib + +LIB = ../lib/libPlugin.a +INCLUDE = -I../Common -I../DataStr + +C_FLAGS = -g -Wall +OS_FLAGS = +VERSION_FLAGS = + +RMFLAGS = -f +CFLAGS = $(C_FLAGS) $(OS_FLAGS) $(VERSION_FLAGS) $(INCLUDE) + +SRC = Plugin.cpp + +OBJ = $(SRC:.cpp=.o) + +.SUFFIXES: .o .cpp + +$(LIB): $(OBJ) + $(AR) $(LIB) $(OBJ) + $(RANLIB) $(LIB) + +.cpp.o: + $(CC) $(CFLAGS) -c $< + +clean: + $(RM) $(RMFLAGS) *.o + +lint: + $(LINT) $(CFLAGS) $(SRC) + +depend: + (sed '/^# DO NOT DELETE THIS LINE/q' Makefile && \ + $(CC) -MM $(CFLAGS) ${SRC} \ + ) >Makefile.new + cp Makefile Makefile.bak + cp Makefile.new Makefile + $(RM) $(RMFLAGS) Makefile.new + +# DO NOT DELETE THIS LINE diff --git a/Plugin/Plugin.cpp b/Plugin/Plugin.cpp new file mode 100644 index 0000000000..001267d55e --- /dev/null +++ b/Plugin/Plugin.cpp @@ -0,0 +1,85 @@ +#include <stdio.h> +#include <dlfcn.h> +#include <map> +#include "Plugin.h" +using namespace std; + +struct ltstr +{ + bool operator()(const char* s1, const char* s2) const + { + return strcmp(s1, s2) < 0; + } +}; + +class PluginContainer +{ +public : + typedef map<char*,GMSH_Plugin*,ltstr>::iterator iter; + map<char*,GMSH_Plugin*,ltstr> m; + iter begin() {return m.begin();} + iter end() {return m.end();} + iter find(char *c) {return m.find(c);} +}; + +GMSH_PluginManager *GMSH_PluginManager::instance = 0; + +GMSH_PluginManager::GMSH_PluginManager() +{ + allPlugins = new PluginContainer; +} + +GMSH_PluginManager::~GMSH_PluginManager() +{ + for(PluginContainer::iter it = allPlugins->begin(); + it != allPlugins->end(); + ++it)delete (*it).second; + delete allPlugins; +} + +GMSH_PluginManager* GMSH_PluginManager::Instance() +{ + if(!instance) + { + instance = new GMSH_PluginManager; + } + return instance; +} + +void GMSH_PluginManager::RegisterDefaultPlugins() +{ + char *homeplugins = getenv ("GMSHPLUGINSHOME"); + if(!homeplugins)return; + +} + +void GMSH_PluginManager::AddPlugin( char *dirName, char *pluginName) +{ + char dynamic_lib[1024]; + char plugin_name[256]; + class GMSH_Plugin* (*RegisterPlugin)(void); + sprintf(dynamic_lib,"%s/%s.so",dirName,pluginName); + void *hlib = dlopen (dynamic_lib,RTLD_NOW); + if(hlib == NULL) + { + throw dynamic_lib; + } + RegisterPlugin = (class GMSH_Plugin* (*)(void)) dlsym(hlib,GMSH_PluginEntry); + char *err = dlerror(); + if(err != NULL) + { + return; + } + + GMSH_Plugin *p = RegisterPlugin(); + p->hlib = hlib; + p->getName(plugin_name); + if(allPlugins->find(plugin_name) != allPlugins->end()) + { + return; + } + allPlugins->m[plugin_name] = p; +} + + + diff --git a/Plugin/Plugin.h b/Plugin/Plugin.h new file mode 100644 index 0000000000..d66966a9a2 --- /dev/null +++ b/Plugin/Plugin.h @@ -0,0 +1,60 @@ +#ifndef _PLUGIN_H_ +#define _PLUGIN_H_ +/* + The one who intend to create a plugin for gmsh have to + -) Create a dynamin lib (.so) containing 1 symbols + GMSH_Plugin * GMSH_RegisterPlugin (); + -) When there is an unacceptable error in the plugin, + just throw this, the plugin manager will be able to + catch the exception. +*/ +const char *GMSH_PluginEntry = "GMSH_RegisterPlugin"; + +class PluginContainer; +class GMSH_Plugin +{ +public : + /*this is there for internal use, this variable will be + used by the PluginManager, just forget it*/ + void *hlib; + /* 3 kind of plugins, one for cad, one for mesh, one for postpro*/ + typedef enum GMSH_PLUGIN_TYPE {GMSH_CAD_PLUGIN, GMSH_MESH_PLUGIN, GMSH_POSTPRO_PLUGIN}; + /* returns the type of plugin for downcasting GMSH_Plugin into + GMSH_CAD_Plugin, GMSH_Mesh_Plugin and GMSH_Post_Plugin */ + virtual GMSH_PLUGIN_TYPE getType() const = 0; + virtual void getName (char *name); + virtual void getInfos (char *author, + char *copyright, + char *help_text) const = 0; + /* When an error is thrown by the plugin, the plugin manager + will show the message and hopefully continue */ + virtual void CatchErrorMessage (char *errorMessage) const = 0; + /* gmsh style option, ca be loaded, saved and set*/ + virtual void SetOption (char *optionName, void *optionValue) = 0; + virtual int getNbOptions() const; + virtual void GetOption (char *optionName, void *optionValue) const = 0; +}; + +class GMSH_PluginManager +{ +/** + Registering all default plugins that are in $(GMSHPLUGINSHOME) + In fact, we will load all .so files in dir $(GMSHPLUGINSHOME) +*/ + void RegisterDefaultPlugins(); + GMSH_PluginManager(); + ~GMSH_PluginManager(); + static GMSH_PluginManager *instance; + PluginContainer* allPlugins; +public : + static GMSH_PluginManager *Instance(); + /** Dynamically add a plugin pluginName.so in dirName*/ + void AddPlugin(char *dirName, char *pluginName); + void CallPlugin (char *name); + void DestroyPlugin (char *name); + void SetPluginOption (char *pluginName, char *option, void *value); +}; +#endif + + + -- GitLab