TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ObjectAccessor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
3  * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #ifndef TRINITY_OBJECTACCESSOR_H
20 #define TRINITY_OBJECTACCESSOR_H
21 
22 #include <mutex>
23 #include <set>
24 #include <unordered_map>
25 #include <boost/thread/locks.hpp>
26 #include <boost/thread/shared_mutex.hpp>
27 
28 #include "Define.h"
29 #include "GridDefines.h"
30 #include "UpdateData.h"
31 #include "Object.h"
32 
33 class Creature;
34 class Corpse;
35 class Unit;
36 class GameObject;
37 class DynamicObject;
38 class WorldObject;
39 class Vehicle;
40 class Map;
41 class WorldRunnable;
42 class Transport;
43 
44 template <class T>
46 {
47  //Non instanceable only static
49 
50 public:
51  static_assert(std::is_same<Player, T>::value
53  "Only Player and Transport can be registered in global HashMapHolder");
54 
55  typedef std::unordered_map<ObjectGuid, T*> MapType;
56 
57  static void Insert(T* o);
58 
59  static void Remove(T* o);
60 
61  static T* Find(ObjectGuid guid);
62 
63  static MapType& GetContainer();
64 
65  static boost::shared_mutex* GetLock();
66 };
67 
68 namespace ObjectAccessor
69 {
70  // these functions return objects only if in map of specified object
73  TC_GAME_API Corpse* GetCorpse(WorldObject const& u, ObjectGuid const& guid);
78  TC_GAME_API Unit* GetUnit(WorldObject const&, ObjectGuid const& guid);
79  TC_GAME_API Creature* GetCreature(WorldObject const& u, ObjectGuid const& guid);
80  TC_GAME_API Pet* GetPet(WorldObject const&, ObjectGuid const& guid);
81  TC_GAME_API Player* GetPlayer(Map const*, ObjectGuid const& guid);
82  TC_GAME_API Player* GetPlayer(WorldObject const&, ObjectGuid const& guid);
84 
85  // these functions return objects if found in whole world
86  // ACCESS LIKE THAT IS NOT THREAD SAFE
88  TC_GAME_API Player* FindPlayerByName(std::string const& name);
89 
90  // this returns Player even if he is not in world, for example teleporting
92  TC_GAME_API Player* FindConnectedPlayerByName(std::string const& name);
93 
94  // when using this, you must use the hashmapholder's lock
96 
97  template<class T>
98  void AddObject(T* object)
99  {
100  HashMapHolder<T>::Insert(object);
101  }
102 
103  template<class T>
104  void RemoveObject(T* object)
105  {
106  HashMapHolder<T>::Remove(object);
107  }
108 
110 };
111 
112 #endif
113 
TC_GAME_API Pet * GetPet(WorldObject const &, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:179
Definition: Object.h:95
bool Remove(ContainerUnorderedMap< SPECIFIC_TYPE, KEY_TYPE > &elements, KEY_TYPE const &handle, SPECIFIC_TYPE *)
Definition: TypeContainerFunctions.h:104
Definition: Corpse.h:49
Definition: Vehicle.h:32
TC_GAME_API Unit * GetUnit(WorldObject const &, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:163
TC_GAME_API Player * FindConnectedPlayerByName(std::string const &name)
Definition: ObjectAccessor.cpp:240
TC_GAME_API DynamicObject * GetDynamicObject(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:153
Definition: Transport.h:28
Definition: Object.h:423
TC_GAME_API void SaveAllPlayers()
Definition: ObjectAccessor.cpp:263
Definition: Creature.h:467
Definition: ObjectAccessor.h:68
TC_GAME_API WorldObject * GetWorldObject(WorldObject const &, ObjectGuid const &)
Definition: ObjectAccessor.cpp:79
HashMapHolder()
Definition: ObjectAccessor.h:48
TC_GAME_API Transport * GetTransport(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:148
TC_GAME_API HashMapHolder< Player >::MapType const & GetPlayers()
Definition: ObjectAccessor.cpp:258
TC_GAME_API AreaTrigger * GetAreaTrigger(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:158
Definition: DynamicObject.h:35
TC_GAME_API Player * FindPlayer(ObjectGuid const &)
Definition: ObjectAccessor.cpp:209
TC_GAME_API Player * GetPlayer(Map const *, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:184
void AddObject(T *object)
Definition: ObjectAccessor.h:98
uint32_t uint32
Definition: Define.h:150
TC_GAME_API Corpse * GetCorpse(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:138
Definition: GameObject.h:880
TC_GAME_API Player * FindPlayerByName(std::string const &name)
Definition: ObjectAccessor.cpp:220
bool Insert(ContainerUnorderedMap< SPECIFIC_TYPE, KEY_TYPE > &elements, KEY_TYPE const &handle, SPECIFIC_TYPE *obj)
Definition: TypeContainerFunctions.h:38
Definition: Map.h:259
TC_GAME_API Creature * GetCreature(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:174
SPECIFIC_TYPE * Find(ContainerUnorderedMap< SPECIFIC_TYPE, KEY_TYPE > const &elements, KEY_TYPE const &handle, SPECIFIC_TYPE *)
Definition: TypeContainerFunctions.h:74
TC_GAME_API Creature * GetCreatureOrPetOrVehicle(WorldObject const &, ObjectGuid const &)
Definition: ObjectAccessor.cpp:198
#define TC_GAME_API
Definition: Define.h:134
TC_GAME_API GameObject * GetGameObject(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:143
Definition: ObjectAccessor.h:45
TC_GAME_API Object * GetObjectByTypeMask(WorldObject const &, ObjectGuid const &, uint32 typemask)
Definition: ObjectAccessor.cpp:96
const FieldDescriptor value
Definition: descriptor.h:1522
Definition: ObjectGuid.h:189
Definition: Unit.h:1305
TC_GAME_API Player * FindConnectedPlayer(ObjectGuid const &)
Definition: ObjectAccessor.cpp:215
void RemoveObject(T *object)
Definition: ObjectAccessor.h:104
Definition: Pet.h:46
Definition: AreaTrigger.h:26