TrinityCore
ObjectAccessor.h
Go to the documentation of this file.
1 /*
2  * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the
6  * Free Software Foundation; either version 2 of the License, or (at your
7  * option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef TRINITY_OBJECTACCESSOR_H
19 #define TRINITY_OBJECTACCESSOR_H
20 
21 #include "ObjectGuid.h"
22 #include <shared_mutex>
23 #include <unordered_map>
24 
25 class AreaTrigger;
26 class Conversation;
27 class Corpse;
28 class Creature;
29 class DynamicObject;
30 class GameObject;
31 class Map;
32 class Object;
33 class Pet;
34 class Player;
35 class SceneObject;
36 class Transport;
37 class Unit;
38 class WorldObject;
39 
40 template <class T>
42 {
43  //Non instanceable only static
45 
46 public:
47 
48  typedef std::unordered_map<ObjectGuid, T*> MapType;
49 
50  static void Insert(T* o);
51 
52  static void Remove(T* o);
53 
54  static T* Find(ObjectGuid guid);
55 
56  static MapType& GetContainer();
57 
58  static std::shared_mutex* GetLock();
59 };
60 
61 namespace ObjectAccessor
62 {
63  // these functions return objects only if in map of specified object
66  TC_GAME_API Corpse* GetCorpse(WorldObject const& u, ObjectGuid const& guid);
73  TC_GAME_API Unit* GetUnit(WorldObject const&, ObjectGuid const& guid);
74  TC_GAME_API Creature* GetCreature(WorldObject const& u, ObjectGuid const& guid);
75  TC_GAME_API Pet* GetPet(WorldObject const&, ObjectGuid const& guid);
76  TC_GAME_API Player* GetPlayer(Map const*, ObjectGuid const& guid);
77  TC_GAME_API Player* GetPlayer(WorldObject const&, ObjectGuid const& guid);
79 
80  // these functions return objects if found in whole world
81  // ACCESS LIKE THAT IS NOT THREAD SAFE
83  TC_GAME_API Player* FindPlayerByName(std::string_view name);
85 
86  // this returns Player even if he is not in world, for example teleporting
88  TC_GAME_API Player* FindConnectedPlayerByName(std::string_view name);
89 
90  // when using this, you must use the hashmapholder's lock
92 
93  template<class T>
94  void AddObject(T* object)
95  {
97  }
98 
99  template<class T>
100  void RemoveObject(T* object)
101  {
102  HashMapHolder<T>::Remove(object);
103  }
104 
105  template<>
106  void AddObject(Player* player);
107 
108  template<>
109  void RemoveObject(Player* player);
110 
112 };
113 
114 #endif
TC_GAME_API Pet * GetPet(WorldObject const &, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:222
Definition: Object.h:146
Definition: Corpse.h:51
void RemoveObject(Player *player)
Definition: ObjectAccessor.cpp:305
TC_GAME_API Unit * GetUnit(WorldObject const &, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:206
TC_GAME_API Player * GetPlayer(WorldObject const &, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:236
Player * Find(std::string_view name)
Definition: ObjectAccessor.cpp:90
uint64 LowType
Definition: ObjectGuid.h:269
TC_GAME_API Player * FindConnectedPlayerByName(std::string_view name)
Definition: ObjectAccessor.cpp:278
TC_GAME_API DynamicObject * GetDynamicObject(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:186
TC_GAME_API Player * FindPlayerByName(std::string_view name)
Definition: ObjectAccessor.cpp:258
Definition: Transport.h:29
Definition: Object.h:430
TC_GAME_API void SaveAllPlayers()
Definition: ObjectAccessor.cpp:288
Definition: Creature.h:69
Definition: ObjectAccessor.h:61
TC_GAME_API WorldObject * GetWorldObject(WorldObject const &, ObjectGuid const &)
Definition: ObjectAccessor.cpp:101
HashMapHolder()
Definition: ObjectAccessor.h:44
TC_GAME_API Transport * GetTransport(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:181
TC_GAME_API AreaTrigger * GetAreaTrigger(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:191
std::unordered_map< ObjectGuid, T * > MapType
Definition: ObjectAccessor.h:48
Definition: DynamicObject.h:36
TC_GAME_API Player * FindPlayer(ObjectGuid const &)
Definition: ObjectAccessor.cpp:252
static void Remove(T *o)
Definition: ObjectAccessor.cpp:43
uint32_t uint32
Definition: Define.h:143
TC_GAME_API Corpse * GetCorpse(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:171
void AddObject(Player *player)
Definition: ObjectAccessor.cpp:298
Definition: GameObject.h:125
TC_GAME_API SceneObject * GetSceneObject(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:196
Definition: Map.h:264
TC_GAME_API Creature * GetCreature(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:217
TC_GAME_API HashMapHolder< Player >::MapType const & GetPlayers()
Definition: ObjectAccessor.cpp:283
void Remove(Player *p)
Definition: ObjectAccessor.cpp:85
TC_GAME_API Creature * GetCreatureOrPetOrVehicle(WorldObject const &, ObjectGuid const &)
Definition: ObjectAccessor.cpp:241
Definition: Conversation.h:29
#define TC_GAME_API
Definition: Define.h:124
Definition: SceneObject.h:32
TC_GAME_API GameObject * GetGameObject(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:176
Definition: ObjectAccessor.h:41
TC_GAME_API Object * GetObjectByTypeMask(WorldObject const &, ObjectGuid const &, uint32 typemask)
Definition: ObjectAccessor.cpp:120
Definition: ObjectGuid.h:258
static void Insert(T *o)
Definition: ObjectAccessor.cpp:32
void Insert(Player *p)
Definition: ObjectAccessor.cpp:80
TC_GAME_API Player * FindPlayerByLowGUID(ObjectGuid::LowType lowguid)
Definition: ObjectAccessor.cpp:267
TC_GAME_API Conversation * GetConversation(WorldObject const &u, ObjectGuid const &guid)
Definition: ObjectAccessor.cpp:201
Definition: Unit.h:746
Definition: Player.h:1131
TC_GAME_API Player * FindConnectedPlayer(ObjectGuid const &)
Definition: ObjectAccessor.cpp:273
Definition: Pet.h:45
Definition: AreaTrigger.h:42