CARMA C++
carma::util::CreateWithNewUnmanagedPolicy< TYPE > Class Template Reference

Singleton creation policy which causes instances to be allocated with new (on the heap) but which provides no deletion of memory. More...

#include <carma/util/Singleton.h>

Static Public Member Functions

static TYPE * createInstance ()
 
static void deleteInstance (TYPE *ptr)
 

Detailed Description

template<class TYPE>
class carma::util::CreateWithNewUnmanagedPolicy< TYPE >

Singleton creation policy which causes instances to be allocated with new (on the heap) but which provides no deletion of memory.

With this creation policy, the user is completely responsible for making sure that the instance is destroyed. Here is an example which uses an auto_ptr to delete the instance when the auto_ptr goes out of scope:

* #include <memory>
* #include "carma/util/Program.h"
*
* using namespace carma::util;
*
* class Automanaged :
* public Singleton<Automanaged, CreateWithNewUnmanagedPolicy> {
* public:
* void doStuff() {};
* private:
* friend class CreateWithNewUnmanagedPolicy<Automanaged>;
* friend class std::auto_ptr<Automanaged>;
* Automanaged();
* ~Automanaged() {
* Singleton<Automanaged,
* CreateWithNewUnmanagedPolicy>::destroyInstance();
* };
* };
*
* int Program::main() {
*
* std::auto_ptr<Automanaged> managed(&(Automanaged::instance()));
*
* // Instance of Automanaged will automatically be destroyed when
* // we leave the scope of Program::main for whatever reason.
* }
*

In general this is a pretty dangerous policy simply because it allows for instances to more easily be deleted out from under one's feet. However, if created in the manner above, any access to Automanaged::instance() following destruction of the instance will result in a std::logic_error exception being thrown.

Definition at line 192 of file Singleton.h.


The documentation for this class was generated from the following file: