| 1 |
/* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */ |
| 2 |
|
| 3 |
/* AbiSource Application Framework |
| 4 |
* Copyright (C) 2001 Dom Lachowicz <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software; you can redistribute it and/or |
| 7 |
* modify it under the terms of the GNU General Public License |
| 8 |
* as published by the Free Software Foundation; either version 2 |
| 9 |
* of the License, or (at your option) any later version. |
| 10 |
* |
| 11 |
* This program is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU General Public License |
| 17 |
* along with this program; if not, write to the Free Software |
| 18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 19 |
* 02110-1301 USA. |
| 20 |
*/ |
| 21 |
|
| 22 |
#ifndef XAP_MODULE_H |
| 23 |
#define XAP_MODULE_H |
| 24 |
|
| 25 |
// This is an abstract class meant for dynamically |
| 26 |
// Loading and unloading modules. To load a module, |
| 27 |
// Pass the intended file name (dll, so, whatever) |
| 28 |
// To a valid child instance of this class. |
| 29 |
|
| 30 |
#ifdef HAVE_CONFIG_H |
| 31 |
#include "config.h" |
| 32 |
#endif |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
|
| 36 |
/* pre-emptive dismissal; ut_types.h is needed by just about everything, |
| 37 |
* so even if it's commented out in-file that's still a lot of work for |
| 38 |
* the preprocessor to do... |
| 39 |
*/ |
| 40 |
#ifndef UT_TYPES_H |
| 41 |
#include "ut_types.h" |
| 42 |
#endif |
| 43 |
|
| 44 |
#ifdef TOOLKIT_WIN |
| 45 |
#define WIN32_LEAN_AND_MEAN |
| 46 |
#include <windows.h> |
| 47 |
#define ABI_PLUGIN_DECLARE(name) static HANDLE s_hModule = static_cast<HANDLE>(NULL); extern "C" {BOOL APIENTRY DllMain( HANDLE hModule, DWORD /*ul_reason_for_call*/, LPVOID /*lpReserved*/ ) { s_hModule = hModule; return TRUE; } } |
| 48 |
#else |
| 49 |
#define ABI_PLUGIN_DECLARE(name) |
| 50 |
#endif |
| 51 |
|
| 52 |
// we want to have C linkage for both |
| 53 |
// this and for all of our required functions |
| 54 |
extern "C" { |
| 55 |
typedef struct { |
| 56 |
const char * name; |
| 57 |
const char * desc; |
| 58 |
const char * version; |
| 59 |
const char * author; |
| 60 |
const char * usage; |
| 61 |
} XAP_ModuleInfo; |
| 62 |
} |
| 63 |
|
| 64 |
class XAP_ModuleManager; |
| 65 |
|
| 66 |
typedef int (*XAP_Plugin_Registration) (XAP_ModuleInfo * mi); |
| 67 |
typedef int (*XAP_Plugin_VersionCheck) (UT_uint32 major, UT_uint32 minor, UT_uint32 release); |
| 68 |
|
| 69 |
class ABI_EXPORT XAP_Module { |
| 70 |
|
| 71 |
friend class XAP_ModuleManager; |
| 72 |
|
| 73 |
protected: |
| 74 |
XAP_Module (); |
| 75 |
|
| 76 |
virtual ~XAP_Module (void); |
| 77 |
|
| 78 |
// load this module into memory. true on success |
| 79 |
virtual bool load (const char * name) = 0; |
| 80 |
|
| 81 |
// unload this module from memory. true on success |
| 82 |
virtual bool unload (void) = 0; |
| 83 |
|
| 84 |
public: |
| 85 |
// marks the module as loaded; returns false if module is already loaded |
| 86 |
bool setSymbols (XAP_Plugin_Registration fnRegister, |
| 87 |
XAP_Plugin_Registration fnDeregister, |
| 88 |
XAP_Plugin_VersionCheck fnSupportsVersion); |
| 89 |
|
| 90 |
bool registered (); |
| 91 |
private: |
| 92 |
bool registerPending (); |
| 93 |
// silly names. fscking c/c++ has the keyword 'register' taken #:^) |
| 94 |
bool registerThySelf (); |
| 95 |
bool unregisterThySelf (); |
| 96 |
bool supportsAbiVersion (UT_uint32 major, UT_uint32 minor, |
| 97 |
UT_uint32 release); |
| 98 |
inline void setCreator (XAP_ModuleManager * creator) {m_creator = creator;} |
| 99 |
inline void setLoaded (bool bLoaded) {m_bLoaded = bLoaded;} |
| 100 |
|
| 101 |
public: |
| 102 |
|
| 103 |
// passed a symbol name and a void ** symbol, |
| 104 |
// *symbol refers to the actual representation of @symbol_name |
| 105 |
// |
| 106 |
// void (*func) (XAP_ModuleInfo *); |
| 107 |
// resolveSymbol ("abi_plugin_init", static_cast<void **>(&func)); |
| 108 |
// int result = func (&m_info); |
| 109 |
virtual bool resolveSymbol (const char * symbol_name, void ** symbol) = 0; |
| 110 |
|
| 111 |
// returns the name of this module, if it has one |
| 112 |
// if return is true, you must FREEP dest |
| 113 |
virtual bool getModuleName (char ** dest) const = 0; |
| 114 |
|
| 115 |
// returns the most recent error message from one of these |
| 116 |
// calls failing. If return is true, you must FREEP dest |
| 117 |
virtual bool getErrorMsg (char ** dest) const = 0; |
| 118 |
|
| 119 |
inline XAP_ModuleManager * getCreator () const {return m_creator;} |
| 120 |
|
| 121 |
inline const XAP_ModuleInfo *getModuleInfo (void) const {return &m_info;} |
| 122 |
|
| 123 |
private: |
| 124 |
|
| 125 |
XAP_Plugin_Registration m_fnRegister; |
| 126 |
XAP_Plugin_Registration m_fnDeregister; |
| 127 |
XAP_Plugin_VersionCheck m_fnSupportsVersion; |
| 128 |
|
| 129 |
XAP_ModuleManager * m_creator; |
| 130 |
bool m_bLoaded; |
| 131 |
bool m_bRegistered; |
| 132 |
int m_iStatus; |
| 133 |
const char * m_szSPI; |
| 134 |
XAP_ModuleInfo m_info; |
| 135 |
}; |
| 136 |
|
| 137 |
#if 0 // TODO: make me 100% and turn me on again one day |
| 138 |
#ifndef ABI_PLUGIN_SOURCE |
| 139 |
static inline bool |
| 140 |
_isCurrentAbiVersion(int a, int b, int c) |
| 141 |
{ |
| 142 |
int _a, _b, _c, _n; |
| 143 |
|
| 144 |
_n = sscanf( ABI_BUILD_VERSION , "%d.%d.%d", &_a, &_b, &_c); |
| 145 |
|
| 146 |
if (_n <= 1 || _a != a || _b != b) |
| 147 |
return false; |
| 148 |
|
| 149 |
if (3 == _n && _c != c) |
| 150 |
return false; |
| 151 |
|
| 152 |
return true; |
| 153 |
} |
| 154 |
#endif /* ! ABI_PLUGIN_SOURCE */ |
| 155 |
|
| 156 |
#define isCurrentAbiVersion(a,b,c) _isCurrentAbiVersion(a, b, c) |
| 157 |
|
| 158 |
#else |
| 159 |
#define isCurrentAbiVersion(a,b,c) true |
| 160 |
#endif |
| 161 |
|
| 162 |
#ifdef ABI_PLUGIN_SOURCE |
| 163 |
#define ABI_VERSION_STRING ABI_PLUGIN_VERSION |
| 164 |
#else |
| 165 |
#define ABI_VERSION_STRING ABI_BUILD_VERSION |
| 166 |
#endif |
| 167 |
|
| 168 |
#endif /* XAP_MODULE_H */ |