Commit 76a92fcf authored by jan.koester's avatar jan.koester
Browse files

test

parent e435c70d
Loading
Loading
Loading
Loading
+0 −1
Changes for plugins/template/template.cpp: 0 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -103,7 +103,6 @@ namespace blogi {

    };


    EXPORT PluginApi* create() {
        return new Template();
    }
+10 −32
Changes for src/plugin.cpp: 10 added lines, 32 removed lines.
Original line number Diff line number Diff line
@@ -152,6 +152,13 @@ void blogi::Plugin::loadPlugins(const char *path, PluginArgs *args){
                            throw std::runtime_error("Cannot load symbol 'create'. Error code: " + std::to_string(lastError));
                        }

                        destroy_t* destroy_plugin = (destroy_t*)GetProcAddress(pldata, "destroy");
                        if (destroy_plugin == nullptr) {
                            DWORD lastError = GetLastError();
                            FreeLibrary(ldplg->pldata);
                            throw std::runtime_error("Cannot load symbol 'destroy'. Error code: " + std::to_string(lastError));
                        }

#else // Linux/Unix
                if (entry.path().extension() == ".so") {
                    try{
@@ -178,11 +185,12 @@ void blogi::Plugin::loadPlugins(const char *path, PluginArgs *args){
                            err[libhttppp::HTTPException::Error] << "Cannot load symbol create: " << (dlsym_error ? dlsym_error : "Unknown error") << '\n';
                            throw err;
                        }
                        destroy_t* destroy_plugin = (destroy_t*) dlsym(ldplg->pldata, "destroy");
#endif
                        PluginApi *plg=create_plugin();
                        plg->setArgs(args);
                        plg->initPlugin();
                        ldplg->ins=plg;
                        ldplg->ins=std::unique_ptr<PluginApi, PluginApiDeleter>(plg, PluginApiDeleter(destroy_plugin,ldplg->pldata));

                        if(_lastPlugin){
                            _lastPlugin->next=std::move(ldplg);
@@ -206,9 +214,6 @@ void blogi::Plugin::loadPlugins(const char *path, PluginArgs *args){
}

blogi::Plugin::~Plugin(){
    for(PluginData *curdat=_firstPlugin.get(); curdat; curdat=curdat->next.get()){
        delete static_cast<PluginApi*>(curdat->ins);
    }
}

const blogi::Plugin::PluginData* blogi::Plugin::getFirstPlugin() const{
@@ -216,42 +221,15 @@ const blogi::Plugin::PluginData* blogi::Plugin::getFirstPlugin() const{
}

blogi::Plugin::PluginData::~PluginData(){
#ifdef  Windows
    // Use a safety check to ensure the handle is valid
    if (pldata) {
        // GetProcAddress is the Windows equivalent of dlsym
        // The cast to a function pointer type is necessary
        destroy_t* destroy_plugin = (destroy_t*)GetProcAddress(pldata, "destroy");

        // Check if the symbol was found. GetProcAddress returns nullptr on failure.
        if (destroy_plugin) {
            // Call the destroy function if it was successfully loaded
            (*destroy_plugin)(ins);
        }
        else {
            // It's good practice to report if the symbol could not be found
            DWORD lastError = GetLastError();
            std::cerr << "Cannot load symbol destroy. Error code: " << lastError << '\n';
        }

        // FreeLibrary is the Windows equivalent of dlclose.
        // It's crucial to call this even if GetProcAddress failed, to prevent a resource leak.
        FreeLibrary(pldata);
    }
#else
    if (pldata)
        dlclose(pldata);
#endif //  Windows
}

blogi::Plugin::PluginData::PluginData(){
    pldata = nullptr;
    next=nullptr;
    ins=nullptr;
}

blogi::PluginApi * blogi::Plugin::PluginData::getInstace() const{
    return ins;
    return ins.get();
}

const blogi::Plugin::PluginData *blogi::Plugin::PluginData::getNextPlg() const{
+33 −2
Changes for src/plugin.h: 33 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -46,7 +46,38 @@
#define EXPORT extern "C"
#endif


namespace blogi {

    class PluginApi;

    typedef void destroy_t(PluginApi*);

struct PluginApiDeleter {
    private:
        destroy_t* destroyer = nullptr;
        void* pldata = nullptr;
    public:
        PluginApiDeleter() = default;
        PluginApiDeleter(destroy_t* fn, void* handle) : destroyer(fn), pldata(handle) {};
        PluginApiDeleter(const PluginApiDeleter&) = default;
        PluginApiDeleter& operator=(const PluginApiDeleter&) = default;

        void operator()(PluginApi* p) const {
            if (p && destroyer) {
                destroyer(p);
            }

            if (pldata) {
#ifdef  Windows
                FreeLibrary(pldata);
#else
                dlclose(pldata);
#endif
            }
        };
    };

    struct PluginArgs{
        std::vector<std::unique_ptr<dbpp::Database>> database;
        std::unique_ptr<Auth>                                         auth;
@@ -130,10 +161,10 @@ namespace blogi {
#else
            void* pldata;
#endif // Windows
            PluginApi  *ins;
            std::unique_ptr<PluginApi,PluginApiDeleter> ins=nullptr;
            std::unique_ptr<PluginData> next = nullptr;
        public:
            ~PluginData();
            virtual ~PluginData();
            PluginApi  *getInstace() const;
            const PluginData  *getNextPlg() const;
            friend class Plugin;