Orxonox  0.0.5 Codename: Arcturus
ScopedSingletonWrapper.h
Go to the documentation of this file.
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  * Reto Grieder
24  * Co-authors:
25  * ...
26  *
27  */
28 
44 #ifndef __ScopedSingletonWrapper_H__
45 #define __ScopedSingletonWrapper_H__
46 
47 #include "core/CorePrereqs.h"
48 
49 #include <cassert>
50 #include "util/Exception.h"
51 #include "util/Singleton.h"
52 #include "Scope.h"
53 
54 namespace orxonox
55 {
60  {
61  public:
64  : className_(className)
65  { }
66  virtual ~ScopedSingletonWrapper() = default;
67 
68  protected:
70  };
71 
87  template <class T, bool allowedToFail>
89  {
90  public:
92  : ScopedSingletonWrapper(className)
93  , singletonPtr_(nullptr)
94  {
95  }
96 
98  {
99  if (singletonPtr_)
100  this->destroy(singletonPtr_);
101  }
102 
104  virtual void activated() override
105  {
106  assert(singletonPtr_ == nullptr);
107  singletonPtr_ = new T();
108  }
109 
111  virtual void deactivated() override
112  {
113  assert(singletonPtr_ != nullptr);
114  this->destroy(singletonPtr_);
115  singletonPtr_ = nullptr;
116  }
117 
120  {
121  singletonPtr_->destroy();
122  }
124  void destroy(void*)
125  {
126  delete singletonPtr_;
127  }
128 
129  private:
131  };
132 
142  template <class T>
144  {
145  public:
147  : ScopedSingletonWrapper(className)
148  , singletonPtr_(nullptr)
149  {
150  }
151 
153  {
154  if (singletonPtr_)
155  this->destroy(singletonPtr_);
156  }
157 
159  virtual void activated() override
160  {
161  assert(singletonPtr_ == nullptr);
162  try
163  { singletonPtr_ = new T(); }
164  catch (const InitialisationAbortedException& ex)
165  { orxout(internal_error) << ex.getDescription() << endl; }
166  catch (...)
167  { orxout(internal_error) << "Singleton creation failed: " << Exception::handleMessage() << endl; }
168  }
169 
171  virtual void deactivated() override
172  {
173  if (singletonPtr_ != nullptr)
174  {
175  this->destroy(singletonPtr_);
176  singletonPtr_ = nullptr;
177  }
178  }
179 
182  {
183  singletonPtr_->destroy();
184  }
186  void destroy(void*)
187  {
188  delete singletonPtr_;
189  }
190 
191  private:
193  };
194 }
195 
196 #endif /* __ScopedSingletonWrapper_H__ */
void destroy(void *)
Destroys the singleton instance - overloaded for all other pointers, calls delete.
Definition: ScopedSingletonWrapper.h:124
void destroy(Destroyable *)
Destroys the singleton instance - overloaded for Destroyable, calls Destroyable::destroy() ...
Definition: ScopedSingletonWrapper.h:181
T * singletonPtr_
Unique instance of the singleton class T.
Definition: ScopedSingletonWrapper.h:192
virtual void activated() override
Called if the Scope of the Singleton gets active (creates the instance)
Definition: ScopedSingletonWrapper.h:104
ScopeListeners register themselves in the corresponding Scope and wait for notifications.
Definition: Scope.h:68
Shared library macros, enums, constants and forward declarations for the core library ...
::std::string string
Definition: gtest-port.h:756
virtual void deactivated() override
Called if the Scope of this Singleton gets deactivated (destroys the instance)
Definition: ScopedSingletonWrapper.h:171
static std::string handleMessage()
Retrieves information from an exception caught with "..." Works for std::exception and CEGUI::Excepti...
Definition: Exception.cc:97
const std::string className_
The name of the scoped singleton class that is managed by this object.
Definition: ScopedSingletonWrapper.h:69
The MetaPickup destroys all the PickupCarriers&#39; Pickupables.
ClassScopedSingletonWrapper(const std::string &className)
Definition: ScopedSingletonWrapper.h:146
Output level, used for error messages which are important for developers.
Definition: OutputDefinitions.h:95
ClassScopedSingletonWrapper(const std::string &className)
Definition: ScopedSingletonWrapper.h:91
~ClassScopedSingletonWrapper()
Definition: ScopedSingletonWrapper.h:152
Declaration of facilities to handle exceptions.
T * singletonPtr_
Unique instance of the singleton class T.
Definition: ScopedSingletonWrapper.h:130
void destroy(Destroyable *)
Destroys the singleton instance - overloaded for Destroyable, calls Destroyable::destroy() ...
Definition: ScopedSingletonWrapper.h:119
ScopedSingletonWrapper(const std::string &className)
Constructor: Initializes all the values.
Definition: ScopedSingletonWrapper.h:63
OutputStream & orxout(OutputLevel level=level::debug_output, const OutputContextContainer &context=context::undefined())
This helper function returns a reference to a commonly used instance of OutputStream.
Definition: Output.h:81
Die Wagnis Klasse hat die folgenden Aufgaben:
Definition: ApplicationPaths.cc:66
Base class of ClassScopedSingletonWrapper.
Definition: ScopedSingletonWrapper.h:59
#define _CoreExport
Definition: CorePrereqs.h:61
~ClassScopedSingletonWrapper()
Definition: ScopedSingletonWrapper.h:97
Definition: InputPrereqs.h:78
Declaration of the classes that are needed to use Scopes: orxonox::Scope and orxonox::ScopeListener.
Definition of the Singleton template that is used as base class for classes that allow only one insta...
virtual void deactivated() override
Called if the Scope of this Singleton gets deactivated (destroys the instance)
Definition: ScopedSingletonWrapper.h:111
void destroy(void *)
Destroys the singleton instance - overloaded for void*, calls delete.
Definition: ScopedSingletonWrapper.h:186
Classes must inherit from this class if they should be used with StrongPtr or WeakPtr.
Definition: Destroyable.h:47
Manages a scoped singleton.
Definition: ScopedSingletonWrapper.h:88
virtual void activated() override
Called if the Scope of the Singleton gets active (creates the instance)
Definition: ScopedSingletonWrapper.h:159