[svn] * Proper SVN structure
--HG-- branch : trunk
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef MANGOS_CREATIONPOLICY_H
|
||||
#define MANGOS_CREATIONPOLICY_H
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "Platform/Define.h"
|
||||
|
||||
namespace MaNGOS
|
||||
{
|
||||
/**
|
||||
* OperatorNew policy creates an object on the heap using new.
|
||||
*/
|
||||
template <class T>
|
||||
class MANGOS_DLL_DECL OperatorNew
|
||||
{
|
||||
public:
|
||||
static T* Create(void) { return (new T); }
|
||||
static void Destroy(T *obj) { delete obj; }
|
||||
};
|
||||
|
||||
/**
|
||||
* LocalStaticCreation policy creates an object on the stack
|
||||
* the first time call Create.
|
||||
*/
|
||||
template <class T>
|
||||
class MANGOS_DLL_DECL LocalStaticCreation
|
||||
{
|
||||
union MaxAlign
|
||||
{
|
||||
char t_[sizeof(T)];
|
||||
short int shortInt_;
|
||||
int int_;
|
||||
long int longInt_;
|
||||
float float_;
|
||||
double double_;
|
||||
long double longDouble_;
|
||||
struct Test;
|
||||
int Test::* pMember_;
|
||||
int (Test::*pMemberFn_)(int);
|
||||
};
|
||||
public:
|
||||
static T* Create(void)
|
||||
{
|
||||
static MaxAlign si_localStatic;
|
||||
return new(&si_localStatic) T;
|
||||
}
|
||||
|
||||
static void Destroy(T *obj) { obj->~T(); }
|
||||
};
|
||||
|
||||
/**
|
||||
* CreateUsingMalloc by pass the memory manger.
|
||||
*/
|
||||
template<class T>
|
||||
class MANGOS_DLL_DECL CreateUsingMalloc
|
||||
{
|
||||
public:
|
||||
static T* Create()
|
||||
{
|
||||
void* p = ::malloc(sizeof(T));
|
||||
if (!p) return 0;
|
||||
return new(p) T;
|
||||
}
|
||||
|
||||
static void Destroy(T* p)
|
||||
{
|
||||
p->~T();
|
||||
::free(p);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* CreateOnCallBack creates the object base on the call back.
|
||||
*/
|
||||
template<class T, class CALL_BACK>
|
||||
class MANGOS_DLL_DECL CreateOnCallBack
|
||||
{
|
||||
public:
|
||||
static T* Create()
|
||||
{
|
||||
return CALL_BACK::createCallBack();
|
||||
}
|
||||
|
||||
static void Destroy(T *p)
|
||||
{
|
||||
CALL_BACK::destroyCallBack(p);
|
||||
}
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#include <cstdlib>
|
||||
#include "ObjectLifeTime.h"
|
||||
|
||||
namespace MaNGOS
|
||||
{
|
||||
extern "C" void external_wrapper(void *p)
|
||||
{
|
||||
std::atexit( (void (*)())p );
|
||||
}
|
||||
|
||||
void MANGOS_DLL_SPEC at_exit( void (*func)() )
|
||||
{
|
||||
external_wrapper((void*)func);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef MANGOS_OBJECTLIFETIME_H
|
||||
#define MANGOS_OBJECTLIFETIME_H
|
||||
|
||||
#include <stdexcept>
|
||||
#include "Platform/Define.h"
|
||||
|
||||
typedef void (* Destroyer)(void);
|
||||
|
||||
namespace MaNGOS
|
||||
{
|
||||
void MANGOS_DLL_SPEC at_exit( void (*func)() );
|
||||
|
||||
template <class T>
|
||||
class MANGOS_DLL_DECL ObjectLifeTime
|
||||
{
|
||||
public:
|
||||
inline static void ScheduleCall(void (*destroyer)() )
|
||||
{
|
||||
at_exit( destroyer );
|
||||
}
|
||||
|
||||
DECLSPEC_NORETURN static void OnDeadReference(void) ATTR_NORETURN;
|
||||
|
||||
};
|
||||
|
||||
template <class T>
|
||||
inline void ObjectLifeTime<T>::OnDeadReference(void)// We don't handle Dead Reference for now
|
||||
{
|
||||
throw std::runtime_error("Dead Reference");
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef MANGOS_SINGLETON_H
|
||||
#define MANGOS_SINGLETON_H
|
||||
|
||||
/**
|
||||
* @brief class Singleton
|
||||
*/
|
||||
|
||||
#include "CreationPolicy.h"
|
||||
#include "ThreadingModel.h"
|
||||
#include "ObjectLifeTime.h"
|
||||
|
||||
namespace MaNGOS
|
||||
{
|
||||
template
|
||||
<
|
||||
typename T,
|
||||
class ThreadingModel = MaNGOS::SingleThreaded<T>,
|
||||
class CreatePolicy = MaNGOS::OperatorNew<T>,
|
||||
class LifeTimePolicy = MaNGOS::ObjectLifeTime<T>
|
||||
>
|
||||
class MANGOS_DLL_DECL Singleton
|
||||
{
|
||||
public:
|
||||
static T& Instance();
|
||||
|
||||
protected:
|
||||
Singleton() {};
|
||||
|
||||
private:
|
||||
|
||||
// Prohibited actions...this does not prevent hijacking.
|
||||
Singleton(const Singleton &);
|
||||
Singleton& operator=(const Singleton &);
|
||||
|
||||
// Singleton Helpers
|
||||
static void DestroySingleton();
|
||||
|
||||
// data structure
|
||||
typedef typename ThreadingModel::Lock Guard;
|
||||
static T *si_instance;
|
||||
static bool si_destroyed;
|
||||
};
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,90 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef MANGOS_SINGLETONIMPL_H
|
||||
#define MANGOS_SINGLETONIMPL_H
|
||||
|
||||
#include "Singleton.h"
|
||||
|
||||
// avoid the using namespace here cuz
|
||||
// its a .h file afterall
|
||||
|
||||
template
|
||||
<
|
||||
typename T,
|
||||
class ThreadingModel,
|
||||
class CreatePolicy,
|
||||
class LifeTimePolicy
|
||||
>
|
||||
T&
|
||||
MaNGOS::Singleton<T, ThreadingModel, CreatePolicy, LifeTimePolicy >::Instance()
|
||||
{
|
||||
if( !si_instance )
|
||||
{
|
||||
// double-checked Locking pattern
|
||||
Guard();
|
||||
if( !si_instance )
|
||||
{
|
||||
if( si_destroyed )
|
||||
{
|
||||
si_destroyed = false;
|
||||
LifeTimePolicy::OnDeadReference();
|
||||
}
|
||||
si_instance = CreatePolicy::Create();
|
||||
LifeTimePolicy::ScheduleCall(&DestroySingleton);
|
||||
}
|
||||
}
|
||||
|
||||
return *si_instance;
|
||||
}
|
||||
|
||||
template
|
||||
<
|
||||
typename T,
|
||||
class ThreadingModel,
|
||||
class CreatePolicy,
|
||||
class LifeTimePolicy
|
||||
>
|
||||
void
|
||||
MaNGOS::Singleton<T, ThreadingModel, CreatePolicy, LifeTimePolicy>::DestroySingleton()
|
||||
{
|
||||
CreatePolicy::Destroy(si_instance);
|
||||
si_instance = NULL;
|
||||
si_destroyed = true;
|
||||
}
|
||||
|
||||
#define INSTANTIATE_SINGLETON_1(TYPE) \
|
||||
template class MANGOS_DLL_DECL MaNGOS::Singleton<TYPE, MaNGOS::SingleThreaded<TYPE>, MaNGOS::OperatorNew<TYPE>, MaNGOS::ObjectLifeTime<TYPE> >; \
|
||||
template<> TYPE* MaNGOS::Singleton<TYPE, MaNGOS::SingleThreaded<TYPE>, MaNGOS::OperatorNew<TYPE>, MaNGOS::ObjectLifeTime<TYPE> >::si_instance = 0; \
|
||||
template<> bool MaNGOS::Singleton<TYPE, MaNGOS::SingleThreaded<TYPE>, MaNGOS::OperatorNew<TYPE>, MaNGOS::ObjectLifeTime<TYPE> >::si_destroyed = false
|
||||
|
||||
#define INSTANTIATE_SINGLETON_2(TYPE, THREADINGMODEL) \
|
||||
template class MANGOS_DLL_DECL MaNGOS::Singleton<TYPE, THREADINGMODEL, MaNGOS::OperatorNew<TYPE>, MaNGOS::ObjectLifeTime<TYPE> >; \
|
||||
template<> TYPE* MaNGOS::Singleton<TYPE, THREADINGMODEL, MaNGOS::OperatorNew<TYPE>, MaNGOS::ObjectLifeTime<TYPE> >::si_instance = 0; \
|
||||
template<> bool MaNGOS::Singleton<TYPE, THREADINGMODEL, MaNGOS::OperatorNew<TYPE>, MaNGOS::ObjectLifeTime<TYPE> >::si_destroyed = false
|
||||
|
||||
#define INSTANTIATE_SINGLETON_3(TYPE, THREADINGMODEL, CREATIONPOLICY ) \
|
||||
template class MANGOS_DLL_DECL MaNGOS::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, MaNGOS::ObjectLifeTime<TYPE> >; \
|
||||
template<> TYPE* MaNGOS::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, MaNGOS::ObjectLifeTime<TYPE> >::si_instance = 0; \
|
||||
template<> bool MaNGOS::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, MaNGOS::ObjectLifeType<TYPE> >::si_destroyed = false
|
||||
|
||||
#define INSTANTIATE_SINGLETON_4(TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME) \
|
||||
template class MANGOS_DLL_DECL MaNGOS::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >; \
|
||||
template<> TYPE* MaNGOS::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >::si_instance = 0; \
|
||||
template<> bool MaNGOS::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >::si_destroyed = false
|
||||
#endif
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef MANGOS_THREADINGMODEL_H
|
||||
#define MANGOS_THREADINGMODEL_H
|
||||
|
||||
/**
|
||||
* @class ThreadingModel<T>
|
||||
*
|
||||
*/
|
||||
|
||||
#include "Platform/Define.h"
|
||||
|
||||
namespace MaNGOS
|
||||
{
|
||||
inline void Guard(void *) {}
|
||||
|
||||
template<typename MUTEX> class MANGOS_DLL_DECL GeneralLock
|
||||
{
|
||||
public:
|
||||
GeneralLock(MUTEX &m) : i_mutex(m)
|
||||
{
|
||||
i_mutex.acquire();
|
||||
}
|
||||
|
||||
~GeneralLock()
|
||||
{
|
||||
i_mutex.release();
|
||||
}
|
||||
private:
|
||||
GeneralLock(const GeneralLock &);
|
||||
GeneralLock& operator=(const GeneralLock &);
|
||||
MUTEX &i_mutex;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
class MANGOS_DLL_DECL SingleThreaded
|
||||
{
|
||||
public:
|
||||
|
||||
struct Lock // empty object
|
||||
{
|
||||
Lock() {}
|
||||
Lock(const T &) {}
|
||||
Lock(const SingleThreaded<T> &) // for single threaded we ignore this
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
typedef T VolatileType;
|
||||
};
|
||||
|
||||
// object level lockable
|
||||
template<class T, class MUTEX>
|
||||
class MANGOS_DLL_DECL ObjectLevelLockable
|
||||
{
|
||||
public:
|
||||
ObjectLevelLockable() : i_mtx() {}
|
||||
|
||||
friend class Lock;
|
||||
|
||||
class Lock
|
||||
{
|
||||
public:
|
||||
Lock(ObjectLevelLockable<T, MUTEX> &host) : i_lock(host.i_mtx)
|
||||
{
|
||||
}
|
||||
|
||||
private:
|
||||
GeneralLock<MUTEX> i_lock;
|
||||
};
|
||||
|
||||
typedef volatile T VolatileType;
|
||||
|
||||
private:
|
||||
// prevent the compiler creating a copy construct
|
||||
ObjectLevelLockable(const ObjectLevelLockable<T, MUTEX> &);
|
||||
ObjectLevelLockable<T, MUTEX>& operator=(const ObjectLevelLockable<T, MUTEX> &);
|
||||
|
||||
MUTEX i_mtx;
|
||||
};
|
||||
|
||||
template<class T, class MUTEX>
|
||||
class MANGOS_DLL_DECL ClassLevelLockable
|
||||
{
|
||||
public:
|
||||
class Lock;
|
||||
friend class Lock;
|
||||
typedef volatile T VolatileType;
|
||||
|
||||
ClassLevelLockable() {}
|
||||
|
||||
class Lock
|
||||
{
|
||||
public:
|
||||
Lock(T& /*host*/) { ClassLevelLockable<T, MUTEX>::si_mtx.acquire(); }
|
||||
Lock(ClassLevelLockable<T, MUTEX> &) { ClassLevelLockable<T, MUTEX>::si_mtx.acquire(); }
|
||||
Lock() { ClassLevelLockable<T, MUTEX>::si_mtx.acquire(); }
|
||||
~Lock() { ClassLevelLockable<T, MUTEX>::si_mtx.release(); }
|
||||
};
|
||||
|
||||
private:
|
||||
static MUTEX si_mtx;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
template<class T, class MUTEX> MUTEX MaNGOS::ClassLevelLockable<T, MUTEX>::si_mtx;
|
||||
|
||||
#define INSTANTIATE_CLASS_MUTEX(CTYPE,MUTEX) \
|
||||
template class MANGOS_DLL_DECL MaNGOS::ClassLevelLockable<CTYPE, MUTEX >
|
||||
#endif
|
||||
Reference in New Issue
Block a user