1
0
mirror of https://github.com/VCMP-SqMod/SqMod.git synced 2024-11-08 00:37:15 +01:00

Add iteration.

This commit is contained in:
Sandu Liviu Catalin 2021-02-05 15:42:27 +02:00
parent bd1504bd24
commit 479272d59f
6 changed files with 125 additions and 0 deletions

View File

@ -342,6 +342,8 @@ bool PvManager::HaveUnitWithTag(StackStrF & tag)
// ------------------------------------------------------------------------------------------------
void PvManager::RemoveEntryWithID(SQInteger id)
{
ModifyEntries();
// Remove this entry from the units
for (const auto & u : m_Units)
{
@ -359,6 +361,8 @@ void PvManager::RemoveEntryWithID(SQInteger id)
// ------------------------------------------------------------------------------------------------
void PvManager::RemoveEntryWithTag(StackStrF & tag)
{
ModifyEntries();
PvEntry & e = *GetValidEntryWithTag(tag);
// Remove this entry from the units
for (const auto & u : m_Units)
@ -383,6 +387,8 @@ void PvManager::RemoveEntryWithTag(StackStrF & tag)
// ------------------------------------------------------------------------------------------------
void PvManager::RemoveClassWithID(SqPvClass & sub, SQInteger id)
{
ModifyClasses();
PvClass::Ref cls = sub.mI.lock();
// Remove this entry from units
for (const auto & u : m_Units)
@ -409,6 +415,8 @@ void PvManager::RemoveClassWithID(SqPvClass & sub, SQInteger id)
// ------------------------------------------------------------------------------------------------
void PvManager::RemoveClassWithTag(SqPvClass & sub, StackStrF & tag)
{
ModifyClasses();
PvClass::Ref cls = sub.mI.lock();
// Remove this class from units
for (const auto & u : m_Units)
@ -441,6 +449,8 @@ void PvManager::RemoveClassWithTag(SqPvClass & sub, StackStrF & tag)
// ------------------------------------------------------------------------------------------------
void PvManager::RemoveUnitWithID(SQInteger id)
{
ModifyUnits();
// Remove this class from classes
for (const auto & c : m_Classes)
{
@ -453,6 +463,8 @@ void PvManager::RemoveUnitWithID(SQInteger id)
// ------------------------------------------------------------------------------------------------
void PvManager::RemoveUnitWithTag(StackStrF & tag)
{
ModifyUnits();
PvUnit & u = *GetValidUnitWithTag(tag);
// Remove this class from classes
for (const auto & c : m_Classes)
@ -469,6 +481,42 @@ void PvManager::RemoveUnitWithTag(StackStrF & tag)
}
}
// ------------------------------------------------------------------------------------------------
void PvManager::EachEntryID(Object & ctx, Function & func)
{
// Prevent any changes to entries during this operation
AutoAssign< bool > aag(m_LockEntries, false, true);
// Iterate entries and forward the ID to the callback
for (const auto & e : m_Entries)
{
func(ctx, e.second->mID);
}
}
// ------------------------------------------------------------------------------------------------
void PvManager::EachClassID(Object & ctx, Function & func)
{
// Prevent any changes to classes during this operation
AutoAssign< bool > aag(m_LockClasses, false, true);
// Iterate classes and forward the ID to the callback
for (const auto & c : m_Classes)
{
func(ctx, c.second->mID);
}
}
// ------------------------------------------------------------------------------------------------
void PvManager::EachUnitID(Object & ctx, Function & func)
{
// Prevent any changes to units during this operation
AutoAssign< bool > aag(m_LockUnits, false, true);
// Iterate units and forward the ID to the callback
for (const auto & u : m_Units)
{
func(ctx, u.second->mID);
}
}
// ------------------------------------------------------------------------------------------------
extern void Register_Privilege_Class(HSQUIRRELVM vm, Table & ns);
extern void Register_Privilege_Entry(HSQUIRRELVM vm, Table & ns);
@ -523,6 +571,9 @@ void Register_Privilege(HSQUIRRELVM vm)
.FmtFunc(_SC("RemoveEntryWithTag"), &PvManager::RemoveEntryWithTag)
.Func(_SC("RemoveClass"), &PvManager::RemoveClassWithID)
.FmtFunc(_SC("RemoveClassWithTag"), &PvManager::RemoveClassWithTag)
.FmtFunc(_SC("EachEntryID"), &PvManager::EachEntryID)
.FmtFunc(_SC("EachClassID"), &PvManager::EachClassID)
.FmtFunc(_SC("EachUnitID"), &PvManager::EachUnitID)
);
RootTable(vm).Bind(_SC("SqPrivilege"), ns);

View File

@ -646,6 +646,21 @@ public:
* Remove a unit with a certain tag from this manager.
*/
void RemoveUnitWithTag(StackStrF & tag);
/* --------------------------------------------------------------------------------------------
* Invoke a given callback with every known entry identifier.
*/
void EachEntryID(Object & ctx, Function & func);
/* --------------------------------------------------------------------------------------------
* Invoke a given callback with every known class identifier.
*/
void EachClassID(Object & ctx, Function & func);
/* --------------------------------------------------------------------------------------------
* Invoke a given callback with every known unit identifier.
*/
void EachUnitID(Object & ctx, Function & func);
};
} // Namespace:: SqMod

View File

@ -390,6 +390,30 @@ bool PvClass::HaveUnitWithTag(StackStrF & tag)
return false;
}
// ------------------------------------------------------------------------------------------------
void PvClass::EachEntryID(Object & ctx, Function & func)
{
// In order to be safe from modifications while iterating, create a copy
PvStatusList list(mPrivileges);
// Iterate entries and forward the ID to the callback
for (const auto & e : list)
{
func(ctx, e.first);
}
}
// ------------------------------------------------------------------------------------------------
void PvClass::EachUnitID(Object & ctx, Function & func)
{
// In order to be safe from modifications while iterating, create a copy
PvUnit::List list(mUnits);
// Iterate units and forward the ID to the callback
for (const auto & u : list)
{
func(ctx, u.second->mID);
}
}
// ================================================================================================
bool SqPvClass::Can(LightObj & obj) const
{
@ -462,6 +486,8 @@ void Register_Privilege_Class(HSQUIRRELVM vm, Table & ns)
.FmtFunc(_SC("GetUnitWithTag"), &SqPvClass::GetUnitWithTag)
.Func(_SC("HaveUnit"), &SqPvClass::HaveUnitWithID)
.FmtFunc(_SC("HaveUnitWithTag"), &SqPvClass::HaveUnitWithTag)
.FmtFunc(_SC("EachEntryID"), &SqPvClass::EachEntryID)
.FmtFunc(_SC("EachUnitID"), &SqPvClass::EachUnitID)
);
}

View File

@ -296,6 +296,16 @@ struct PvClass
* See if a unit with a certain tag inherits this class.
*/
bool HaveUnitWithTag(StackStrF & tag);
/* --------------------------------------------------------------------------------------------
* Invoke a given callback with every owned entry identifier.
*/
void EachEntryID(Object & ctx, Function & func);
/* --------------------------------------------------------------------------------------------
* Invoke a given callback with every parented unit identifier.
*/
void EachUnitID(Object & ctx, Function & func);
};
/* ------------------------------------------------------------------------------------------------
@ -358,6 +368,9 @@ struct SqPvClass
SQMOD_NODISCARD LightObj GetUnitWithTag(StackStrF & tag) const { return Valid().GetUnitWithTag(tag); }
SQMOD_NODISCARD bool HaveUnitWithID(SQInteger id) const { return Valid().HaveUnitWithID(id); }
SQMOD_NODISCARD bool HaveUnitWithTag(StackStrF & tag) const { return Valid().HaveUnitWithTag(tag); }
// --------------------------------------------------------------------------------------------
void EachEntryID(Object & ctx, Function & func) { return Valid().EachEntryID(ctx, func); }
void EachUnitID(Object & ctx, Function & func) { return Valid().EachUnitID(ctx, func); }
};
} // Namespace:: SqMod

View File

@ -342,6 +342,18 @@ bool PvUnit::Can(SQInteger id) const
return false;
}
// ------------------------------------------------------------------------------------------------
void PvUnit::EachEntryID(Object & ctx, Function & func)
{
// In order to be safe from modifications while iterating, create a copy
PvStatusList list(mPrivileges);
// Iterate entries and forward the ID to the callback
for (const auto & e : list)
{
func(ctx, e.first);
}
}
// ================================================================================================
LightObj SqPvUnit::GetClass() const
{
@ -429,6 +441,7 @@ void Register_Privilege_Unit(HSQUIRRELVM vm, Table & ns)
.Func(_SC("Modify"), &SqPvUnit::ModifyPrivilegeWithID)
.FmtFunc(_SC("ModifyWithTag"), &SqPvUnit::ModifyPrivilegeWithTag)
.Func(_SC("RemoveAll"), &SqPvUnit::RemoveAllPrivileges)
.Func(_SC("EachEntryID"), &SqPvUnit::EachEntryID)
);
}

View File

@ -249,6 +249,11 @@ struct PvUnit
* Check if this unit has a certain privilege.
*/
SQMOD_NODISCARD bool Can(SQInteger id) const;
/* --------------------------------------------------------------------------------------------
* Invoke a given callback with every owned entry identifier.
*/
void EachEntryID(Object & ctx, Function & func);
};
/* ------------------------------------------------------------------------------------------------
@ -304,6 +309,8 @@ struct SqPvUnit
void ModifyPrivilegeWithID(SQInteger id, SQInteger value) { Valid().ModifyPrivilege(id, value); }
void ModifyPrivilegeWithTag(StackStrF & tag, SQInteger value) { Valid().ModifyPrivilege(tag, value); }
void RemoveAllPrivileges() { Valid().RemoveAllPrivileges(); }
// --------------------------------------------------------------------------------------------
void EachEntryID(Object & ctx, Function & func) { return Valid().EachEntryID(ctx, func); }
};
} // Namespace:: SqMod