Problems with lambdas and references

This commit is contained in:
Kayne Ruse
2014-11-26 03:05:40 +11:00
parent bd3838a04e
commit fdaae0e8f2
21 changed files with 70 additions and 26 deletions
+12
View File
@@ -24,6 +24,18 @@
#include <stdexcept> #include <stdexcept>
#include <algorithm> #include <algorithm>
RegionPagerBase::RegionPagerBase() {
//
}
RegionPagerBase::RegionPagerBase(RegionPagerBase&& rhs) {
regionList = std::move(rhs.regionList);
}
RegionPagerBase::~RegionPagerBase() {
UnloadAll();
}
Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) { Region::type_t RegionPagerBase::SetTile(int x, int y, int z, Region::type_t v) {
Region* ptr = GetRegion(x, y); Region* ptr = GetRegion(x, y);
return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v); return ptr->SetTile(x - ptr->GetX(), y - ptr->GetY(), z, v);
+3 -2
View File
@@ -28,8 +28,9 @@
class RegionPagerBase { class RegionPagerBase {
public: public:
RegionPagerBase() = default; RegionPagerBase();
virtual ~RegionPagerBase() { UnloadAll(); }; RegionPagerBase(RegionPagerBase&&);
virtual ~RegionPagerBase();
//tile manipulation //tile manipulation
virtual Region::type_t SetTile(int x, int y, int z, Region::type_t v); virtual Region::type_t SetTile(int x, int y, int z, Region::type_t v);
+13
View File
@@ -23,6 +23,18 @@
#include <stdexcept> #include <stdexcept>
RegionPagerLua::RegionPagerLua() {
//
}
RegionPagerLua::RegionPagerLua(RegionPagerLua&& rhs) {
lua = rhs.lua;
loadRef = rhs.loadRef;
saveRef = rhs.saveRef;
createRef = rhs.createRef;
unloadRef = rhs.unloadRef;
}
RegionPagerLua::~RegionPagerLua() { RegionPagerLua::~RegionPagerLua() {
//unload all regions //unload all regions
UnloadAll(); UnloadAll();
@@ -164,6 +176,7 @@ void RegionPagerLua::UnloadRegion(int x, int y) {
lua_pop(lua, 1); lua_pop(lua, 1);
} }
//no return
void RegionPagerLua::UnloadAll() { void RegionPagerLua::UnloadAll() {
//get the pager's function from the registry //get the pager's function from the registry
lua_rawgeti(lua, LUA_REGISTRYINDEX, unloadRef); lua_rawgeti(lua, LUA_REGISTRYINDEX, unloadRef);
+2 -1
View File
@@ -34,7 +34,8 @@
class RegionPagerLua : public RegionPagerBase { class RegionPagerLua : public RegionPagerBase {
public: public:
RegionPagerLua() = default; RegionPagerLua();
RegionPagerLua(RegionPagerLua&&);
~RegionPagerLua(); ~RegionPagerLua();
//region manipulation //region manipulation
+1 -1
View File
@@ -200,7 +200,7 @@ void AccountManager::UnloadAll() {
elementMap.clear(); elementMap.clear();
} }
void AccountManager::UnloadIf(std::function<bool(std::pair<const int, AccountData>)> fn) { void AccountManager::UnloadIf(std::function<bool(std::pair<const int, AccountData>&)> fn) {
//replicate std::remove_if, using custom code //replicate std::remove_if, using custom code
std::map<int, AccountData>::iterator it = elementMap.begin(); std::map<int, AccountData>::iterator it = elementMap.begin();
while (it != elementMap.end()) { while (it != elementMap.end()) {
+1 -1
View File
@@ -48,7 +48,7 @@ public:
void Delete(int uid) override; void Delete(int uid) override;
void UnloadAll() override; void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, AccountData>)> fn) override; void UnloadIf(std::function<bool(std::pair<const int, AccountData>&)> fn) override;
//accessors and mutators //accessors and mutators
AccountData* Get(int uid) override; AccountData* Get(int uid) override;
+1 -1
View File
@@ -227,7 +227,7 @@ void CharacterManager::UnloadAll() {
elementMap.clear(); elementMap.clear();
} }
void CharacterManager::UnloadIf(std::function<bool(std::pair<const int, CharacterData>)> fn) { void CharacterManager::UnloadIf(std::function<bool(std::pair<const int, CharacterData>&)> fn) {
std::map<int, CharacterData>::iterator it = elementMap.begin(); std::map<int, CharacterData>::iterator it = elementMap.begin();
while (it != elementMap.end()) { while (it != elementMap.end()) {
if (fn(*it)) { if (fn(*it)) {
+1 -1
View File
@@ -48,7 +48,7 @@ public:
void Delete(int uid) override; void Delete(int uid) override;
void UnloadAll() override; void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, CharacterData>)> fn) override; void UnloadIf(std::function<bool(std::pair<const int, CharacterData>&)> fn) override;
//accessors and mutators //accessors and mutators
CharacterData* Get(int uid) override; CharacterData* Get(int uid) override;
+1 -1
View File
@@ -73,7 +73,7 @@ void ClientManager::UnloadAll() {
elementMap.clear(); elementMap.clear();
} }
void ClientManager::UnloadIf(std::function<bool(std::pair<const int, ClientData>)> fn) { void ClientManager::UnloadIf(std::function<bool(std::pair<const int, ClientData>&)> fn) {
std::map<int, ClientData>::iterator it = elementMap.begin(); std::map<int, ClientData>::iterator it = elementMap.begin();
while (it != elementMap.end()) { while (it != elementMap.end()) {
if (fn(*it)) { if (fn(*it)) {
+1 -1
View File
@@ -45,7 +45,7 @@ public:
void Unload(int uid) override; void Unload(int uid) override;
void UnloadAll() override; void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, ClientData>)> fn) override; void UnloadIf(std::function<bool(std::pair<const int, ClientData>&)> fn) override;
//accessors & mutators //accessors & mutators
ClientData* Get(int uid) override; ClientData* Get(int uid) override;
+1 -1
View File
@@ -45,7 +45,7 @@ void DoorManager::UnloadAll() {
//TODO //TODO
} }
void DoorManager::UnloadIf(std::function<bool(std::pair<const int, DoorData>)> fn) { void DoorManager::UnloadIf(std::function<bool(std::pair<const int, DoorData>&)> fn) {
//TODO //TODO
} }
+1 -1
View File
@@ -43,7 +43,7 @@ public:
void Delete(int uid) override; void Delete(int uid) override;
void UnloadAll() override; void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, DoorData>)> fn) override; void UnloadIf(std::function<bool(std::pair<const int, DoorData>&)> fn) override;
//accessors & mutators //accessors & mutators
DoorData* Get(int uid) override; DoorData* Get(int uid) override;
+1 -1
View File
@@ -45,7 +45,7 @@ void MonsterManager::UnloadAll() {
//TODO //TODO
} }
void MonsterManager::UnloadIf(std::function<bool(std::pair<const int, MonsterData>)> fn) { void MonsterManager::UnloadIf(std::function<bool(std::pair<const int, MonsterData>&)> fn) {
//TODO //TODO
} }
+1 -1
View File
@@ -50,7 +50,7 @@ public:
void Delete(int uid) override; void Delete(int uid) override;
void UnloadAll() override; void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int, MonsterData>)> fn) override; void UnloadIf(std::function<bool(std::pair<const int, MonsterData>&)> fn) override;
//accessors & mutators //accessors & mutators
MonsterData* Get(int uid) override; MonsterData* Get(int uid) override;
+16 -1
View File
@@ -21,6 +21,21 @@
*/ */
#include "room_data.hpp" #include "room_data.hpp"
RoomData::RoomData() {
//
}
RoomData::RoomData(RoomData& rhs) {
roomName = std::move(rhs.roomName);
tilesetName = std::move(rhs.tilesetName);
pager = std::move(rhs.pager);
//entityList = std::move(rhs.entityList);
}
RoomData::~RoomData() {
//
}
std::string RoomData::SetRoomName(std::string s) { std::string RoomData::SetRoomName(std::string s) {
return roomName = s; return roomName = s;
} }
@@ -42,5 +57,5 @@ RegionPagerLua* RoomData::GetPager() {
} }
std::list<Entity*>* RoomData::GetEntityList() { std::list<Entity*>* RoomData::GetEntityList() {
return &entityList; // return &entityList;
} }
+4 -3
View File
@@ -36,8 +36,9 @@
class RoomData { class RoomData {
public: public:
RoomData() = default; RoomData();
~RoomData() = default; RoomData(RoomData&);
~RoomData();
//accessors and mutators //accessors and mutators
std::string SetRoomName(std::string s); std::string SetRoomName(std::string s);
@@ -57,7 +58,7 @@ private:
std::string tilesetName; std::string tilesetName;
RegionPagerLua pager; RegionPagerLua pager;
std::list<Entity*> entityList; // std::list<Entity*> entityList;
}; };
#endif #endif
+3 -2
View File
@@ -33,7 +33,8 @@
int RoomManager::Create(std::string roomName) { int RoomManager::Create(std::string roomName) {
std::cout << "Create-1" << std::endl; std::cout << "Create-1" << std::endl;
//create the room //create the room
RoomData* newRoom = &elementMap[counter]; //implicitly constructs the element elementMap.emplace(counter); ////explicitly constructs the element
RoomData* newRoom = &(elementMap.find(counter)->second);
std::cout << "Create-2" << std::endl; std::cout << "Create-2" << std::endl;
newRoom->SetRoomName(roomName); newRoom->SetRoomName(roomName);
std::cout << "Create-3" << std::endl; std::cout << "Create-3" << std::endl;
@@ -59,7 +60,7 @@ void RoomManager::UnloadAll() {
elementMap.clear(); elementMap.clear();
} }
void RoomManager::UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn) { void RoomManager::UnloadIf(std::function<bool(std::pair<const int,RoomData>&)> fn) {
std::map<int, RoomData>::iterator it = elementMap.begin(); std::map<int, RoomData>::iterator it = elementMap.begin();
while (it != elementMap.end()) { while (it != elementMap.end()) {
if (fn(*it)) { if (fn(*it)) {
+1 -1
View File
@@ -42,7 +42,7 @@ public:
void Unload(int uid) override; void Unload(int uid) override;
void UnloadAll() override; void UnloadAll() override;
void UnloadIf(std::function<bool(std::pair<const int,RoomData>)> fn) override; void UnloadIf(std::function<bool(std::pair<const int,RoomData>&)> fn) override;
//accessors and mutators //accessors and mutators
RoomData* Get(int uid) override; RoomData* Get(int uid) override;
+2 -2
View File
@@ -175,10 +175,10 @@ void ServerApplication::Proc() {
int disconnected = clientMgr.CheckConnections(); int disconnected = clientMgr.CheckConnections();
if (disconnected != -1) { if (disconnected != -1) {
//find and unload the accounts associated with this client //find and unload the accounts associated with this client
accountMgr.UnloadIf([&](std::pair<const int, AccountData> account) -> bool { accountMgr.UnloadIf([&](std::pair<const int, AccountData>& account) -> bool {
if (account.second.GetClientIndex() == disconnected) { if (account.second.GetClientIndex() == disconnected) {
//find and unload the characters associated with this account //find and unload the characters associated with this account
characterMgr.UnloadIf([&](std::pair<const int, CharacterData> character) -> bool { characterMgr.UnloadIf([&](std::pair<const int, CharacterData>& character) -> bool {
if (character.second.GetOwner() == account.first) { if (character.second.GetOwner() == account.first) {
// PumpCharacterUnload(character.first); // PumpCharacterUnload(character.first);
return true; return true;
+3 -3
View File
@@ -139,7 +139,7 @@ void ServerApplication::HandleLogoutRequest(ClientPacket* const argPacket) {
network.SendTo(clientData->GetAddress(), static_cast<SerialPacket*>(&newPacket)); network.SendTo(clientData->GetAddress(), static_cast<SerialPacket*>(&newPacket));
//save and unload this accounts characters //save and unload this accounts characters
characterMgr.UnloadIf([&](std::pair<int, CharacterData> it) -> bool { characterMgr.UnloadIf([&](std::pair<const int, CharacterData>& it) -> bool {
if (argPacket->accountIndex == it.second.GetOwner()) { if (argPacket->accountIndex == it.second.GetOwner()) {
//pump the unload message to all remaining clients //pump the unload message to all remaining clients
// PumpCharacterUnload(it.first); // PumpCharacterUnload(it.first);
@@ -173,10 +173,10 @@ void ServerApplication::HandleDisconnectRequest(ClientPacket* const argPacket) {
//TODO: need a method for this redundunt chunk of redundant code //TODO: need a method for this redundunt chunk of redundant code
//find and unload the accounts associated with this client //find and unload the accounts associated with this client
accountMgr.UnloadIf([&](std::pair<const int, AccountData> account) -> bool { accountMgr.UnloadIf([&](std::pair<const int, AccountData>& account) -> bool {
if (account.second.GetClientIndex() == argPacket->clientIndex) { if (account.second.GetClientIndex() == argPacket->clientIndex) {
//find and unload the characters associated with this account //find and unload the characters associated with this account
characterMgr.UnloadIf([&](std::pair<const int, CharacterData> character) -> bool { characterMgr.UnloadIf([&](std::pair<const int, CharacterData>& character) -> bool {
if (character.second.GetOwner() == account.first) { if (character.second.GetOwner() == account.first) {
// PumpCharacterUnload(character.first); // PumpCharacterUnload(character.first);
return true; return true;
@@ -36,7 +36,7 @@ public:
virtual void Delete(int uid) = 0; virtual void Delete(int uid) = 0;
virtual void UnloadAll() = 0; virtual void UnloadAll() = 0;
virtual void UnloadIf(std::function<bool(std::pair<const int, T>)> fn) = 0; virtual void UnloadIf(std::function<bool(std::pair<const int, T>&)> fn) = 0;
//accessors & mutators //accessors & mutators
virtual T* Get(int uid) = 0; virtual T* Get(int uid) = 0;