Skip to content

Commit

Permalink
GunManager: take StringName by reference
Browse files Browse the repository at this point in the history
- The StringName values are borrowed rather than copied, which saves a small fixed cost to increment the refcount
  • Loading branch information
sturnclaw committed Dec 7, 2024
1 parent 2266dcf commit e78994b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions src/ship/GunManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void GunManager::LoadFromJson(const Json &jsonObj, Space *space)
}
}

bool GunManager::AddWeaponMount(StringName id, StringName tagName, vector2f gimbalLimit)
bool GunManager::AddWeaponMount(const StringName &id, const StringName &tagName, vector2f gimbalLimit)
{
WeaponMount mount = {};

Expand All @@ -133,7 +133,7 @@ bool GunManager::AddWeaponMount(StringName id, StringName tagName, vector2f gimb
return result.second;
}

bool GunManager::RemoveWeaponMount(StringName id)
bool GunManager::RemoveWeaponMount(const StringName &id)
{
auto iter = m_mounts.find(id);
if (iter == m_mounts.end())
Expand All @@ -146,7 +146,7 @@ bool GunManager::RemoveWeaponMount(StringName id)
return true;
}

bool GunManager::MountWeapon(StringName hardpoint, const WeaponData &gunData)
bool GunManager::MountWeapon(const StringName &hardpoint, const WeaponData &gunData)
{
auto iter = m_mounts.find(hardpoint);
if (iter == m_mounts.end())
Expand All @@ -173,7 +173,7 @@ bool GunManager::MountWeapon(StringName hardpoint, const WeaponData &gunData)
return true;
}

void GunManager::UnmountWeapon(StringName hardpoint)
void GunManager::UnmountWeapon(const StringName &hardpoint)
{
auto iter = std::find_if(m_weapons.begin(), m_weapons.end(), [&](const WeaponState &ws) {
return ws.mount->id == hardpoint;
Expand All @@ -192,7 +192,7 @@ void GunManager::UnmountWeapon(StringName hardpoint)
}
}

bool GunManager::IsWeaponMounted(StringName hardpoint) const
bool GunManager::IsWeaponMounted(const StringName &hardpoint) const
{
return std::find_if(m_weapons.begin(), m_weapons.end(), [&](const WeaponState &ws) {
return ws.mount->id == hardpoint;
Expand All @@ -210,7 +210,7 @@ void GunManager::RemoveGroupIndex(WeaponIndexSet &group, uint32_t index)
group = (group & keep) | ((group & mask) >> 1);
}

uint32_t GunManager::GetWeaponIndexForHardpoint(StringName hardpoint) const
uint32_t GunManager::GetWeaponIndexForHardpoint(const StringName &hardpoint) const
{
auto iter = std::find_if(m_weapons.begin(), m_weapons.end(), [&](const WeaponState &ws) {
return ws.mount->id == hardpoint;
Expand Down
12 changes: 6 additions & 6 deletions src/ship/GunManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,25 +139,25 @@ class GunManager : public LuaWrappable {

// Add a weapon mount to this gun manager.
// Returns false if a hardpoint already exists on this GunManager with the specified name.
bool AddWeaponMount(StringName id, StringName tagName, vector2f gimbalLimitDegrees);
bool AddWeaponMount(const StringName &id, const StringName &tagName, vector2f gimbalLimitDegrees);
// Remove a weapon mount from this gun manager.
// The caller should always ensure that the weapon mount is empty before calling this function.
// Returns false if the mount does not exist or is not empty.
bool RemoveWeaponMount(StringName id);
bool RemoveWeaponMount(const StringName &id);

// Attach a weapon to a specific mount.
// Returns false if the hardpoint cannot be found or the weapon could not be mounted.
bool MountWeapon(StringName hardpoint, const WeaponData &data);
bool MountWeapon(const StringName &hardpoint, const WeaponData &data);
// Remove the attached weapon from a specific mount
void UnmountWeapon(StringName hardpoint);
void UnmountWeapon(const StringName &hardpoint);
// Check if any weapon is attached to a specific mount
bool IsWeaponMounted(StringName hardpoint) const;
bool IsWeaponMounted(const StringName &hardpoint) const;

const std::vector<WeaponState> &GetWeapons() const { return m_weapons; }

uint32_t GetNumWeapons() const { return m_weapons.size(); }
const WeaponState *GetWeaponState(uint32_t numWeapon) const { return m_weapons.size() > numWeapon ? &m_weapons[numWeapon] : nullptr; }
uint32_t GetWeaponIndexForHardpoint(StringName hardpoint) const;
uint32_t GetWeaponIndexForHardpoint(const StringName &hardpoint) const;

// ==========================================

Expand Down

0 comments on commit e78994b

Please sign in to comment.