Skip to content

Commit 8cc4477

Browse files
committed
Added ElunaInfoKey
1 parent 18f0c16 commit 8cc4477

File tree

2 files changed

+90
-24
lines changed

2 files changed

+90
-24
lines changed

ElunaMgr.cpp

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void ElunaMgr::Create(Map* map, ElunaInfo const& info)
3131
_elunaMap.emplace(info.key, std::make_unique<Eluna>(map));
3232
}
3333

34-
Eluna* ElunaMgr::Get(uint64 key) const
34+
Eluna* ElunaMgr::Get(ElunaInfoKey key) const
3535
{
3636
auto it = _elunaMap.find(key);
3737
if (it != _elunaMap.end())
@@ -45,7 +45,7 @@ Eluna* ElunaMgr::Get(ElunaInfo const& info) const
4545
return Get(info.key);
4646
}
4747

48-
void ElunaMgr::Destroy(uint64 key)
48+
void ElunaMgr::Destroy(ElunaInfoKey key)
4949
{
5050
_elunaMap.erase(key);
5151
}
@@ -55,45 +55,75 @@ void ElunaMgr::Destroy(ElunaInfo const& info)
5555
Destroy(info.key);
5656
}
5757

58-
ElunaInfo::ElunaInfo(uint32 mapId, uint32 instanceId)
58+
ElunaInfoKey::ElunaInfoKey(uint32 mapId, uint32 instanceId)
5959
{
60-
key = MakeKey(mapId, instanceId);
60+
value = MakeKey(mapId, instanceId);
6161
}
6262

63-
ElunaInfo::~ElunaInfo()
63+
bool ElunaInfoKey::operator==(const ElunaInfoKey& other) const
6464
{
65-
if (IsValid() && sElunaMgr)
66-
sElunaMgr->Destroy(key);
65+
return value == other.value;
6766
}
6867

69-
constexpr uint64 ElunaInfo::MakeKey(uint32 mapId, uint32 instanceId)
68+
constexpr ElunaInfoKey ElunaInfoKey::MakeKey(uint32 mapId, uint32 instanceId)
7069
{
71-
return instanceId | (static_cast<uint64>(mapId) << 32);
70+
return ElunaInfoKey(instanceId | (static_cast<uint64>(mapId) << 32));
7271
}
7372

74-
constexpr uint64 ElunaInfo::MakeGlobalKey(uint32 instanceId)
73+
constexpr ElunaInfoKey ElunaInfoKey::MakeGlobalKey(uint32 instanceId)
7574
{
7675
return MakeKey(GLOBAL_MAP_ID, instanceId);
7776
}
7877

78+
bool ElunaInfoKey::IsValid() const
79+
{
80+
return value != INVALID_KEY_VALUE;
81+
}
82+
83+
bool ElunaInfoKey::IsGlobal() const
84+
{
85+
return IsValid() && GetMapId() == GLOBAL_MAP_ID;
86+
}
87+
88+
uint32 ElunaInfoKey::GetMapId() const
89+
{
90+
return value >> 32;
91+
}
92+
93+
uint32 ElunaInfoKey::GetInstanceId() const
94+
{
95+
return value & 0xFFFFFFFF;
96+
}
97+
98+
ElunaInfo::ElunaInfo(uint32 mapId, uint32 instanceId)
99+
{
100+
key = ElunaInfoKey::MakeKey(mapId, instanceId);
101+
}
102+
103+
ElunaInfo::~ElunaInfo()
104+
{
105+
if (IsValid() && sElunaMgr)
106+
sElunaMgr->Destroy(key);
107+
}
108+
79109
bool ElunaInfo::IsValid() const
80110
{
81-
return key != INVALID_KEY_ID;
111+
return key.IsValid();
82112
}
83113

84114
bool ElunaInfo::IsGlobal() const
85115
{
86-
return IsValid() && GetMapId() == GLOBAL_MAP_ID;
116+
return key.IsGlobal();
87117
}
88118

89119
uint32 ElunaInfo::GetMapId() const
90120
{
91-
return key >> 32;
121+
return key.GetMapId();
92122
}
93123

94124
uint32 ElunaInfo::GetInstanceId() const
95125
{
96-
return key & 0xFFFFFFFF;
126+
return key.GetInstanceId();
97127
}
98128

99129
Eluna* ElunaInfo::GetEluna() const

ElunaMgr.h

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,55 @@
1313

1414
class Eluna;
1515

16+
struct ElunaInfoKey
17+
{
18+
public:
19+
ElunaInfoKey() : value(INVALID_KEY_VALUE) {}
20+
ElunaInfoKey(uint64 key) : value(key) {}
21+
ElunaInfoKey(uint32 mapId, uint32 instanceId);
22+
~ElunaInfoKey() {}
23+
24+
bool operator==(const ElunaInfoKey& other) const;
25+
26+
public:
27+
static constexpr ElunaInfoKey MakeKey(uint32 mapId, uint32 instanceId);
28+
static constexpr ElunaInfoKey MakeGlobalKey(uint32 instanceId);
29+
30+
bool IsValid() const;
31+
bool IsGlobal() const;
32+
33+
uint32 GetMapId() const;
34+
uint32 GetInstanceId() const;
35+
36+
public:
37+
static uint64 const INVALID_KEY_VALUE = std::numeric_limits<uint64>().max();
38+
static uint32 const GLOBAL_MAP_ID = std::numeric_limits<uint32>().max();
39+
40+
uint64 value;
41+
};
42+
43+
// Specialize ElunaInfoKey for use in unordered_map
44+
namespace std
45+
{
46+
template <>
47+
struct hash<ElunaInfoKey>
48+
{
49+
size_t operator()(const ElunaInfoKey& key) const
50+
{
51+
return key.value;
52+
}
53+
};
54+
}
55+
1656
struct ElunaInfo
1757
{
1858
public:
59+
ElunaInfo() : key() {}
60+
ElunaInfo(ElunaInfoKey key) : key(key) {}
1961
ElunaInfo(uint32 mapId, uint32 instanceId);
2062
~ElunaInfo();
2163

2264
public:
23-
static constexpr uint64 MakeKey(uint32 mapId, uint32 instanceId);
24-
static constexpr uint64 MakeGlobalKey(uint32 instanceId);
25-
2665
bool IsValid() const;
2766
bool IsGlobal() const;
2867

@@ -33,10 +72,7 @@ struct ElunaInfo
3372
Eluna* GetEluna() const;
3473

3574
public:
36-
static uint64 const INVALID_KEY_ID = std::numeric_limits<uint64>().max();
37-
static uint32 const GLOBAL_MAP_ID = std::numeric_limits<uint32>().max();
38-
39-
uint64 key = INVALID_KEY_ID;
75+
ElunaInfoKey key;
4076
};
4177

4278
class ElunaMgr
@@ -52,14 +88,14 @@ class ElunaMgr
5288

5389
void Create(Map* map, ElunaInfo const& info);
5490

55-
Eluna* Get(uint64 key) const;
91+
Eluna* Get(ElunaInfoKey key) const;
5692
Eluna* Get(ElunaInfo const& info) const;
5793

58-
void Destroy(uint64 key);
94+
void Destroy(ElunaInfoKey key);
5995
void Destroy(ElunaInfo const& info);
6096

6197
private:
62-
std::unordered_map<uint64, std::unique_ptr<Eluna>> _elunaMap;
98+
std::unordered_map<ElunaInfoKey, std::unique_ptr<Eluna>> _elunaMap;
6399
};
64100

65101
#define sElunaMgr ElunaMgr::instance()

0 commit comments

Comments
 (0)