Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/libraries/core/module/Plugin.cc @ 10818

Last change on this file since 10818 was 10553, checked in by landauf, 9 years ago

fixed reference counting in unload() - unsigned int cannot be < 0

  • Property svn:eol-style set to native
File size: 2.7 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        this->referenceCounter_++;
55        if (this->referenceCounter_ == 1) // increased from 0 to 1 -> load plugin
56        {
57            orxout(internal_info) << "Loading plugin " << this->name_ << "..." << endl;
58            this->moduleInstance_ = new ModuleInstance(this->libraryName_);
59            Core::getInstance().loadModule(this->moduleInstance_);
60        }
61        else
62            orxout(internal_info) << "Increased reference count for plugin " << this->name_ << " to " << this->referenceCounter_ << endl;
63    }
64
65    void Plugin::unload()
66    {
67        if (this->referenceCounter_ == 0)
68        {
69            orxout(internal_warning) << "Tried to dereference plugin " << this->name_ << " more often than it was referenced" << endl;
70            return;
71        }
72
73        this->referenceCounter_--;
74        if (this->referenceCounter_ == 0) // reduced from 1 to 0 -> load plugin
75        {
76            orxout(internal_info) << "Unloading plugin " << this->name_ << "..." << endl;
77            Core::getInstance().unloadModule(this->moduleInstance_);
78            delete this->moduleInstance_;
79            this->moduleInstance_ = NULL;
80        }
81        else
82            orxout(internal_info) << "Reduced reference count for plugin " << this->name_ << " to " << this->referenceCounter_ << endl;
83    }
84}
Note: See TracBrowser for help on using the repository browser.