Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/libraries/core/module/Plugin.cc @ 10552

Last change on this file since 10552 was 10552, checked in by landauf, 5 years ago

added PluginManager to load/unload plugins at runtime

  • Property svn:eol-style set to native
File size: 2.6 KB
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "Plugin.h"
30
31#include "ModuleInstance.h"
32#include "util/Output.h"
33#include "core/Core.h"
34
35namespace orxonox
36{
37    Plugin::Plugin(const std::string& name, const std::string& libraryName) : name_(name), libraryName_(libraryName)
38    {
39        this->referenceCounter_ = 0;
40        this->moduleInstance_ = NULL;
41    }
42
43    Plugin::~Plugin()
44    {
45        if (this->moduleInstance_ != NULL)
46        {
47            this->referenceCounter_ = 1; // force unloading
48            this->unload();
49        }
50    }
51
52    void Plugin::load()
53    {
54        if (this->referenceCounter_ == 0)
55        {
56            orxout(internal_info) << "Loading plugin " << this->name_ << "..." << endl;
57            this->moduleInstance_ = new ModuleInstance(this->libraryName_);
58            Core::getInstance().loadModule(this->moduleInstance_);
59        }
60        else
61        {
62            orxout(internal_info) << "Plugin " << this->name_ << " is already referenced" << endl;
63        }
64
65        this->referenceCounter_++;
66    }
67
68    void Plugin::unload()
69    {
70        this->referenceCounter_--;
71        if (this->referenceCounter_ == 0)
72        {
73            orxout(internal_info) << "Unloading plugin " << this->name_ << "..." << endl;
74            Core::getInstance().unloadModule(this->moduleInstance_);
75            delete this->moduleInstance_;
76            this->moduleInstance_ = NULL;
77        }
78        else if (this->referenceCounter_ > 0)
79        {
80            orxout(internal_info) << "Plugin " << this->name_ << " is still referenced" << endl;
81        }
82        else
83        {
84            orxout(internal_warning) << "Plugin " << this->name_ << " was dereferenced more often than it was reference" << endl;
85            this->referenceCounter_ = 0;
86        }
87    }
88}
Note: See TracBrowser for help on using the repository browser.