flatten 20260225
This commit is contained in:
43
tjp/core/singleton/Singleton.cpp
Executable file
43
tjp/core/singleton/Singleton.cpp
Executable file
@@ -0,0 +1,43 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
/**
|
||||
* legal header
|
||||
*
|
||||
* ============================================================================
|
||||
* TJP 10/11/01 Got rid of the constructed singleton, it was essentially
|
||||
* a pointer!
|
||||
*
|
||||
* @author Timothy Prepscius
|
||||
* @created 3/../01
|
||||
*/
|
||||
|
||||
#include "Singleton.hpp"
|
||||
|
||||
namespace timprepscius {
|
||||
namespace core {
|
||||
|
||||
Singletons::Singletons_ &Singletons::singletons()
|
||||
{
|
||||
static Singletons_ *v = new Singletons_();
|
||||
return *v;
|
||||
}
|
||||
|
||||
void Singletons::clearAll ()
|
||||
{
|
||||
for (auto *singleton : singletons())
|
||||
singleton->clear();
|
||||
}
|
||||
|
||||
Singleton_::Singleton_ ()
|
||||
{
|
||||
Singletons::singletons().insert(this);
|
||||
}
|
||||
|
||||
Singleton_::~Singleton_ ()
|
||||
{
|
||||
Singletons::singletons().erase(this);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
36
tjp/core/singleton/Singleton.h
Executable file
36
tjp/core/singleton/Singleton.h
Executable file
@@ -0,0 +1,36 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
/**
|
||||
* legal header
|
||||
*
|
||||
* ============================================================================
|
||||
* TJP 10/11/01 Got rid of the constructed singleton, it was essentially
|
||||
* a pointer!
|
||||
*
|
||||
* @author Timothy Prepscius
|
||||
* @created 3/../01
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace timprepscius {
|
||||
namespace core {
|
||||
|
||||
/**
|
||||
* a Singleton container, whose contained instance is constructed on its
|
||||
* first request
|
||||
*
|
||||
* T defines the class pointer type
|
||||
* X defines the allocation class type
|
||||
*/
|
||||
class Singletons;
|
||||
|
||||
|
||||
class Singleton_;
|
||||
|
||||
template<class T, class X=T>
|
||||
class Singleton;
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
125
tjp/core/singleton/Singleton.hpp
Executable file
125
tjp/core/singleton/Singleton.hpp
Executable file
@@ -0,0 +1,125 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
/**
|
||||
* legal header
|
||||
*
|
||||
* ============================================================================
|
||||
* TJP 10/11/01 Got rid of the constructed singleton, it was essentially
|
||||
* a pointer!
|
||||
*
|
||||
* @author Timothy Prepscius
|
||||
* @created 3/../01
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "Singleton.h"
|
||||
|
||||
#include "../containers/FilledArray.hpp"
|
||||
|
||||
namespace timprepscius {
|
||||
namespace core {
|
||||
|
||||
/**
|
||||
* a Singleton container, whose contained instance is constructed on its
|
||||
* first request
|
||||
*
|
||||
* T defines the class pointer type
|
||||
* X defines the allocation class type
|
||||
*/
|
||||
class Singletons
|
||||
{
|
||||
protected:
|
||||
friend class Singleton_;
|
||||
typedef FilledArray<class Singleton_ *> Singletons_;
|
||||
static Singletons_ &singletons();
|
||||
|
||||
public:
|
||||
static void clearAll ();
|
||||
} ;
|
||||
|
||||
class Singleton_
|
||||
{
|
||||
protected:
|
||||
friend class Singletons;
|
||||
virtual void clear () {}
|
||||
|
||||
public:
|
||||
Singleton_ ();
|
||||
virtual ~Singleton_ ();
|
||||
} ;
|
||||
|
||||
template<class T, class X>
|
||||
class Singleton : Singleton_
|
||||
{
|
||||
private:
|
||||
T *instance;
|
||||
|
||||
void clear () override
|
||||
{
|
||||
deleteInstance();
|
||||
}
|
||||
|
||||
public:
|
||||
Singleton()
|
||||
{
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
virtual ~Singleton()
|
||||
{
|
||||
deleteInstance();
|
||||
}
|
||||
|
||||
void setInstance (T *instance)
|
||||
{
|
||||
if (this->instance)
|
||||
delete instance;
|
||||
|
||||
this->instance = instance;
|
||||
}
|
||||
|
||||
void verifyInstance()
|
||||
{
|
||||
if (instance==nullptr)
|
||||
{
|
||||
instance = new X();
|
||||
}
|
||||
}
|
||||
|
||||
void deleteInstance()
|
||||
{
|
||||
delete instance;
|
||||
instance = nullptr;
|
||||
}
|
||||
|
||||
T *p()
|
||||
{
|
||||
verifyInstance();
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**********************************************************************
|
||||
* operator overloads to simplify use
|
||||
**********************************************************************/
|
||||
|
||||
template<typename U>
|
||||
operator U *()
|
||||
{
|
||||
return p();
|
||||
}
|
||||
|
||||
T *operator ->()
|
||||
{
|
||||
return p();
|
||||
}
|
||||
|
||||
T &operator *()
|
||||
{
|
||||
return *p();
|
||||
}
|
||||
} ;
|
||||
|
||||
} // namespace
|
||||
} // namespace
|
||||
|
||||
27
tjp/core/singleton/ThreadSafeSingleton.h
Executable file
27
tjp/core/singleton/ThreadSafeSingleton.h
Executable file
@@ -0,0 +1,27 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
/**
|
||||
* legal header
|
||||
*
|
||||
* ============================================================================
|
||||
* TJP 10/11/01 Got rid of the constructed singleton, it was essentially
|
||||
* a pointer!
|
||||
*
|
||||
* @author Timothy Prepscius
|
||||
* @created 3/../01
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace timprepscius {
|
||||
namespace core {
|
||||
|
||||
template<class T>
|
||||
class ThreadSafeSingletonNoConstruct;
|
||||
|
||||
template<class T, class X=T>
|
||||
class ThreadSafeSingleton;
|
||||
|
||||
} // namespace utilities
|
||||
} // namespace
|
||||
|
||||
112
tjp/core/singleton/ThreadSafeSingleton.hpp
Executable file
112
tjp/core/singleton/ThreadSafeSingleton.hpp
Executable file
@@ -0,0 +1,112 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
/**
|
||||
* legal header
|
||||
*
|
||||
* ============================================================================
|
||||
* TJP 10/11/01 Got rid of the constructed singleton, it was essentially
|
||||
* a pointer!
|
||||
*
|
||||
* @author Timothy Prepscius
|
||||
* @created 3/../01
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ThreadSafeSingleton.h"
|
||||
#include "../threads/Monitor.h"
|
||||
|
||||
namespace timprepscius {
|
||||
namespace core {
|
||||
|
||||
/**
|
||||
* a Singleton container, whose contained instance is constructed on its
|
||||
* first request
|
||||
*
|
||||
* T defines the class pointer type
|
||||
* X defines the allocation class type
|
||||
*/
|
||||
|
||||
template<class T>
|
||||
class ThreadSafeSingletonNoConstruct : public Monitor<T>
|
||||
{
|
||||
public:
|
||||
typedef Monitor<T> Super;
|
||||
typedef typename Super::Writer Accessor;
|
||||
|
||||
protected:
|
||||
// only called by reader/writer after lock
|
||||
virtual T *access() const
|
||||
{
|
||||
return this->object;
|
||||
}
|
||||
|
||||
public:
|
||||
ThreadSafeSingletonNoConstruct () :
|
||||
Super(NULL)
|
||||
{
|
||||
}
|
||||
|
||||
virtual ~ThreadSafeSingletonNoConstruct()
|
||||
{
|
||||
deleteInstance();
|
||||
}
|
||||
|
||||
virtual void setInstance (T *instance)
|
||||
{
|
||||
Accessor w(*this);
|
||||
{
|
||||
this->object = instance;
|
||||
}
|
||||
}
|
||||
|
||||
void deleteInstance()
|
||||
{
|
||||
Accessor w(*this);
|
||||
|
||||
if (this->object)
|
||||
delete this->object;
|
||||
|
||||
this->object = NULL;
|
||||
}
|
||||
} ;
|
||||
|
||||
template<class T, class X=T>
|
||||
class ThreadSafeSingleton : public ThreadSafeSingletonNoConstruct<T>
|
||||
{
|
||||
public:
|
||||
typedef ThreadSafeSingletonNoConstruct<T> Super;
|
||||
typedef typename Super::Writer Accessor;
|
||||
|
||||
protected:
|
||||
// only called by reader/writer after lock
|
||||
virtual T *access() const
|
||||
{
|
||||
if (!this->object)
|
||||
{
|
||||
const_cast<ThreadSafeSingleton<T> *>(this)->setInstance(new T());
|
||||
}
|
||||
|
||||
return this->object;
|
||||
}
|
||||
|
||||
public:
|
||||
virtual ~ThreadSafeSingleton()
|
||||
{
|
||||
Super::deleteInstance();
|
||||
}
|
||||
|
||||
virtual void setInstance (T *instance)
|
||||
{
|
||||
Accessor a(*this);
|
||||
|
||||
if (this->object)
|
||||
delete this->object;
|
||||
|
||||
this->object = instance;
|
||||
}
|
||||
} ;
|
||||
|
||||
} // namespace utilities
|
||||
} // namespace
|
||||
|
||||
41
tjp/core/singleton/ThreadSpecificSingleton.h
Executable file
41
tjp/core/singleton/ThreadSpecificSingleton.h
Executable file
@@ -0,0 +1,41 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
/**
|
||||
* legal header
|
||||
*
|
||||
* ============================================================================
|
||||
* TJP 10/11/01 Got rid of the constructed singleton, it was essentially
|
||||
* a pointer!
|
||||
*
|
||||
* @author Timothy Prepscius
|
||||
* @created 3/../01
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
namespace timprepscius {
|
||||
namespace core {
|
||||
|
||||
/**
|
||||
* a Singleton container, whose contained instance is constructed on its
|
||||
* first request
|
||||
*
|
||||
* T defines the class pointer type
|
||||
* X defines the allocation class type
|
||||
*/
|
||||
template<class T, class X=T>
|
||||
class ThreadSpecificSingletonNoConstruct;
|
||||
|
||||
/**
|
||||
* a Singleton container, whose contained instance is constructed on its
|
||||
* first request
|
||||
*
|
||||
* T defines the class pointer type
|
||||
* X defines the allocation class type
|
||||
*/
|
||||
template<class T, class X=T>
|
||||
class ThreadSpecificSingleton;
|
||||
|
||||
} // namespace utilities
|
||||
} // namespace
|
||||
|
||||
132
tjp/core/singleton/ThreadSpecificSingleton.hpp
Executable file
132
tjp/core/singleton/ThreadSpecificSingleton.hpp
Executable file
@@ -0,0 +1,132 @@
|
||||
// TJP COPYRIGHT HEADER
|
||||
|
||||
/**
|
||||
* legal header
|
||||
*
|
||||
* ============================================================================
|
||||
* TJP 10/11/01 Got rid of the constructed singleton, it was essentially
|
||||
* a pointer!
|
||||
*
|
||||
* @author Timothy Prepscius
|
||||
* @created 3/../01
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ThreadSpecificSingleton.h"
|
||||
|
||||
#include "../system/DisableWarningsPush.h"
|
||||
#include <boost/thread.hpp>
|
||||
#include "../system/DisableWarningsPop.h"
|
||||
|
||||
namespace timprepscius {
|
||||
namespace core {
|
||||
|
||||
/**
|
||||
* a Singleton container, whose contained instance is constructed on its
|
||||
* first request
|
||||
*
|
||||
* T defines the class pointer type
|
||||
* X defines the allocation class type
|
||||
*/
|
||||
template<class T, class X>
|
||||
class ThreadSpecificSingletonNoConstruct
|
||||
{
|
||||
private:
|
||||
boost::thread_specific_ptr<T> instance;
|
||||
|
||||
static void cleanup_function (T *p) { }
|
||||
|
||||
public:
|
||||
ThreadSpecificSingletonNoConstruct () :
|
||||
instance(&cleanup_function) { }
|
||||
|
||||
virtual ~ThreadSpecificSingletonNoConstruct () {}
|
||||
|
||||
virtual void setInstance (T *instance)
|
||||
{
|
||||
this->instance.release();
|
||||
this->instance.reset(instance);
|
||||
}
|
||||
|
||||
void deleteInstance()
|
||||
{
|
||||
delete this->instance.release();
|
||||
}
|
||||
|
||||
virtual T *p()
|
||||
{
|
||||
return instance.get();
|
||||
}
|
||||
|
||||
/**********************************************************************
|
||||
* operator overloads to simplify use
|
||||
**********************************************************************/
|
||||
|
||||
operator T *()
|
||||
{
|
||||
return p();
|
||||
}
|
||||
|
||||
T *operator ->()
|
||||
{
|
||||
return p();
|
||||
}
|
||||
|
||||
T &operator *()
|
||||
{
|
||||
return *p();
|
||||
}
|
||||
} ;
|
||||
|
||||
/**
|
||||
* a Singleton container, whose contained instance is constructed on its
|
||||
* first request
|
||||
*
|
||||
* T defines the class pointer type
|
||||
* X defines the allocation class type
|
||||
*/
|
||||
template<class T, class X>
|
||||
class ThreadSpecificSingleton : public ThreadSpecificSingletonNoConstruct<T>
|
||||
{
|
||||
protected:
|
||||
typedef ThreadSpecificSingletonNoConstruct<T> Super;
|
||||
public:
|
||||
virtual void setInstance (T *instance)
|
||||
{
|
||||
Super::deleteInstance();
|
||||
Super::setInstance(instance);
|
||||
}
|
||||
|
||||
T *p()
|
||||
{
|
||||
T *p = Super::p();
|
||||
if (!p)
|
||||
Super::setInstance(p = new X());
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
/**********************************************************************
|
||||
* operator overloads to simplify use
|
||||
**********************************************************************/
|
||||
|
||||
operator T *()
|
||||
{
|
||||
return p();
|
||||
}
|
||||
|
||||
T *operator ->()
|
||||
{
|
||||
return p();
|
||||
}
|
||||
|
||||
T &operator *()
|
||||
{
|
||||
return *p();
|
||||
}
|
||||
} ;
|
||||
|
||||
} // namespace utilities
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user