Skip to content

Commit

Permalink
[Enhancement] refactored code and fixes compilation warn from Ubuntu …
Browse files Browse the repository at this point in the history
…23.04 (#726)

Code refactoring to not be necessary to use vector "clear()" and "shrink_to_fit()" with that I fixed the warnings generated by the GCC compiler in Ubuntu 23.04!
  • Loading branch information
beats-dh authored Dec 30, 2022
1 parent 53651cd commit c61555b
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 66 deletions.
83 changes: 45 additions & 38 deletions src/lua/creature/actions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,22 +216,25 @@ bool Actions::registerLuaItemEvent(Action* action) {
return false;
}

std::for_each(itemIdVector.begin(), itemIdVector.end(), [this, &action, &itemIdVector](uint16_t &itemId) {
std::vector<uint16_t> tmpVector;
tmpVector.reserve(itemIdVector.size());

for (const auto& itemId : itemIdVector) {
// Check if the item is already registered and prevent it from being registered again
if (hasItemId(itemId)) {
SPDLOG_WARN("[Actions::registerLuaItemEvent] - Duplicate "
"registered item with id: {} in range from id: {}, to id: {}",
itemId, itemIdVector.at(0), itemIdVector.at(itemIdVector.size() - 1));
return false;
continue;
}

// Register item in the action item map
setItemId(itemId, std::move(*action));
return true;
});
itemIdVector.clear();
itemIdVector.shrink_to_fit();
return true;
tmpVector.emplace_back(itemId);
}

itemIdVector = std::move(tmpVector);
return !itemIdVector.empty();
}

