libbio
Loading...
Searching...
No Matches
Namespaces | Macros | Typedefs | Functions
SingletonMacros.h File Reference
#include <cstddef>
#include <new>
#include "bio/common/thread/ThreadSafe.h"
Include dependency graph for SingletonMacros.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Namespaces

namespace  bio
 
namespace  bio::pointer_arena
 

Macros

#define BIO_SINGLETON_CREATE_INSTANCE(className)    return new className();
 
#define BIO_SINGLETON(className, baseClass)
 

Typedefs

typedef void *(* bio::SingletonCreator) ()
 

Functions

voidbio::pointer_arena::Allocate (::std::size_t bytes, ::std::size_t align)
 
voidbio::GetOrCreateSingleton (const char *key, SingletonCreator creator)
 

Macro Definition Documentation

◆ BIO_SINGLETON

#define BIO_SINGLETON (   className,
  baseClass 
)
Value:
class className : \
public baseClass, \
virtual public ::bio::ThreadSafe \
{ \
public: \
static className& Instance() \
{ \
return *static_cast<className*>( \
::bio::GetOrCreateSingleton( \
#className, \
&className::CreateInstance)); \
} \
private: \
static void* CreateInstance() \
{ \
BIO_SINGLETON_CREATE_INSTANCE(className) \
} \
className() \
{ \
} \
className(className const &); \
void operator=(className const &); \
};
Definition ThreadSafe.h:57
ThreadSafe & operator=(const ThreadSafe &toCopy)
Definition ThreadSafe.cpp:220

Singleton interface makes the constructor private so that there is only one instance ever created, which is by Instance()
we also override default copy constructor and assignment operator so that nobody can make a copy of the singleton (otherwise it wouldn't be a singleton). We don't define them, so these methods will give a link error if used.

Parameters
classNamethe name of the new singleton
baseClassthe name of the class the new singleton will inherit from (i.e. what to make into a singleton).

◆ BIO_SINGLETON_CREATE_INSTANCE

#define BIO_SINGLETON_CREATE_INSTANCE (   className)     return new className();