/* * Copyright (C) 2010 - 2014 Eluna Lua Engine * This program is free software licensed under GPL version 3 * Please see the included DOCS/LICENSE.md for more information */ #ifndef _ELUNA_UTIL_H #define _ELUNA_UTIL_H #include "Common.h" #include "SharedDefines.h" #include "ObjectGuid.h" #ifdef TRINITY #include "QueryResult.h" #ifdef CATA #include "Object.h" #endif #else #include "Database/QueryResult.h" #endif // Some dummy includes containing BOOST_VERSION: // ObjectAccessor.h Config.h Log.h #ifdef BOOST_VERSION #define USING_BOOST #endif #ifdef USING_BOOST #include #include #endif #ifdef TRINITY typedef QueryResult ElunaQuery; #define ELUNA_LOG_INFO(...) TC_LOG_INFO("eluna", __VA_ARGS__); #define ELUNA_LOG_ERROR(...) TC_LOG_ERROR("eluna", __VA_ARGS__); #define ELUNA_LOG_DEBUG(...) TC_LOG_DEBUG("eluna", __VA_ARGS__); #define GET_GUID GetGUID #else typedef QueryNamedResult ElunaQuery; #define ASSERT MANGOS_ASSERT #define ELUNA_LOG_INFO(...) sLog.outString(__VA_ARGS__); #define ELUNA_LOG_ERROR(...) sLog.outErrorEluna(__VA_ARGS__); #define ELUNA_LOG_DEBUG(...) sLog.outDebug(__VA_ARGS__); #define GET_GUID GetObjectGuid #define GetGameObjectTemplate GetGameObjectInfo #define GetItemTemplate GetItemPrototype #define GetTemplate GetProto #endif #ifndef UNORDERED_MAP #include #define UNORDERED_MAP std::unordered_map #endif #ifndef UNORDERED_SET #include #define UNORDERED_SET std::unordered_set #endif #ifndef MAKE_NEW_GUID #define MAKE_NEW_GUID(l, e, h) ObjectGuid(h, e, l) #endif #ifndef GUID_ENPART #define GUID_ENPART(guid) ObjectGuid(guid).GetEntry() #endif #ifndef GUID_LOPART #define GUID_LOPART(guid) ObjectGuid(guid).GetCounter() #endif #ifndef GUID_HIPART #define GUID_HIPART(guid) ObjectGuid(guid).GetHigh() #endif class Unit; class WorldObject; namespace ElunaUtil { uint32 GetCurrTime(); uint32 GetTimeDiff(uint32 oldMSTime); class ObjectGUIDCheck { public: ObjectGUIDCheck(ObjectGuid guid); bool operator()(WorldObject* object); ObjectGuid _guid; }; // Binary predicate to sort WorldObjects based on the distance to a reference WorldObject class ObjectDistanceOrderPred { public: ObjectDistanceOrderPred(WorldObject const* pRefObj, bool ascending = true); bool operator()(WorldObject const* pLeft, WorldObject const* pRight) const; WorldObject const* m_refObj; const bool m_ascending; }; // Doesn't get self class WorldObjectInRangeCheck { public: WorldObjectInRangeCheck(bool nearest, WorldObject const* obj, float range, uint16 typeMask = 0, uint32 entry = 0, uint32 hostile = 0); WorldObject const& GetFocusObject() const; bool operator()(WorldObject* u); bool i_nearest; WorldObject const* i_obj; float i_range; uint16 i_typeMask; uint32 i_entry; uint32 i_hostile; WorldObjectInRangeCheck(WorldObjectInRangeCheck const&); }; /* * Usage: * Inherit this class, then when needing lock, use * ReadGuard lock(_lock); * or * WriteGuard lock(_lock); * * The lock is automatically released at end of scope */ class RWLockable { public: #ifdef USING_BOOST typedef boost::shared_mutex LockType; typedef boost::shared_lock ReadGuard; typedef boost::unique_lock WriteGuard; #else typedef ACE_RW_Thread_Mutex LockType; typedef ACE_Read_Guard ReadGuard; typedef ACE_Write_Guard WriteGuard; #endif LockType& GetLock() { return _lock; } private: LockType _lock; }; }; #endif