bool Actions::registerLuaUniqueEvent(Action* action) {
Expand All @@ -240,23 +243,24 @@ bool Actions::registerLuaUniqueEvent(Action* action) {
return false;
}

std::for_each(uniqueIdVector.begin(), uniqueIdVector.end(), [this, &action, &uniqueIdVector](uint16_t &uniqueId) {
std::vector<uint16_t> tmpVector;
tmpVector.reserve(uniqueIdVector.size());

for (const auto& uniqueId : uniqueIdVector) {
// Check if the unique is already registered and prevent it from being registered again
if (hasUniqueId(uniqueId)) {
if (!hasUniqueId(uniqueId)) {
// Register unique id the unique item map
setUniqueId(uniqueId, std::move(*action));
tmpVector.emplace_back(uniqueId);
} else {
SPDLOG_WARN("[Actions::registerLuaUniqueEvent] - Duplicate "
"registered item with uid: {} in range from uid: {}, to uid: {}",
uniqueId, uniqueIdVector.at(0), uniqueIdVector.at(uniqueIdVector.size() - 1));
return false;
}
}

// Register unique id the unique item map
setUniqueId(uniqueId, std::move(*action));
return true;
});

uniqueIdVector.clear();
uniqueIdVector.shrink_to_fit();
return true;
uniqueIdVector = std::move(tmpVector);
return !uniqueIdVector.empty();
}

bool Actions::registerLuaActionEvent(Action* action) {
Expand All @@ -265,23 +269,24 @@ bool Actions::registerLuaActionEvent(Action* action) {
return false;
}

std::for_each(actionIdVector.begin(), actionIdVector.end(), [this, &action, &actionIdVector](uint16_t &actionId) {
std::vector<uint16_t> tmpVector;
tmpVector.reserve(actionIdVector.size());

for (const auto& actionId : actionIdVector) {
// Check if the unique is already registered and prevent it from being registered again
if (hasActionId(actionId)) {
if (!hasActionId(actionId)) {
// Register action in the action item map
setActionId(actionId, std::move(*action));
tmpVector.emplace_back(actionId);
} else {
SPDLOG_WARN("[Actions::registerLuaActionEvent] - Duplicate "
"registered item with aid: {} in range from aid: {}, to aid: {}",
actionId, actionIdVector.at(0), actionIdVector.at(actionIdVector.size() - 1));
return false;
}
}

// Register action in the action item map
setActionId(actionId, std::move(*action));
return true;
});

actionIdVector.clear();
actionIdVector.shrink_to_fit();
return true;
actionIdVector = std::move(tmpVector);
return !actionIdVector.empty();
}

bool Actions::registerLuaPositionEvent(Action* action) {
Expand All @@ -290,21 +295,23 @@ bool Actions::registerLuaPositionEvent(Action* action) {
return false;
}

for (Position position : positionVector) {
std::vector<Position> tmpVector;
tmpVector.reserve(positionVector.size());

for (const auto& position : positionVector) {
// Check if the position is already registered and prevent it from being registered again
if (hasPosition(position)) {
if (!hasPosition(position)) {
// Register position in the action position map
setPosition(position, std::move(*action));
tmpVector.emplace_back(position);
} else {
SPDLOG_WARN("[Actions::registerLuaPositionEvent] - Duplicate "
"registered script with range position: {}", position.toString());
continue;
}

// Register position in the action position map
setPosition(position, std::move(*action));
}

positionVector.clear();
positionVector.shrink_to_fit();
return true;
positionVector = std::move(tmpVector);
return !positionVector.empty();
}

bool Actions::registerLuaEvent(Action* event) {
Expand Down
75 changes: 49 additions & 26 deletions src/lua/creature/movement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,24 @@ bool MoveEvents::registerLuaItemEvent(MoveEvent& moveEvent) {
return false;
}

std::for_each(itemIdVector.begin(), itemIdVector.end(), [this, &moveEvent](const uint32_t &itemId) {
std::vector<uint32_t> tmpVector;
tmpVector.reserve(itemIdVector.size());

for (const auto& itemId : itemIdVector) {
if (moveEvent.getEventType() == MOVE_EVENT_EQUIP) {
ItemType& it = Item::items.getItemType(itemId);
it.wieldInfo = moveEvent.getWieldInfo();
it.minReqLevel = moveEvent.getReqLevel();
it.minReqMagicLevel = moveEvent.getReqMagLv();
it.vocationString = moveEvent.getVocationString();
}
return registerEvent(moveEvent, itemId, itemIdMap);
});
itemIdVector.clear();
itemIdVector.shrink_to_fit();
return true;
if (registerEvent(moveEvent, itemId, itemIdMap)) {
tmpVector.emplace_back(itemId);
}
}

itemIdVector = std::move(tmpVector);
return !itemIdVector.empty();
}

bool MoveEvents::registerLuaActionEvent(MoveEvent& moveEvent) {
Expand All @@ -62,13 +67,17 @@ bool MoveEvents::registerLuaActionEvent(MoveEvent& moveEvent) {
return false;
}

std::for_each(actionIdVector.begin(), actionIdVector.end(), [this, &moveEvent](const uint32_t &actionId) {
return registerEvent(moveEvent, actionId, actionIdMap);
});
std::vector<uint32_t> tmpVector;
tmpVector.reserve(actionIdVector.size());

actionIdVector.clear();
actionIdVector.shrink_to_fit();
return true;
for (const auto& actionId : actionIdVector) {
if (registerEvent(moveEvent, actionId, actionIdMap)) {
tmpVector.emplace_back(actionId);
}
}

actionIdVector = std::move(tmpVector);
return !actionIdVector.empty();
}

bool MoveEvents::registerLuaUniqueEvent(MoveEvent& moveEvent) {
Expand All @@ -77,13 +86,17 @@ bool MoveEvents::registerLuaUniqueEvent(MoveEvent& moveEvent) {
return false;
}

std::for_each(uniqueIdVector.begin(), uniqueIdVector.end(), [this, &moveEvent](const uint32_t &uniqueId) {
return registerEvent(moveEvent, uniqueId, uniqueIdMap);
});
std::vector<uint32_t> tmpVector;
tmpVector.reserve(uniqueIdVector.size());

uniqueIdVector.clear();
uniqueIdVector.shrink_to_fit();
return true;
for (const auto& uniqueId : uniqueIdVector) {
if (registerEvent(moveEvent, uniqueId, uniqueIdMap)) {
tmpVector.emplace_back(uniqueId);
}
}

uniqueIdVector = std::move(tmpVector);
return !uniqueIdVector.empty();
}

bool MoveEvents::registerLuaPositionEvent(MoveEvent& moveEvent) {
Expand All @@ -92,13 +105,17 @@ bool MoveEvents::registerLuaPositionEvent(MoveEvent& moveEvent) {
return false;
}

std::for_each(positionVector.begin(), positionVector.end(), [this, &moveEvent](const Position &position) {
return registerEvent(moveEvent, position, positionsMap);
});
std::vector<Position> tmpVector;
tmpVector.reserve(positionVector.size());

for (const auto& position : positionVector) {
if (registerEvent(moveEvent, position, positionsMap)) {
tmpVector.emplace_back(position);
}
}

positionVector.clear();
positionVector.shrink_to_fit();
return true;
positionVector = std::move(tmpVector);
return !positionVector.empty();
}

bool MoveEvents::registerLuaEvent(MoveEvent& moveEvent) {
Expand All @@ -118,21 +135,24 @@ bool MoveEvents::registerLuaEvent(MoveEvent& moveEvent) {
return false;
}

void MoveEvents::registerEvent(MoveEvent& moveEvent, int32_t id, std::map<int32_t, MoveEventList>& moveListMap) const {
bool MoveEvents::registerEvent(MoveEvent& moveEvent, int32_t id, std::map<int32_t, MoveEventList>& moveListMap) const {
auto it = moveListMap.find(id);
if (it == moveListMap.end()) {
MoveEventList moveEventList;
moveEventList.moveEvent[moveEvent.getEventType()].push_back(std::move(moveEvent));
moveListMap[id] = moveEventList;
return true;
} else {
std::list<MoveEvent>& moveEventList = it->second.moveEvent[moveEvent.getEventType()];
for (MoveEvent& existingMoveEvent : moveEventList) {
if (existingMoveEvent.getSlot() == moveEvent.getSlot()) {
SPDLOG_WARN("[MoveEvents::registerEvent] - "
"Duplicate move event found: {}", id);
return false;
}
}
moveEventList.push_back(std::move(moveEvent));
return true;
}
}

Expand Down Expand Up @@ -208,20 +228,23 @@ MoveEvent* MoveEvents::getEvent(Item& item, MoveEvent_t eventType) {
return nullptr;
}

void MoveEvents::registerEvent(MoveEvent& moveEvent, const Position& position, std::map<Position, MoveEventList>& moveListMap) const {
bool MoveEvents::registerEvent(MoveEvent& moveEvent, const Position& position, std::map<Position, MoveEventList>& moveListMap) const {
auto it = moveListMap.find(position);
if (it == moveListMap.end()) {
MoveEventList moveEventList;
moveEventList.moveEvent[moveEvent.getEventType()].push_back(std::move(moveEvent));
moveListMap[position] = moveEventList;
return true;
} else {
std::list<MoveEvent>& moveEventList = it->second.moveEvent[moveEvent.getEventType()];
if (!moveEventList.empty()) {
SPDLOG_WARN("[MoveEvents::registerEvent] - "
"Duplicate move event found: {}", position.toString());
return false;
}

moveEventList.push_back(std::move(moveEvent));
return true;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/lua/creature/movement.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,8 @@ class MoveEvents final : public BaseEvents {
return false;
}

void registerEvent(MoveEvent& moveEvent, int32_t id, std::map<int32_t, MoveEventList>& moveListMap) const;
void registerEvent(MoveEvent& moveEvent, const Position& position, std::map<Position, MoveEventList>& moveListMap) const;
bool registerEvent(MoveEvent& moveEvent, int32_t id, std::map<int32_t, MoveEventList>& moveListMap) const;
bool registerEvent(MoveEvent& moveEvent, const Position& position, std::map<Position, MoveEventList>& moveListMap) const;
MoveEvent* getEvent(Tile& tile, MoveEvent_t eventType);

MoveEvent* getEvent(Item& item, MoveEvent_t eventType, Slots_t slot);
Expand Down

0 comments on commit c61555b

Please sign in to comment.