* Get rid of framework and move the files to game and shared.

--HG--
branch : trunk
This commit is contained in:
XTZGZoReX
2010-06-06 23:44:21 +02:00
parent 1170c67a70
commit 22950963e9
38 changed files with 0 additions and 11 deletions
+64
View File
@@ -0,0 +1,64 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_COMPILERDEFS_H
#define TRINITY_COMPILERDEFS_H
#define PLATFORM_WINDOWS 0
#define PLATFORM_UNIX 1
#define PLATFORM_APPLE 2
#define PLATFORM_INTEL 3
// must be first (win 64 also define WIN32)
#if defined( _WIN64 )
# define PLATFORM PLATFORM_WINDOWS
#elif defined( __WIN32__ ) || defined( WIN32 ) || defined( _WIN32 )
# define PLATFORM PLATFORM_WINDOWS
#elif defined( __APPLE_CC__ )
# define PLATFORM PLATFORM_APPLE
#elif defined( __INTEL_COMPILER )
# define PLATFORM PLATFORM_INTEL
#else
# define PLATFORM PLATFORM_UNIX
#endif
#define COMPILER_MICROSOFT 0
#define COMPILER_GNU 1
#define COMPILER_BORLAND 2
#define COMPILER_INTEL 3
#ifdef _MSC_VER
# define COMPILER COMPILER_MICROSOFT
#elif defined( __BORLANDC__ )
# define COMPILER COMPILER_BORLAND
#elif defined( __INTEL_COMPILER )
# define COMPILER COMPILER_INTEL
#elif defined( __GNUC__ )
# define COMPILER COMPILER_GNU
#else
# pragma error "FATAL ERROR: Unknown compiler."
#endif
#if COMPILER == COMPILER_MICROSOFT
# pragma warning( disable : 4267 ) // conversion from 'size_t' to 'int', possible loss of data
# pragma warning( disable : 4786 ) // identifier was truncated to '255' characters in the debug information
#endif
#endif
+85
View File
@@ -0,0 +1,85 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_DEFINE_H
#define TRINITY_DEFINE_H
#include <sys/types.h>
#include <ace/Basic_Types.h>
#include <ace/ACE_export.h>
#include "Platform/CompilerDefs.h"
#define TRINITY_LITTLEENDIAN 0
#define TRINITY_BIGENDIAN 1
#if !defined(TRINITY_ENDIAN)
# if defined (ACE_BIG_ENDIAN)
# define TRINITY_ENDIAN TRINITY_BIGENDIAN
# else //ACE_BYTE_ORDER != ACE_BIG_ENDIAN
# define TRINITY_ENDIAN TRINITY_LITTLEENDIAN
# endif //ACE_BYTE_ORDER
#endif //TRINITY_ENDIAN
#if PLATFORM == PLATFORM_WINDOWS
# define TRINITY_PATH_MAX MAX_PATH
# ifndef DECLSPEC_NORETURN
# define DECLSPEC_NORETURN __declspec(noreturn)
# endif //DECLSPEC_NORETURN
#else //PLATFORM != PLATFORM_WINDOWS
# define TRINITY_PATH_MAX PATH_MAX
# define DECLSPEC_NORETURN
#endif //PLATFORM
#if !defined(DEBUG)
# define TRINITY_INLINE inline
#else //DEBUG
# if !defined(TRINITY_DEBUG)
# define TRINITY_DEBUG
# endif //TRINITY_DEBUG
# define TRINITY_INLINE
#endif //!DEBUG
#if COMPILER == COMPILER_GNU
# define ATTR_NORETURN __attribute__((noreturn))
# define ATTR_PRINTF(F,V) __attribute__ ((format (printf, F, V)))
#else //COMPILER != COMPILER_GNU
# define ATTR_NORETURN
# define ATTR_PRINTF(F,V)
#endif //COMPILER == COMPILER_GNU
typedef ACE_INT64 int64;
typedef ACE_INT32 int32;
typedef ACE_INT16 int16;
typedef ACE_INT8 int8;
typedef ACE_UINT64 uint64;
typedef ACE_UINT32 uint32;
typedef ACE_UINT16 uint16;
typedef ACE_UINT8 uint8;
#if COMPILER != COMPILER_MICROSOFT
typedef uint16 WORD;
typedef uint32 DWORD;
#endif //COMPILER
typedef uint64 OBJECT_HANDLE;
#endif //TRINITY_DEFINE_H
@@ -0,0 +1,100 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_REFERENCE_H
#define TRINITY_REFERENCE_H
/**
* Referencer<T>
* Referencer is an object that holds a reference holder that hold a reference
* counted object. When an object's reference count drop to zero, it removes
* the object. This is a non intrusive mechanism and any object at any point
* in time can be referenced. When and object is reference counted, do not
* pass the object directly to other methods but rather, pass its
* reference around. Objects can be reference counted in both single threaded
* model and multi-threaded model
*/
#include <stdexcept>
#include "Platform/Define.h"
#include "Policies/ThreadingModel.h"
#include "ReferenceHolder.h"
template
<
typename T,
class THREADING_MODEL = Trinity::SingleThreaded<T>
>
class Referencer
{
typedef typename THREADING_MODEL::Lock Lock;
typedef ReferenceHolder<T, THREADING_MODEL> ReferenceeHolder;
public:
/// Constructs a referencer.
Referencer(T *ref = NULL);
/// Copy constructor
Referencer(const Referencer &obj) : i_holder(NULL) { *this = obj; }
/// Destructor
~Referencer();
/// Referencee accessor
T* referencee(void) { return (i_holder == NULL ? NULL : i_holder->i_referencee); }
const T* referencee(void) const { return (i_holder == NULL ? NULL : i_holder->i_referencee); }
//T& referencee(void){ return _referencee(); }
//const T& referencee(void) const { return const_cast<Referencer *>(this)->_referencee(); }
operator T&(void) { return _referencee(); }
operator const T&(void) const { return *const_cast<Referencer *>(this)->_referencee(); }
/// cast operators
T* operator*() { return (i_holder == NULL ? NULL : i_holder->i_referencee); }
T const * operator*() const { return (i_holder == NULL ? NULL : i_holder->i_referencee); }
/// overload operators
T* operator->() { return (i_holder == NULL ? NULL : i_holder->i_referencee); }
const T * operator->() const { return (i_holder == NULL ? NULL : i_holder->i_referencee); }
/// operator =
Referencer& operator=(const Referencer &obj);
Referencer& operator=(T *);
/// returns true if i_referencee is null
bool isNull(void) const { return i_holder == NULL; }
private:
T& _referencee(void)
{
if( i_holder == NULL )
throw std::runtime_error("Invalid access to null pointer");
return *i_holder->i_referencee;
}
void deReference(ReferenceeHolder *);
void addReference(ReferenceeHolder *);
// private data
ReferenceeHolder *i_holder;
};
#endif
@@ -0,0 +1,42 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_REFERENCEHOLDER_H
#define TRINITY_REFERENCEHOLDER_H
/** ReferenceHolder holds the actualy referenced obejct as well the refence
count. The ReferenecHolder implements as a policy base object and
will decided by the Reference class to be consnsitent.
*/
template
<
typename T,
class THREADING_MODEL
>
struct ReferenceHolder : public THREADING_MODEL
{
explicit ReferenceHolder(T *ref) : i_referencee(ref), i_referenceCount(0) {}
T *i_referencee;
unsigned int i_referenceCount;
typedef typename THREADING_MODEL::Lock Lock;
};
#endif
@@ -0,0 +1,133 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_REFERENCEIMPL_H
#define TRINITY_REFERENCEIMPL_H
#include "Reference.h"
template
<
typename T,
class THREADING_MODEL
>
Referencer<T, THREADING_MODEL>::Referencer(T *ref)
: i_holder(NULL)
{
if( ref != NULL )
{
i_holder = new ReferenceeHolder(ref);
++i_holder->i_referenceCount;
}
}
template
<
typename T,
class THREADING_MODEL
>
Referencer<T, THREADING_MODEL>::~Referencer()
{
if( i_holder != NULL )
deReference(i_holder);
i_holder = NULL;
}
template
<
typename T,
class THREADING_MODEL
>
Referencer<T, THREADING_MODEL>&
Referencer<T, THREADING_MODEL>::operator=(const Referencer<T, THREADING_MODEL> &obj)
{
if( i_holder != NULL )
deReference(i_holder);
if( obj.i_holder != NULL )
addReference(obj.i_holder);
i_holder = obj.i_holder;
return *this;
}
template
<
typename T,
class THREADING_MODEL
>
Referencer<T, THREADING_MODEL>&
Referencer<T, THREADING_MODEL>::operator=(T *ref)
{
if( i_holder != NULL )
deReference(i_holder);
i_holder = NULL;
if( ref != NULL )
{
i_holder = new ReferenceeHolder(ref);
++i_holder->i_referenceCount;
}
return *this;
}
template
<
typename T,
class THREADING_MODEL
>
void
Referencer<T, THREADING_MODEL>::deReference(ReferenceHolder<T, THREADING_MODEL> *holder)
{
assert( holder != NULL && holder->i_referenceCount > 0);
bool delete_object = false;
{
// The guard is within the scope due to the guard
// must release earlier than expected.
Lock guard(*holder);
Guard(&guard);
--holder->i_referenceCount;
if( holder->i_referenceCount == 0 )
delete_object = true;
}
if( delete_object )
{
delete holder->i_referencee;
delete holder;
}
}
template
<
typename T,
class THREADING_MODEL
>
void
Referencer<T, THREADING_MODEL>::addReference(ReferenceHolder<T, THREADING_MODEL> *holder)
{
assert( i_holder != NULL );
Lock guard(*holder);
Guard(&guard);
++holder->i_referenceCount;
}
#endif
+63
View File
@@ -0,0 +1,63 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_FACTORY_HOLDER
#define TRINITY_FACTORY_HOLDER
#include "Platform/Define.h"
#include "Utilities/TypeList.h"
#include "ObjectRegistry.h"
#include "Policies/SingletonImp.h"
/** FactoryHolder holds a factory object of a specific type
*/
template<class T, class Key = std::string>
class FactoryHolder
{
public:
typedef ObjectRegistry<FactoryHolder<T, Key >, Key > FactoryHolderRegistry;
typedef Trinity::Singleton<FactoryHolderRegistry > FactoryHolderRepository;
FactoryHolder(Key k) : i_key(k) {}
virtual ~FactoryHolder() {}
inline Key key() const { return i_key; }
void RegisterSelf(void) { FactoryHolderRepository::Instance().InsertItem(this, i_key); }
void DeregisterSelf(void) { FactoryHolderRepository::Instance().RemoveItem(this, false); }
/// Abstract Factory create method
virtual T* Create(void *data = NULL) const = 0;
private:
Key i_key;
};
/** Permissible is a classic way of letting the object decide
* whether how good they handle things. This is not retricted
* to factory selectors.
*/
template<class T>
class Permissible
{
public:
virtual ~Permissible() {}
virtual int Permit(const T *) const = 0;
};
#endif
+247
View File
@@ -0,0 +1,247 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 _LINKEDLIST
#define _LINKEDLIST
#include "Common.h"
//============================================
class LinkedListHead;
class LinkedListElement
{
private:
friend class LinkedListHead;
LinkedListElement* iNext;
LinkedListElement* iPrev;
public:
LinkedListElement() { iNext = NULL; iPrev = NULL; }
~LinkedListElement() { delink(); }
bool hasNext() const { return(iNext && iNext->iNext != NULL); }
bool hasPrev() const { return(iPrev && iPrev->iPrev != NULL); }
bool isInList() const { return(iNext != NULL && iPrev != NULL); }
LinkedListElement * next() { return hasNext() ? iNext : NULL; }
LinkedListElement const* next() const { return hasNext() ? iNext : NULL; }
LinkedListElement * prev() { return hasPrev() ? iPrev : NULL; }
LinkedListElement const* prev() const { return hasPrev() ? iPrev : NULL; }
LinkedListElement * nocheck_next() { return iNext; }
LinkedListElement const* nocheck_next() const { return iNext; }
LinkedListElement * nocheck_prev() { return iPrev; }
LinkedListElement const* nocheck_prev() const { return iPrev; }
void delink()
{
if(isInList())
{
iNext->iPrev = iPrev; iPrev->iNext = iNext; iNext = NULL; iPrev = NULL;
}
}
void insertBefore(LinkedListElement* pElem)
{
pElem->iNext = this;
pElem->iPrev = iPrev;
iPrev->iNext = pElem;
iPrev = pElem;
}
void insertAfter(LinkedListElement* pElem)
{
pElem->iPrev = this;
pElem->iNext = iNext;
iNext->iPrev = pElem;
iNext = pElem;
}
};
//============================================
class LinkedListHead
{
private:
LinkedListElement iFirst;
LinkedListElement iLast;
uint32 iSize;
public:
LinkedListHead()
{
// create empty list
iFirst.iNext = &iLast;
iLast.iPrev = &iFirst;
iSize = 0;
}
bool isEmpty() const { return(!iFirst.iNext->isInList()); }
LinkedListElement * getFirst() { return(isEmpty() ? NULL : iFirst.iNext); }
LinkedListElement const* getFirst() const { return(isEmpty() ? NULL : iFirst.iNext); }
LinkedListElement * getLast() { return(isEmpty() ? NULL : iLast.iPrev); }
LinkedListElement const* getLast() const { return(isEmpty() ? NULL : iLast.iPrev); }
void insertFirst(LinkedListElement* pElem)
{
iFirst.insertAfter(pElem);
}
void insertLast(LinkedListElement* pElem)
{
iLast.insertBefore(pElem);
}
uint32 getSize() const
{
if(!iSize)
{
uint32 result = 0;
LinkedListElement const* e = getFirst();
while(e)
{
++result;
e = e->next();
}
return result;
}
else
return iSize;
}
void incSize() { ++iSize; }
void decSize() { --iSize; }
template<class _Ty>
class Iterator
{
public:
typedef std::bidirectional_iterator_tag iterator_category;
typedef _Ty value_type;
typedef ptrdiff_t difference_type;
typedef ptrdiff_t distance_type;
typedef _Ty* pointer;
typedef _Ty const* const_pointer;
typedef _Ty& reference;
typedef _Ty const & const_reference;
Iterator() : _Ptr(0)
{ // construct with null node pointer
}
Iterator(pointer _Pnode) : _Ptr(_Pnode)
{ // construct with node pointer _Pnode
}
Iterator& operator=(Iterator const &_Right)
{
return (*this) = _Right._Ptr;
}
Iterator& operator=(const_pointer const &_Right)
{
_Ptr = (pointer)_Right;
return (*this);
}
reference operator*()
{ // return designated value
return *_Ptr;
}
pointer operator->()
{ // return pointer to class object
return _Ptr;
}
Iterator& operator++()
{ // preincrement
_Ptr = _Ptr->next();
return (*this);
}
Iterator operator++(int)
{ // postincrement
iterator _Tmp = *this;
++*this;
return (_Tmp);
}
Iterator& operator--()
{ // predecrement
_Ptr = _Ptr->prev();
return (*this);
}
Iterator operator--(int)
{ // postdecrement
iterator _Tmp = *this;
--*this;
return (_Tmp);
}
bool operator==(Iterator const &_Right) const
{ // test for iterator equality
return (_Ptr == _Right._Ptr);
}
bool operator!=(Iterator const &_Right) const
{ // test for iterator inequality
return (!(*this == _Right));
}
bool operator==(pointer const &_Right) const
{ // test for pointer equality
return (_Ptr != _Right);
}
bool operator!=(pointer const &_Right) const
{ // test for pointer equality
return (!(*this == _Right));
}
bool operator==(const_reference _Right) const
{ // test for reference equality
return (_Ptr == &_Right);
}
bool operator!=(const_reference _Right) const
{ // test for reference equality
return (_Ptr != &_Right);
}
pointer _Mynode()
{ // return node pointer
return (_Ptr);
}
protected:
pointer _Ptr; // pointer to node
};
typedef Iterator<LinkedListElement> iterator;
};
//============================================
#endif
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 _REFMANAGER_H
#define _REFMANAGER_H
//=====================================================
#include "Utilities/LinkedList.h"
#include "Utilities/LinkedReference/Reference.h"
template <class TO, class FROM> class RefManager : public LinkedListHead
{
public:
typedef LinkedListHead::Iterator< Reference<TO, FROM> > iterator;
RefManager() { }
virtual ~RefManager() { clearReferences(); }
Reference<TO, FROM>* getFirst() { return ((Reference<TO, FROM>*) LinkedListHead::getFirst()); }
Reference<TO, FROM> const* getFirst() const { return ((Reference<TO, FROM> const*) LinkedListHead::getFirst()); }
Reference<TO, FROM>* getLast() { return ((Reference<TO, FROM>*) LinkedListHead::getLast()); }
Reference<TO, FROM> const* getLast() const { return ((Reference<TO, FROM> const*) LinkedListHead::getLast()); }
iterator begin() { return iterator(getFirst()); }
iterator end() { return iterator(NULL); }
iterator rbegin() { return iterator(getLast()); }
iterator rend() { return iterator(NULL); }
void clearReferences()
{
LinkedListElement* ref;
while((ref = getFirst()) != NULL)
{
((Reference<TO, FROM>*) ref)->invalidate();
ref->delink(); // the delink might be already done by invalidate(), but doing it here again does not hurt and insures an empty list
}
}
};
//=====================================================
#endif
@@ -0,0 +1,94 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 _REFERENCE_H
#define _REFERENCE_H
#include "Utilities/LinkedList.h"
//=====================================================
template <class TO, class FROM> class Reference : public LinkedListElement
{
private:
TO* iRefTo;
FROM* iRefFrom;
protected:
// Tell our refTo (target) object that we have a link
virtual void targetObjectBuildLink() = 0;
// Tell our refTo (taget) object, that the link is cut
virtual void targetObjectDestroyLink() = 0;
// Tell our refFrom (source) object, that the link is cut (Target destroyed)
virtual void sourceObjectDestroyLink() = 0;
public:
Reference() { iRefTo = NULL; iRefFrom = NULL; }
virtual ~Reference() {}
// Create new link
void link(TO* toObj, FROM* fromObj)
{
assert(fromObj); // fromObj MUST not be NULL
if(isValid())
unlink();
if(toObj != NULL)
{
iRefTo = toObj;
iRefFrom = fromObj;
targetObjectBuildLink();
}
}
// We don't need the reference anymore. Call comes from the refFrom object
// Tell our refTo object, that the link is cut
void unlink() { targetObjectDestroyLink(); delink(); iRefTo = NULL; iRefFrom = NULL; }
// Link is invalid due to destruction of referenced target object. Call comes from the refTo object
// Tell our refFrom object, that the link is cut
void invalidate() // the iRefFrom MUST remain!!
{
sourceObjectDestroyLink(); delink(); iRefTo = NULL;
}
bool isValid() const // Only check the iRefTo
{
return iRefTo != NULL;
}
Reference<TO,FROM> * next() { return((Reference<TO,FROM> *) LinkedListElement::next()); }
Reference<TO,FROM> const * next() const { return((Reference<TO,FROM> const *) LinkedListElement::next()); }
Reference<TO,FROM> * prev() { return((Reference<TO,FROM> *) LinkedListElement::prev()); }
Reference<TO,FROM> const * prev() const { return((Reference<TO,FROM> const *) LinkedListElement::prev()); }
Reference<TO,FROM> * nocheck_next() { return((Reference<TO,FROM> *) LinkedListElement::nocheck_next()); }
Reference<TO,FROM> const * nocheck_next() const { return((Reference<TO,FROM> const *) LinkedListElement::nocheck_next()); }
Reference<TO,FROM> * nocheck_prev() { return((Reference<TO,FROM> *) LinkedListElement::nocheck_prev()); }
Reference<TO,FROM> const * nocheck_prev() const { return((Reference<TO,FROM> const *) LinkedListElement::nocheck_prev()); }
TO* operator ->() const { return iRefTo; }
TO* getTarget() const { return iRefTo; }
FROM* getSource() const { return iRefFrom; }
};
//=====================================================
#endif
+111
View File
@@ -0,0 +1,111 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_OBJECTREGISTRY_H
#define TRINITY_OBJECTREGISTRY_H
#include "Platform/Define.h"
#include "Utilities/UnorderedMap.h"
#include "Policies/Singleton.h"
#include <string>
#include <vector>
#include <map>
/** ObjectRegistry holds all registry item of the same type
*/
template<class T, class Key = std::string>
class ObjectRegistry
{
public:
typedef std::map<Key, T *> RegistryMapType;
/// Returns a registry item
const T* GetRegistryItem(Key key) const
{
typename RegistryMapType::const_iterator iter = i_registeredObjects.find(key);
return( iter == i_registeredObjects.end() ? NULL : iter->second );
}
/// Inserts a registry item
bool InsertItem(T *obj, Key key, bool override = false)
{
typename RegistryMapType::iterator iter = i_registeredObjects.find(key);
if( iter != i_registeredObjects.end() )
{
if( !override )
return false;
delete iter->second;
i_registeredObjects.erase(iter);
}
i_registeredObjects[key] = obj;
return true;
}
/// Removes a registry item
void RemoveItem(Key key, bool delete_object = true)
{
typename RegistryMapType::iterator iter = i_registeredObjects.find(key);
if( iter != i_registeredObjects.end() )
{
if( delete_object )
delete iter->second;
i_registeredObjects.erase(iter);
}
}
/// Returns true if registry contains an item
bool HasItem(Key key) const
{
return (i_registeredObjects.find(key) != i_registeredObjects.end());
}
/// Inefficiently return a vector of registered items
unsigned int GetRegisteredItems(std::vector<Key> &l) const
{
unsigned int sz = l.size();
l.resize(sz + i_registeredObjects.size());
for (typename RegistryMapType::const_iterator iter = i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter)
l[sz++] = iter->first;
return i_registeredObjects.size();
}
/// Return the map of registered items
RegistryMapType const &GetRegisteredItems() const
{
return i_registeredObjects;
}
private:
RegistryMapType i_registeredObjects;
friend class Trinity::OperatorNew<ObjectRegistry<T, Key> >;
// protected for friend use since it should be a singleton
ObjectRegistry() {}
~ObjectRegistry()
{
for (typename RegistryMapType::iterator iter=i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter)
delete iter->second;
i_registeredObjects.clear();
}
};
#endif
+126
View File
@@ -0,0 +1,126 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_TYPECONTAINER_H
#define TRINITY_TYPECONTAINER_H
/*
* Here, you'll find a series of containers that allow you to hold multiple
* types of object at the same time.
*/
#include <map>
#include <vector>
#include "Platform/Define.h"
#include "Utilities/TypeList.h"
#include "GameSystem/GridRefManager.h"
/*
* @class ContainerMapList is a mulit-type container for map elements
* By itself its meaningless but collaborate along with TypeContainers,
* it become the most powerfully container in the whole system.
*/
template<class OBJECT> struct ContainerMapList
{
//std::map<OBJECT_HANDLE, OBJECT *> _element;
GridRefManager<OBJECT> _element;
};
template<> struct ContainerMapList<TypeNull> /* nothing is in type null */
{
};
template<class H, class T> struct ContainerMapList<TypeList<H, T> >
{
ContainerMapList<H> _elements;
ContainerMapList<T> _TailElements;
};
/*
* @class ContaierArrayList is a multi-type container for
* array of elements.
*/
template<class OBJECT> struct ContainerArrayList
{
std::vector<OBJECT> _element;
};
// termination condition
template<> struct ContainerArrayList<TypeNull> {};
// recursion
template<class H, class T> struct ContainerArrayList<TypeList<H, T> >
{
ContainerArrayList<H> _elements;
ContainerArrayList<T> _TailElements;
};
/*
* @class ContainerList is a simple list of different types of elements
*
*/
template<class OBJECT> struct ContainerList
{
OBJECT _element;
};
/* TypeNull is underfined */
template<> struct ContainerList<TypeNull> {};
template<class H, class T> struct ContainerList<TypeList<H, T> >
{
ContainerList<H> _elements;
ContainerMapList<T> _TailElements;
};
#include "TypeContainerFunctions.h"
/*
* @class TypeMapContainer contains a fixed number of types and is
* determined at compile time. This is probably the most complicated
* class and do its simplest thing, that is, holds objects
* of different types.
*/
template<class OBJECT_TYPES>
class TypeMapContainer
{
public:
template<class SPECIFIC_TYPE> size_t Count() const { return Trinity::Count(i_elements, (SPECIFIC_TYPE*)NULL); }
/// inserts a specific object into the container
template<class SPECIFIC_TYPE> bool insert(SPECIFIC_TYPE *obj)
{
SPECIFIC_TYPE* t = Trinity::Insert(i_elements, obj);
return (t != NULL);
}
/// Removes the object from the container, and returns the removed object
template<class SPECIFIC_TYPE> bool remove(SPECIFIC_TYPE* obj)
{
SPECIFIC_TYPE* t = Trinity::Remove(i_elements, obj);
return (t != NULL);
}
ContainerMapList<OBJECT_TYPES> & GetElements(void) { return i_elements; }
const ContainerMapList<OBJECT_TYPES> & GetElements(void) const { return i_elements;}
private:
ContainerMapList<OBJECT_TYPES> i_elements;
};
#endif
@@ -0,0 +1,116 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TYPECONTAINER_FUNCTIONS_H
#define TYPECONTAINER_FUNCTIONS_H
/*
* Here you'll find a list of helper functions to make
* the TypeContainer usefull. Without it, its hard
* to access or mutate the container.
*/
#include "Platform/Define.h"
#include "Utilities/TypeList.h"
#include <map>
namespace Trinity
{
/* ContainerMapList Helpers */
// count functions
template<class SPECIFIC_TYPE> size_t Count(const ContainerMapList<SPECIFIC_TYPE> &elements, SPECIFIC_TYPE* /*fake*/)
{
return elements._element.getSize();
};
template<class SPECIFIC_TYPE> size_t Count(const ContainerMapList<TypeNull> &/*elements*/, SPECIFIC_TYPE* /*fake*/)
{
return 0;
}
template<class SPECIFIC_TYPE, class T> size_t Count(const ContainerMapList<T> &/*elements*/, SPECIFIC_TYPE* /*fake*/)
{
return 0;
}
template<class SPECIFIC_TYPE, class T> size_t Count(const ContainerMapList<TypeList<SPECIFIC_TYPE, T> >&elements, SPECIFIC_TYPE* fake)
{
return Count(elements._elements,fake);
}
template<class SPECIFIC_TYPE, class H, class T> size_t Count(const ContainerMapList<TypeList<H, T> >&elements, SPECIFIC_TYPE* fake)
{
return Count(elements._TailElements, fake);
}
// non-const insert functions
template<class SPECIFIC_TYPE> SPECIFIC_TYPE* Insert(ContainerMapList<SPECIFIC_TYPE> &elements, SPECIFIC_TYPE *obj)
{
//elements._element[hdl] = obj;
obj->GetGridRef().link(&elements._element, obj);
return obj;
};
template<class SPECIFIC_TYPE> SPECIFIC_TYPE* Insert(ContainerMapList<TypeNull> &/*elements*/, SPECIFIC_TYPE * /*obj*/)
{
return NULL;
}
// this is a missed
template<class SPECIFIC_TYPE, class T> SPECIFIC_TYPE* Insert(ContainerMapList<T> &/*elements*/, SPECIFIC_TYPE * /*obj*/)
{
return NULL; // a missed
}
// Recursion
template<class SPECIFIC_TYPE, class H, class T> SPECIFIC_TYPE* Insert(ContainerMapList<TypeList<H, T> >&elements, SPECIFIC_TYPE *obj)
{
SPECIFIC_TYPE* t= Insert(elements._elements, obj);
return (t != NULL ? t : Insert(elements._TailElements, obj));
}
// non-const remove method
template<class SPECIFIC_TYPE> SPECIFIC_TYPE* Remove(ContainerMapList<SPECIFIC_TYPE> & /*elements*/, SPECIFIC_TYPE *obj)
{
obj->GetGridRef().unlink();
return obj;
}
template<class SPECIFIC_TYPE> SPECIFIC_TYPE* Remove(ContainerMapList<TypeNull> &/*elements*/, SPECIFIC_TYPE * /*obj*/)
{
return NULL;
}
// this is a missed
template<class SPECIFIC_TYPE, class T> SPECIFIC_TYPE* Remove(ContainerMapList<T> &/*elements*/, SPECIFIC_TYPE * /*obj*/)
{
return NULL; // a missed
}
template<class SPECIFIC_TYPE, class T, class H> SPECIFIC_TYPE* Remove(ContainerMapList<TypeList<H, T> > &elements, SPECIFIC_TYPE *obj)
{
// The head element is bad
SPECIFIC_TYPE* t = Remove(elements._elements, obj);
return ( t != NULL ? t : Remove(elements._TailElements, obj) );
}
}
#endif
@@ -0,0 +1,170 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TYPECONTAINER_FUNCTIONS_PTR_H
#define TYPECONTAINER_FUNCTIONS_PTR_H
/*
* Here you'll find a list of helper functions to make
* the TypeContainer usefull. Without it, its hard
* to access or mutate the container.
*/
#include "Platform/Define.h"
#include "Utilities/TypeList.h"
#include <map>
namespace Trinity
{
/* ContainerMapList Helpers */
// count functions
// template<class SPECIFIC_TYPE> size_t Count(const ContainerMapList<SPECIFIC_TYPE> &elements, CountedPtr<SPECIFIC_TYPE>* /*fake*/)
// {
// return elements._element.size();
// };
//
// template<class SPECIFIC_TYPE> size_t Count(const ContainerMapList<TypeNull> &elements, CountedPtr<SPECIFIC_TYPE>* /*fake*/)
// {
// return 0;
// }
//
// template<class SPECIFIC_TYPE, class T> size_t Count(const ContainerMapList<T> &elements, CountedPtr<SPECIFIC_TYPE>* /*fake*/)
// {
// return 0;
// }
//
// template<class SPECIFIC_TYPE, class T> size_t Count(const ContainerMapList<TypeList<SPECIFIC_TYPE, T> >&elements, SPECIFIC_TYPE* fake)
// {
// return Count(elements._elements,fake);
// }
//
// template<class SPECIFIC_TYPE, class H, class T> size_t Count(const ContainerMapList<TypeList<H, T> >&elements, SPECIFIC_TYPE* fake)
// {
// return Count(elements._TailElements, fake);
// }
// non-const find functions
template<class SPECIFIC_TYPE> CountedPtr<SPECIFIC_TYPE>& Find(ContainerMapList<SPECIFIC_TYPE> &elements, OBJECT_HANDLE hdl, CountedPtr<SPECIFIC_TYPE>* /*fake*/)
{
typename std::map<OBJECT_HANDLE, CountedPtr<SPECIFIC_TYPE> >::iterator iter = elements._element.find(hdl);
return (iter == elements._element.end() ? NullPtr<SPECIFIC_TYPE>((SPECIFIC_TYPE*)NULL) : iter->second);
};
template<class SPECIFIC_TYPE> CountedPtr<SPECIFIC_TYPE>& Find(ContainerMapList<TypeNull> &elements, OBJECT_HANDLE hdl, CountedPtr<SPECIFIC_TYPE>* /*fake*/)
{
return NullPtr<SPECIFIC_TYPE>((SPECIFIC_TYPE*)NULL);// terminate recursion
}
template<class SPECIFIC_TYPE, class T> CountedPtr<SPECIFIC_TYPE>& Find(ContainerMapList<T> &elements, OBJECT_HANDLE hdl, CountedPtr<SPECIFIC_TYPE>* /*fake*/)
{
return NullPtr<SPECIFIC_TYPE>((SPECIFIC_TYPE*)NULL);// this is a missed
}
template<class SPECIFIC_TYPE, class H, class T> CountedPtr<SPECIFIC_TYPE>& Find(ContainerMapList<TypeList<H, T> >&elements, OBJECT_HANDLE hdl, CountedPtr<SPECIFIC_TYPE>* fake)
{
CountedPtr<SPECIFIC_TYPE> &t = Find(elements._elements, hdl,fake);
return (!t ? Find(elements._TailElements, hdl,fake) : t);
}
// const find functions
template<class SPECIFIC_TYPE> const CountedPtr<SPECIFIC_TYPE>& Find(const ContainerMapList<SPECIFIC_TYPE> &elements, OBJECT_HANDLE hdl, CountedPtr<SPECIFIC_TYPE>* /*fake*/)
{
typename CountedPtr<SPECIFIC_TYPE>::iterator iter = elements._element.find(hdl);
return (iter == elements._element.end() ? NullPtr<SPECIFIC_TYPE>((SPECIFIC_TYPE*)NULL) : iter->second);
};
template<class SPECIFIC_TYPE> const CountedPtr<SPECIFIC_TYPE>& Find(const ContainerMapList<TypeNull> &elements, OBJECT_HANDLE hdl, CountedPtr<SPECIFIC_TYPE>* /*fake*/)
{
return NullPtr<SPECIFIC_TYPE>((SPECIFIC_TYPE*)NULL);
}
template<class SPECIFIC_TYPE, class T> const CountedPtr<SPECIFIC_TYPE>& Find(const ContainerMapList<T> &elements, OBJECT_HANDLE hdl, CountedPtr<SPECIFIC_TYPE>* /*fake*/)
{
return NullPtr<SPECIFIC_TYPE>((SPECIFIC_TYPE*)NULL);
}
template<class SPECIFIC_TYPE, class H, class T> CountedPtr<SPECIFIC_TYPE>& Find(const ContainerMapList<TypeList<H, T> >&elements, OBJECT_HANDLE hdl, CountedPtr<SPECIFIC_TYPE>* fake)
{
CountedPtr<SPECIFIC_TYPE> &t = Find(elements._elements, hdl,fake);
if(!t)
t = Find(elements._TailElement, hdl,fake);
return t;
}
// non-const insert functions
template<class SPECIFIC_TYPE> CountedPtr<SPECIFIC_TYPE>& Insert(ContainerMapList<SPECIFIC_TYPE> &elements, CountedPtr<SPECIFIC_TYPE> &obj, OBJECT_HANDLE hdl)
{
elements._element[hdl] = obj;
return obj;
};
template<class SPECIFIC_TYPE> CountedPtr<SPECIFIC_TYPE>& Insert(ContainerMapList<TypeNull> &elements, CountedPtr<SPECIFIC_TYPE> &obj, OBJECT_HANDLE hdl)
{
return NullPtr<SPECIFIC_TYPE>((SPECIFIC_TYPE*)NULL);
}
// this is a missed
template<class SPECIFIC_TYPE, class T> CountedPtr<SPECIFIC_TYPE>& Insert(ContainerMapList<T> &elements, CountedPtr<SPECIFIC_TYPE> &obj, OBJECT_HANDLE hdl)
{
return NullPtr<SPECIFIC_TYPE>((SPECIFIC_TYPE*)NULL);// a missed
}
// Recursion
template<class SPECIFIC_TYPE, class H, class T> CountedPtr<SPECIFIC_TYPE>& Insert(ContainerMapList<TypeList<H, T> >&elements, CountedPtr<SPECIFIC_TYPE> &obj, OBJECT_HANDLE hdl)
{
CountedPtr<SPECIFIC_TYPE> &t= Insert(elements._elements, obj, hdl);
return (!t ? Insert(elements._TailElements, obj, hdl) : t);
}
// non-const remove method
template<class SPECIFIC_TYPE> bool Remove(ContainerMapList<SPECIFIC_TYPE> &elements, CountedPtr<SPECIFIC_TYPE> &obj, OBJECT_HANDLE hdl)
{
typename std::map<OBJECT_HANDLE, CountedPtr<SPECIFIC_TYPE> >::iterator iter = elements._element.find(hdl);
if( iter != elements._element.end() )
{
elements._element.erase(iter);
return true;
}
return false; // found... terminate the search
}
template<class SPECIFIC_TYPE> bool Remove(ContainerMapList<TypeNull> &elements, CountedPtr<SPECIFIC_TYPE> &obj, OBJECT_HANDLE hdl)
{
return false;
}
// this is a missed
template<class SPECIFIC_TYPE, class T> bool Remove(ContainerMapList<T> &elements, CountedPtr<SPECIFIC_TYPE> &obj, OBJECT_HANDLE hdl)
{
return false;
}
template<class SPECIFIC_TYPE, class T, class H> bool Remove(ContainerMapList<TypeList<H, T> > &elements, CountedPtr<SPECIFIC_TYPE> &obj, OBJECT_HANDLE hdl)
{
// The head element is bad
bool t = Remove(elements._elements, obj, hdl);
return ( !t ? Remove(elements._TailElements, obj, hdl) : t );
}
}
#endif
@@ -0,0 +1,119 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_TYPECONTAINERVISITOR_H
#define TRINITY_TYPECONTAINERVISITOR_H
/*
* @class TypeContainerVisitor is implemented as a visitor pattern. It is
* a visitor to the TypeContainerList or TypeContainerMapList. The visitor has
* to overload its types as a visit method is called.
*/
#include "Platform/Define.h"
#include "TypeContainer.h"
// forward declaration
template<class T, class Y> class TypeContainerVisitor;
// visitor helper
template<class VISITOR, class TYPE_CONTAINER> void VisitorHelper(VISITOR &v, TYPE_CONTAINER &c)
{
v.Visit(c);
};
// terminate condition for container list
template<class VISITOR> void VisitorHelper(VISITOR &v, ContainerList<TypeNull> &c)
{
}
template<class VISITOR, class T> void VisitorHelper(VISITOR &v, ContainerList<T> &c)
{
v.Visit(c._element);
}
// recursion for container list
template<class VISITOR, class H, class T> void VisitorHelper(VISITOR &v, ContainerList<TypeList<H, T> > &c)
{
VisitorHelper(v, c._elements);
VisitorHelper(v, c._TailElements);
}
// terminate condition container map list
template<class VISITOR> void VisitorHelper(VISITOR &/*v*/, ContainerMapList<TypeNull> &/*c*/)
{
}
template<class VISITOR, class T> void VisitorHelper(VISITOR &v, ContainerMapList<T> &c)
{
v.Visit(c._element);
}
// recursion container map list
template<class VISITOR, class H, class T> void VisitorHelper(VISITOR &v, ContainerMapList<TypeList<H, T> > &c)
{
VisitorHelper(v, c._elements);
VisitorHelper(v, c._TailElements);
}
// array list
template<class VISITOR, class T> void VisitorHelper(VISITOR &v, ContainerArrayList<T> &c)
{
v.Visit(c._element);
}
template<class VISITOR> void VisitorHelper(VISITOR &/*v*/, ContainerArrayList<TypeNull> &/*c*/)
{
}
// recursion
template<class VISITOR, class H, class T> void VisitorHelper(VISITOR &v, ContainerArrayList<TypeList<H, T> > &c)
{
VisitorHelper(v, c._elements);
VisitorHelper(v, c._TailElements);
}
// for TypeMapContainer
template<class VISITOR, class OBJECT_TYPES> void VisitorHelper(VISITOR &v, TypeMapContainer<OBJECT_TYPES> &c)
{
VisitorHelper(v, c.GetElements());
}
template<class VISITOR, class TYPE_CONTAINER>
class TypeContainerVisitor
{
public:
TypeContainerVisitor(VISITOR &v) : i_visitor(v) {}
void Visit(TYPE_CONTAINER &c)
{
VisitorHelper(i_visitor, c);
}
void Visit(const TYPE_CONTAINER &c) const
{
VisitorHelper(i_visitor, c);
}
private:
VISITOR &i_visitor;
};
#endif
+46
View File
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_TYPELIST_H
#define TRINITY_TYPELIST_H
/*
@struct TypeList
TypeList is the most simple but yet the most powerfull class of all. It holds
at compile time the different type of objects in a linked list.
*/
class TypeNull;
template<typename HEAD, typename TAIL>
struct TypeList
{
typedef HEAD Head;
typedef TAIL Tail;
};
// enough for now.. can be expand at any point in time as needed
#define TYPELIST_1(T1) TypeList<T1,TypeNull>
#define TYPELIST_2(T1, T2) TypeList<T1, TYPELIST_1(T2) >
#define TYPELIST_3(T1, T2, T3) TypeList<T1, TYPELIST_2(T2, T3) >
#define TYPELIST_4(T1, T2, T3, T4) TypeList<T1, TYPELIST_3(T2, T3, T4) >
#define TYPELIST_5(T1, T2, T3, T4, T5) TypeList<T1, TYPELIST_4(T2, T3, T4, T5) >
#endif
+73
View File
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_UNORDERED_MAP_H
#define TRINITY_UNORDERED_MAP_H
#include "Platform/CompilerDefs.h"
#include "Platform/Define.h"
#if COMPILER == COMPILER_INTEL
#include <ext/hash_map>
#elif COMPILER == COMPILER_GNU && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3)
#include <tr1/unordered_map>
#elif COMPILER == COMPILER_GNU && __GNUC__ >= 3
#include <ext/hash_map>
#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1500 && _HAS_TR1 // VC9.0 and later
#include <unordered_map>
#else
#include <hash_map>
#endif
#ifdef _STLPORT_VERSION
#define UNORDERED_MAP std::hash_map
using std::hash_map;
#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1500 && _HAS_TR1
#define UNORDERED_MAP std::tr1::unordered_map
#elif COMPILER == COMPILER_MICROSOFT && _MSC_VER >= 1300
#define UNORDERED_MAP stdext::hash_map
using stdext::hash_map;
#elif COMPILER == COMPILER_INTEL
#define UNORDERED_MAP std::hash_map
using std::hash_map;
#elif COMPILER == COMPILER_GNU && (__GNUC__ > 4 || __GNUC__ == 4 && __GNUC_MINOR__ >= 3)
#define UNORDERED_MAP std::tr1::unordered_map
#elif COMPILER == COMPILER_GNU && __GNUC__ >= 3
#define UNORDERED_MAP __gnu_cxx::hash_map
namespace __gnu_cxx
{
template<> struct hash<unsigned long long>
{
size_t operator()(const unsigned long long &__x) const { return (size_t)__x; }
};
template<typename T> struct hash<T *>
{
size_t operator()(T * const &__x) const { return (size_t)__x; }
};
};
#else
#define UNORDERED_MAP std::hash_map
using std::hash_map;
#endif
#endif
+110
View File
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_CREATIONPOLICY_H
#define TRINITY_CREATIONPOLICY_H
#include <stdlib.h>
#include "Platform/Define.h"
namespace Trinity
{
/**
* OperatorNew policy creates an object on the heap using new.
*/
template <class T>
class 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 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 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 CreateOnCallBack
{
public:
static T* Create()
{
return CALL_BACK::createCallBack();
}
static void Destroy(T *p)
{
CALL_BACK::destroyCallBack(p);
}
};
}
#endif
@@ -0,0 +1,36 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 Trinity
{
extern "C" void external_wrapper(void *p)
{
std::atexit( (void (*)())p );
}
void at_exit( void (*func)() )
{
external_wrapper((void*)func);
}
}
@@ -0,0 +1,53 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_OBJECTLIFETIME_H
#define TRINITY_OBJECTLIFETIME_H
#include <stdexcept>
#include "Platform/Define.h"
typedef void (* Destroyer)(void);
namespace Trinity
{
void at_exit( void (*func)() );
template <class T>
class ObjectLifeTime
{
public:
static void ScheduleCall(void (*destroyer)() )
{
at_exit( destroyer );
}
DECLSPEC_NORETURN static void OnDeadReference(void) ATTR_NORETURN;
};
template <class T>
void ObjectLifeTime<T>::OnDeadReference(void) // We don't handle Dead Reference for now
{
throw std::runtime_error("Dead Reference");
}
}
#endif
+65
View File
@@ -0,0 +1,65 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_SINGLETON_H
#define TRINITY_SINGLETON_H
/**
* @brief class Singleton
*/
#include "CreationPolicy.h"
#include "ThreadingModel.h"
#include "ObjectLifeTime.h"
namespace Trinity
{
template
<
typename T,
class ThreadingModel = Trinity::SingleThreaded<T>,
class CreatePolicy = Trinity::OperatorNew<T>,
class LifeTimePolicy = Trinity::ObjectLifeTime<T>
>
class 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
+93
View File
@@ -0,0 +1,93 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_SINGLETONIMPL_H
#define TRINITY_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&
Trinity::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
Trinity::Singleton<T, ThreadingModel, CreatePolicy, LifeTimePolicy>::DestroySingleton()
{
CreatePolicy::Destroy(si_instance);
si_instance = NULL;
si_destroyed = true;
}
#define INSTANTIATE_SINGLETON_1(TYPE) \
template class Trinity::Singleton<TYPE, Trinity::SingleThreaded<TYPE>, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >; \
template<> TYPE* Trinity::Singleton<TYPE, Trinity::SingleThreaded<TYPE>, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >::si_instance = 0; \
template<> bool Trinity::Singleton<TYPE, Trinity::SingleThreaded<TYPE>, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >::si_destroyed = false
#define INSTANTIATE_SINGLETON_2(TYPE, THREADINGMODEL) \
template class Trinity::Singleton<TYPE, THREADINGMODEL, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >; \
template<> TYPE* Trinity::Singleton<TYPE, THREADINGMODEL, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >::si_instance = 0; \
template<> bool Trinity::Singleton<TYPE, THREADINGMODEL, Trinity::OperatorNew<TYPE>, Trinity::ObjectLifeTime<TYPE> >::si_destroyed = false
#define INSTANTIATE_SINGLETON_3(TYPE, THREADINGMODEL, CREATIONPOLICY ) \
template class Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, Trinity::ObjectLifeTime<TYPE> >; \
template<> TYPE* Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, Trinity::ObjectLifeTime<TYPE> >::si_instance = 0; \
template<> bool Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, Trinity::ObjectLifeType<TYPE> >::si_destroyed = false
#define INSTANTIATE_SINGLETON_4(TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME) \
template class Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >; \
template<> TYPE* Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >::si_instance = 0; \
template<> bool Trinity::Singleton<TYPE, THREADINGMODEL, CREATIONPOLICY, OBJECTLIFETIME >::si_destroyed = false
#endif
+130
View File
@@ -0,0 +1,130 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_THREADINGMODEL_H
#define TRINITY_THREADINGMODEL_H
/**
* @class ThreadingModel<T>
*
*/
#include "Platform/Define.h"
namespace Trinity
{
inline void Guard(void *) {}
template<typename MUTEX> class 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 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 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 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 Trinity::ClassLevelLockable<T, MUTEX>::si_mtx;
#define INSTANTIATE_CLASS_MUTEX(CTYPE,MUTEX) \
template class Trinity::ClassLevelLockable<CTYPE, MUTEX >
#endif
+386
View File
@@ -0,0 +1,386 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_CALLBACK_H
#define TRINITY_CALLBACK_H
/// ------------ BASE CLASSES ------------
namespace Trinity
{
template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void >
class _Callback
{
protected:
typedef void (Class::*Method)(ParamType1, ParamType2, ParamType3, ParamType4);
Class *m_object;
Method m_method;
ParamType1 m_param1;
ParamType2 m_param2;
ParamType3 m_param3;
ParamType4 m_param4;
void _Execute() { (m_object->*m_method)(m_param1, m_param2, m_param3, m_param4); }
public:
_Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4)
: m_object(object), m_method(method), m_param1(param1), m_param2(param2), m_param3(param3), m_param4(param4) {}
_Callback(_Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4> const& cb)
: m_object(cb.object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3), m_param4(cb.m_param4) {}
};
template < class Class, typename ParamType1, typename ParamType2, typename ParamType3 >
class _Callback < Class, ParamType1, ParamType2, ParamType3 >
{
protected:
typedef void (Class::*Method)(ParamType1, ParamType2, ParamType3);
Class *m_object;
Method m_method;
ParamType1 m_param1;
ParamType2 m_param2;
ParamType3 m_param3;
void _Execute() { (m_object->*m_method)(m_param1, m_param2, m_param3); }
public:
_Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3)
: m_object(object), m_method(method), m_param1(param1), m_param2(param2) {}
_Callback(_Callback < Class, ParamType1, ParamType2, ParamType3 > const& cb)
: m_object(cb.object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3) {}
};
template < class Class, typename ParamType1, typename ParamType2 >
class _Callback < Class, ParamType1, ParamType2 >
{
protected:
typedef void (Class::*Method)(ParamType1, ParamType2);
Class *m_object;
Method m_method;
ParamType1 m_param1;
ParamType2 m_param2;
void _Execute() { (m_object->*m_method)(m_param1, m_param2); }
public:
_Callback(Class *object, Method method, ParamType1 param1, ParamType2 param2)
: m_object(object), m_method(method), m_param1(param1), m_param2(param2) {}
_Callback(_Callback < Class, ParamType1, ParamType2 > const& cb)
: m_object(cb.m_object), m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2) {}
};
template < class Class, typename ParamType1 >
class _Callback < Class, ParamType1 >
{
protected:
typedef void (Class::*Method)(ParamType1);
Class *m_object;
Method m_method;
ParamType1 m_param1;
void _Execute() { (m_object->*m_method)(m_param1); }
public:
_Callback(Class *object, Method method, ParamType1 param1)
: m_object(object), m_method(method), m_param1(param1) {}
_Callback(_Callback < Class, ParamType1 > const& cb)
: m_object(cb.m_object), m_method(cb.m_method), m_param1(cb.m_param1) {}
};
template < class Class >
class _Callback < Class >
{
protected:
typedef void (Class::*Method)();
Class *m_object;
Method m_method;
void _Execute() { (m_object->*m_method)(); }
public:
_Callback(Class *object, Method method)
: m_object(object), m_method(method) {}
_Callback(_Callback < Class > const& cb)
: m_object(cb.m_object), m_method(cb.m_method) {}
};
/// ---- Statics ----
template < typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void >
class _SCallback
{
protected:
typedef void (*Method)(ParamType1, ParamType2, ParamType3, ParamType4);
Method m_method;
ParamType1 m_param1;
ParamType2 m_param2;
ParamType3 m_param3;
ParamType4 m_param4;
void _Execute() { (*m_method)(m_param1, m_param2, m_param3, m_param4); }
public:
_SCallback(Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4)
: m_method(method), m_param1(param1), m_param2(param2), m_param3(param3), m_param4(param4) {}
_SCallback(_SCallback < ParamType1, ParamType2, ParamType3, ParamType4> const& cb)
: m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3), m_param4(cb.m_param4) {}
};
template < typename ParamType1, typename ParamType2, typename ParamType3 >
class _SCallback < ParamType1, ParamType2, ParamType3 >
{
protected:
typedef void (*Method)(ParamType1, ParamType2, ParamType3);
Method m_method;
ParamType1 m_param1;
ParamType2 m_param2;
ParamType3 m_param3;
void _Execute() { (*m_method)(m_param1, m_param2, m_param3); }
public:
_SCallback(Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3)
: m_method(method), m_param1(param1), m_param2(param2), m_param3(param3) {}
_SCallback(_SCallback < ParamType1, ParamType2, ParamType3 > const& cb)
: m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2), m_param3(cb.m_param3) {}
};
template < typename ParamType1, typename ParamType2 >
class _SCallback < ParamType1, ParamType2 >
{
protected:
typedef void (*Method)(ParamType1, ParamType2);
Method m_method;
ParamType1 m_param1;
ParamType2 m_param2;
void _Execute() { (*m_method)(m_param1, m_param2); }
public:
_SCallback(Method method, ParamType1 param1, ParamType2 param2)
: m_method(method), m_param1(param1), m_param2(param2) {}
_SCallback(_SCallback < ParamType1, ParamType2 > const& cb)
: m_method(cb.m_method), m_param1(cb.m_param1), m_param2(cb.m_param2) {}
};
template < typename ParamType1 >
class _SCallback < ParamType1 >
{
protected:
typedef void (*Method)(ParamType1);
Method m_method;
ParamType1 m_param1;
void _Execute() { (*m_method)(m_param1); }
public:
_SCallback(Method method, ParamType1 param1)
: m_method(method), m_param1(param1) {}
_SCallback(_SCallback < ParamType1 > const& cb)
: m_method(cb.m_method), m_param1(cb.m_param1) {}
};
template < >
class _SCallback < >
{
protected:
typedef void (*Method)();
Method m_method;
void _Execute() { (*m_method)(); }
public:
_SCallback(Method method)
: m_method(method) {}
_SCallback(_SCallback <> const& cb)
: m_method(cb.m_method) {}
};
}
/// --------- GENERIC CALLBACKS ----------
namespace Trinity
{
class ICallback
{
public:
virtual void Execute() = 0;
virtual ~ICallback() {}
};
template < class CB >
class _ICallback : public CB, public ICallback
{
public:
_ICallback(CB const& cb) : CB(cb) {}
void Execute() { CB::_Execute(); }
};
template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void, typename ParamType4 = void >
class Callback :
public _ICallback< _Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4 > >
{
private:
typedef _Callback < Class, ParamType1, ParamType2, ParamType3, ParamType4 > C4;
public:
Callback(Class *object, typename C4::Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3, ParamType4 param4)
: _ICallback< C4 >(C4(object, method, param1, param2, param3, param4)) {}
};
template < class Class, typename ParamType1, typename ParamType2, typename ParamType3 >
class Callback < Class, ParamType1, ParamType2, ParamType3 > :
public _ICallback< _Callback < Class, ParamType1, ParamType2, ParamType3 > >
{
private:
typedef _Callback < Class, ParamType1, ParamType2, ParamType3 > C3;
public:
Callback(Class *object, typename C3::Method method, ParamType1 param1, ParamType2 param2, ParamType3 param3)
: _ICallback< C3 >(C3(object, method, param1, param2, param3)) {}
};
template < class Class, typename ParamType1, typename ParamType2 >
class Callback < Class, ParamType1, ParamType2 > :
public _ICallback< _Callback < Class, ParamType1, ParamType2 > >
{
private:
typedef _Callback < Class, ParamType1, ParamType2 > C2;
public:
Callback(Class *object, typename C2::Method method, ParamType1 param1, ParamType2 param2)
: _ICallback< C2 >(C2(object, method, param1, param2)) {}
};
template < class Class, typename ParamType1 >
class Callback < Class, ParamType1 > :
public _ICallback< _Callback < Class, ParamType1 > >
{
private:
typedef _Callback < Class, ParamType1 > C1;
public:
Callback(Class *object, typename C1::Method method, ParamType1 param1)
: _ICallback< C1 >(C1(object, method, param1)) {}
};
template < class Class >
class Callback < Class > : public _ICallback< _Callback < Class > >
{
private:
typedef _Callback < Class > C0;
public:
Callback(Class *object, typename C0::Method method)
: _ICallback< C0 >(C0(object, method)) {}
};
}
/// ---------- QUERY CALLBACKS -----------
#include "QueryResult.h"
class QueryResult;
namespace Trinity
{
class IQueryCallback
{
public:
virtual void Execute() = 0;
virtual ~IQueryCallback() {}
virtual void SetResult(QueryResult_AutoPtr result) = 0;
virtual QueryResult_AutoPtr GetResult() = 0;
};
template < class CB >
class _IQueryCallback : public CB, public IQueryCallback
{
public:
_IQueryCallback(CB const& cb) : CB(cb) {}
void Execute() { CB::_Execute(); }
void SetResult(QueryResult_AutoPtr result) { CB::m_param1 = result; }
QueryResult_AutoPtr GetResult() { return CB::m_param1; }
};
template < class Class, typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void >
class QueryCallback :
public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > >
{
private:
typedef _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > QC3;
public:
QueryCallback(Class *object, typename QC3::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2, ParamType3 param3)
: _IQueryCallback< QC3 >(QC3(object, method, result, param1, param2, param3)) {}
};
template < class Class, typename ParamType1, typename ParamType2 >
class QueryCallback < Class, ParamType1, ParamType2 > :
public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2 > >
{
private:
typedef _Callback < Class, QueryResult_AutoPtr, ParamType1, ParamType2 > QC2;
public:
QueryCallback(Class *object, typename QC2::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2)
: _IQueryCallback< QC2 >(QC2(object, method, result, param1, param2)) {}
};
template < class Class, typename ParamType1 >
class QueryCallback < Class, ParamType1 > :
public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr, ParamType1 > >
{
private:
typedef _Callback < Class, QueryResult_AutoPtr, ParamType1 > QC1;
public:
QueryCallback(Class *object, typename QC1::Method method, QueryResult_AutoPtr result, ParamType1 param1)
: _IQueryCallback< QC1 >(QC1(object, method, result, param1)) {}
};
template < class Class >
class QueryCallback < Class > : public _IQueryCallback< _Callback < Class, QueryResult_AutoPtr > >
{
private:
typedef _Callback < Class, QueryResult_AutoPtr > QC0;
public:
QueryCallback(Class *object, typename QC0::Method method, QueryResult_AutoPtr result)
: _IQueryCallback< QC0 >(QC0(object, method, result)) {}
};
/// ---- Statics ----
template < typename ParamType1 = void, typename ParamType2 = void, typename ParamType3 = void >
class SQueryCallback :
public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > >
{
private:
typedef _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2, ParamType3 > QC3;
public:
SQueryCallback(typename QC3::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2, ParamType3 param3)
: _IQueryCallback< QC3 >(QC3(method, result, param1, param2, param3)) {}
};
template < typename ParamType1, typename ParamType2 >
class SQueryCallback < ParamType1, ParamType2 > :
public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2 > >
{
private:
typedef _SCallback < QueryResult_AutoPtr, ParamType1, ParamType2 > QC2;
public:
SQueryCallback(typename QC2::Method method, QueryResult_AutoPtr result, ParamType1 param1, ParamType2 param2)
: _IQueryCallback< QC2 >(QC2(method, result, param1, param2)) {}
};
template < typename ParamType1 >
class SQueryCallback < ParamType1 > :
public _IQueryCallback< _SCallback < QueryResult_AutoPtr, ParamType1 > >
{
private:
typedef _SCallback < QueryResult_AutoPtr, ParamType1 > QC1;
public:
SQueryCallback(typename QC1::Method method, QueryResult_AutoPtr result, ParamType1 param1)
: _IQueryCallback< QC1 >(QC1(method, result, param1)) {}
};
template < >
class SQueryCallback < > : public _IQueryCallback< _SCallback < QueryResult_AutoPtr > >
{
private:
typedef _SCallback < QueryResult_AutoPtr > QC0;
public:
SQueryCallback(QC0::Method method, QueryResult_AutoPtr result)
: _IQueryCallback< QC0 >(QC0(method, result)) {}
};
}
#endif
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 TRINITY_BYTECONVERTER_H
#define TRINITY_BYTECONVERTER_H
/** ByteConverter reverse your byte order. This is use
for cross platform where they have different endians.
*/
#include<Platform/Define.h>
#include<algorithm>
namespace ByteConverter
{
template<size_t T>
inline void convert(char *val)
{
std::swap(*val, *(val + T - 1));
convert<T - 2>(val + 1);
}
template<> inline void convert<0>(char *) {}
template<> inline void convert<1>(char *) {} // ignore central byte
template<typename T> inline void apply(T *val)
{
convert<sizeof(T)>((char *)(val));
}
}
#if TRINITY_ENDIAN == TRINITY_BIGENDIAN
template<typename T> inline void EndianConvert(T& val) { ByteConverter::apply<T>(&val); }
template<typename T> inline void EndianConvertReverse(T&) { }
#else
template<typename T> inline void EndianConvert(T&) { }
template<typename T> inline void EndianConvertReverse(T& val) { ByteConverter::apply<T>(&val); }
#endif
template<typename T> void EndianConvert(T*); // will generate link error
template<typename T> void EndianConvertReverse(T*); // will generate link error
inline void EndianConvert(uint8&) { }
inline void EndianConvert( int8&) { }
inline void EndianConvertReverse(uint8&) { }
inline void EndianConvertReverse( int8&) { }
#endif
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 "EventProcessor.h"
EventProcessor::EventProcessor()
{
m_time = 0;
m_aborting = false;
}
EventProcessor::~EventProcessor()
{
KillAllEvents(true);
}
void EventProcessor::Update(uint32 p_time)
{
// update time
m_time += p_time;
// main event loop
EventList::iterator i;
while (((i = m_events.begin()) != m_events.end()) && i->first <= m_time)
{
// get and remove event from queue
BasicEvent* Event = i->second;
m_events.erase(i);
if (!Event->to_Abort)
{
if (Event->Execute(m_time, p_time))
{
// completely destroy event if it is not re-added
delete Event;
}
}
else
{
Event->Abort(m_time);
delete Event;
}
}
}
void EventProcessor::KillAllEvents(bool force)
{
// prevent event insertions
m_aborting = true;
// first, abort all existing events
for (EventList::iterator i = m_events.begin(); i != m_events.end();)
{
EventList::iterator i_old = i;
++i;
i_old->second->to_Abort = true;
i_old->second->Abort(m_time);
if(force || i_old->second->IsDeletable())
{
delete i_old->second;
if(!force) // need per-element cleanup
m_events.erase (i_old);
}
}
// fast clear event list (in force case)
if(force)
m_events.clear();
}
void EventProcessor::AddEvent(BasicEvent* Event, uint64 e_time, bool set_addtime)
{
if (set_addtime) Event->m_addTime = m_time;
Event->m_execTime = e_time;
m_events.insert(std::pair<uint64, BasicEvent*>(e_time, Event));
}
uint64 EventProcessor::CalculateTime(uint64 t_offset)
{
return(m_time + t_offset);
}
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
*
* Copyright (C) 2008-2010 Trinity <http://www.trinitycore.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 __EVENTPROCESSOR_H
#define __EVENTPROCESSOR_H
#include "Platform/Define.h"
#include<map>
// Note. All times are in milliseconds here.
class BasicEvent
{
public:
BasicEvent() { to_Abort = false; }
virtual ~BasicEvent() // override destructor to perform some actions on event removal
{
};
// this method executes when the event is triggered
// return false if event does not want to be deleted
// e_time is execution time, p_time is update interval
virtual bool Execute(uint64 /*e_time*/, uint32 /*p_time*/) { return true; }
virtual bool IsDeletable() const { return true; } // this event can be safely deleted
virtual void Abort(uint64 /*e_time*/) {} // this method executes when the event is aborted
bool to_Abort; // set by externals when the event is aborted, aborted events don't execute
// and get Abort call when deleted
// these can be used for time offset control
uint64 m_addTime; // time when the event was added to queue, filled by event handler
uint64 m_execTime; // planned time of next execution, filled by event handler
};
typedef std::multimap<uint64, BasicEvent*> EventList;
class EventProcessor
{
public:
EventProcessor();
~EventProcessor();
void Update(uint32 p_time);
void KillAllEvents(bool force);
void AddEvent(BasicEvent* Event, uint64 e_time, bool set_addtime = true);
uint64 CalculateTime(uint64 t_offset);
protected:
uint64 m_time;
EventList m_events;
bool m_aborting;
};
#endif