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

Discard previous approach.

Left incomplete untill further notice.
This commit is contained in:
Sandu Liviu Catalin 2020-09-04 23:52:52 +03:00
parent 076da5fe02
commit f0779f57cb
2 changed files with 309 additions and 1067 deletions

View File

@ -17,309 +17,189 @@ static constexpr size_t BAD_POS = ~static_cast< size_t >(0);
// ------------------------------------------------------------------------------------------------
PvManagers PvManager::s_Managers;
// ------------------------------------------------------------------------------------------------
PvManager & PvUnit::GetManager() const
/* ------------------------------------------------------------------------------------------------
* Privilege unit wrapper.
*/
class PvUnitWrap
{
return m_Class->GetManager(); // Classes must always have a manager!
}
// ------------------------------------------------------------------------------------------------
PvClass & PvUnit::GetClass() const
private:
/* --------------------------------------------------------------------------------------------
*
*/
PvUnit::Ref mInst;
public:
/* -------------------------------------------------------------------------------------------
* Default constructor.
*/
PvUnitWrap(SQInteger id, PvClass * cls)
{
return *m_Class; // Units must always have a class!
}
// ------------------------------------------------------------------------------------------------
void PvUnit::SetClass(PvClass & cls)
{
cls.AddUnit(*this);
}
// ------------------------------------------------------------------------------------------------
bool PvUnit::Can(const PvEntry & entry) const
{
return false;
}
// ------------------------------------------------------------------------------------------------
bool PvUnit::Can(const PvEntry & entry, const PvUnit & unit) const
{
return false;
}
// ------------------------------------------------------------------------------------------------
PvUnit * PvClass::GetUnitByID(SQInteger id) const
/* -------------------------------------------------------------------------------------------
* Default constructor.
*/
PvUnitWrap(SQInteger id, StackStrF && tag, PvClass * cls)
{
// Locate the requested unit
for (const auto & e : m_Units)
{
if (e.first == id)
{
return e.second;
}
}
// No such unit
return nullptr;
}
// ------------------------------------------------------------------------------------------------
PvUnit * PvClass::GetUnitByTag(StackStrF & name) const
/* -------------------------------------------------------------------------------------------
* Copy constructor (disabled).
*/
PvUnitWrap(const PvUnitWrap & o) = delete;
/* -------------------------------------------------------------------------------------------
* Destructor.
*/
~PvUnitWrap();
/* -------------------------------------------------------------------------------------------
* Copy assignment operator (disabled).
*/
PvUnitWrap & operator = (const PvUnitWrap & o) = delete;
/* --------------------------------------------------------------------------------------------
* Release all script resources. Recursively forward request.
*/
void Release()
{
// Retrieve the hash
auto hash = name.HashIt();
// Locate the requested unit
for (const auto & e : m_Units)
{
// Does the string hash match?
if (e.second->GetTagW().GetHash() == hash &&
// Check if the whole string matches as well
strncmp(name.mPtr, e.second->GetTagW().mPtr,
std::min(name.mLen, e.second->GetTagW().mLen)) == 0)
{
return e.second; // This is what we're looking for
}
}
// No such unit
return nullptr;
}
// ------------------------------------------------------------------------------------------------
LightObj PvClass::CreateUnit(SQInteger id, StackStrF & name)
};
/* ------------------------------------------------------------------------------------------------
* Privilege class wrapper.
*/
class PvClassWrap
{
friend class PvManager;
private:
/* --------------------------------------------------------------------------------------------
*
*/
PvClass::Ref mInst;
public:
/* -------------------------------------------------------------------------------------------
* Default constructor.
*/
PvClassWrap(SQInteger id, PvManager * mgr)
{
return m_Manager->CreateUnitImpl(id, *this, name).second;
}
// ------------------------------------------------------------------------------------------------
void PvClass::AddUnit(PvUnit & unit)
/* -------------------------------------------------------------------------------------------
* Default constructor.
*/
PvClassWrap(SQInteger id, StackStrF && tag, PvManager * mgr)
{
// Is this unit already of this class?
if (&unit.GetClass() == this)
{
return; // Nothing to change!
}
// Remove it from current class
unit.GetClass().UnlistUnit(unit);
// Enlist it with us
EnlistUnit(unit);
// Who's your daddy now!
unit.m_Class = this;
}
// ------------------------------------------------------------------------------------------------
bool PvClass::EnlistUnit(PvUnit & unit)
/* -------------------------------------------------------------------------------------------
* Copy constructor (disabled).
*/
PvClassWrap(const PvClassWrap & o) = delete;
/* -------------------------------------------------------------------------------------------
* Move constructor (disabled).
*/
PvClassWrap(PvClassWrap && o) = delete;
/* -------------------------------------------------------------------------------------------
* Destructor.
*/
~PvClassWrap();
/* -------------------------------------------------------------------------------------------
* Copy assignment operator (disabled).
*/
PvClassWrap & operator = (const PvClassWrap & o) = delete;
/* -------------------------------------------------------------------------------------------
* Move assignment operator (disabled).
*/
PvClassWrap & operator = (PvClassWrap && o) = delete;
/* --------------------------------------------------------------------------------------------
* Release all script resources. Recursively forward request.
*/
void Release()
{
// Does it exist already?
for (const auto & e : m_Units)
{
if (e.first == unit.GetID())
{
return false; // Already enlisted here!
}
}
// Enlist it now
m_Units.emplace_back(unit.GetID(), &unit);
// Enlisted now
return true;
}
// ------------------------------------------------------------------------------------------------
bool PvClass::UnlistUnit(PvUnit & unit)
};
/* ------------------------------------------------------------------------------------------------
* Privilege entry wrapper.
*/
class PvEntryWrap
{
// Locate the entry in our list
auto it = std::find_if(m_Units.cbegin(), m_Units.cend(),
[=, id = unit.GetID()](PvUnitList::const_reference e) -> bool {
return e.first == id;
});
// Is this unit enlisted here?
if (it == m_Units.end())
friend class PvManager;
private:
/* --------------------------------------------------------------------------------------------
*
*/
PvEntry::Ref mInst;
public:
/* -------------------------------------------------------------------------------------------
* Default constructor.
*/
PvEntryWrap(SQInteger id, PvManager * mgr)
{
return false; // Not enlisted here!
}
// Remove the unit from out container
m_Units.erase(it);
// Unlisted now
return true;
}
// ------------------------------------------------------------------------------------------------
/* -------------------------------------------------------------------------------------------
* Default constructor.
*/
PvEntryWrap(SQInteger id, StackStrF && tag, PvManager * mgr)
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
// ------------------------------------------------------------------------------------------------
PvEntry * PvManager::GetEntryByID(SQInteger id) const
{
// Locate the requested entry
for (const auto & e : m_Entries)
{
if (e.first == id)
{
return e.second;
}
}
// No such entry
return nullptr;
}
// ------------------------------------------------------------------------------------------------
PvEntry * PvManager::GetEntryByTag(StackStrF & name) const
{
// Retrieve the hash
auto hash = name.HashIt();
// Locate the requested entry
for (const auto & e : m_Entries)
{
// Does the string hash match?
if (e.second->GetTagW().GetHash() == hash &&
// Check if the whole string matches as well
strncmp(name.mPtr, e.second->GetTagW().mPtr,
std::min(name.mLen, e.second->GetTagW().mLen)) == 0)
{
return e.second; // This is what we're looking for
}
}
// No such entry
return nullptr;
}
// ------------------------------------------------------------------------------------------------
LightObj PvManager::CreateEntry(SQInteger id, StackStrF & name)
{
// See if this entry exists already
PvEntry * e = GetEntryByID(id);
// Return existing entry if already exists
if (e != nullptr)
{
return LightObj(e);
}
// Create an entry instance
LightObj o(DeleteGuard< PvEntry >(new PvEntry(id, std::move(name), this)));
// Get a pointer to the new instance
e = o.CastI< PvEntry >();
// Append the new entry
m_Entries.emplace_back(id, e);
// Return the resulted object
return o;
}
// ------------------------------------------------------------------------------------------------
PvClass *PvManager::GetClassByID(SQInteger id) const
/* -------------------------------------------------------------------------------------------
* Copy constructor (disabled).
*/
PvEntryWrap(const PvEntryWrap & o) = delete;
/* -------------------------------------------------------------------------------------------
* Move constructor (disabled).
*/
PvEntryWrap(PvEntryWrap && o) = delete;
/* -------------------------------------------------------------------------------------------
* Destructor.
*/
~PvEntryWrap();
/* -------------------------------------------------------------------------------------------
* Copy assignment operator (disabled).
*/
PvEntryWrap & operator = (const PvEntryWrap & o) = delete;
/* -------------------------------------------------------------------------------------------
* Move assignment operator (disabled).
*/
PvEntryWrap & operator = (PvEntryWrap && o) = delete;
/* --------------------------------------------------------------------------------------------
* Release all script resources. Recursively forward request.
*/
void Release()
{
// Locate the requested class
for (const auto & e : m_Classes)
{
if (e.first == id)
{
return e.second;
}
}
// No such class
return nullptr;
}
// ------------------------------------------------------------------------------------------------
PvClass * PvManager::GetClassByTag(StackStrF & name) const
{
// Retrieve the hash
auto hash = name.HashIt();
// Locate the requested class
for (const auto & e : m_Classes)
{
// Does the string hash match?
if (e.second->GetTagW().GetHash() == hash &&
// Check if the whole string matches as well
strncmp(name.mPtr, e.second->GetTagW().mPtr,
std::min(name.mLen, e.second->GetTagW().mLen)) == 0)
{
return e.second; // This is what we're looking for
}
}
// No such class
return nullptr;
}
// ------------------------------------------------------------------------------------------------
LightObj PvManager::CreateClass(SQInteger id, StackStrF & name)
{
// See if this class exists already
PvClass * e = GetClassByID(id);
// Return existing class if already exists
if (e != nullptr)
{
return LightObj(e);
}
// Create an class instance
LightObj o(DeleteGuard< PvClass >(new PvClass(id, std::move(name), this)));
// Get a pointer to the new instance
e = o.CastI< PvClass >();
// Append the new class
m_Classes.emplace_back(id, e);
// Return the resulted object
return o;
}
// ------------------------------------------------------------------------------------------------
PvUnit * PvManager::GetUnitByID(SQInteger id) const
{
// Locate the requested unit
for (const auto & e : m_Units)
{
if (e.first == id)
{
return e.second;
}
}
// No such unit
return nullptr;
}
// ------------------------------------------------------------------------------------------------
PvUnit * PvManager::GetUnitByTag(StackStrF & name) const
{
// Retrieve the hash
auto hash = name.HashIt();
// Locate the requested unit
for (const auto & e : m_Units)
{
// Does the string hash match?
if (e.second->GetTagW().GetHash() == hash &&
// Check if the whole string matches as well
strncmp(name.mPtr, e.second->GetTagW().mPtr,
std::min(name.mLen, e.second->GetTagW().mLen)) == 0)
{
return e.second; // This is what we're looking for
}
}
// No such unit
return nullptr;
}
// ------------------------------------------------------------------------------------------------
LightObj PvManager::CreateUnit(SQInteger id, PvClass & cls, StackStrF & name)
{
// Return the resulted object
return CreateUnitImpl(id, cls, name).second;
}
// ------------------------------------------------------------------------------------------------
std::pair< PvUnit *, LightObj > PvManager::CreateUnitImpl(SQInteger id, PvClass & cls, StackStrF & name)
{
// See if this unit exists already
PvUnit * unit = GetUnitByID(id);
// Return existing unit if already exists
if (unit != nullptr)
{
// Return the existing unit
return std::make_pair(unit, LightObj{unit});
}
// Create a unit instance
LightObj o{DeleteGuard< PvUnit >(new PvUnit(id, std::move(name), &cls))};
// Grab the pointer from the object
unit = o.CastI< PvUnit >();
// Enlist the new unit with this manager
m_Units.emplace_back(id, unit);
// Enlist the new unit on the specified class
cls.EnlistUnit(*unit);
// Return the created unit
return std::make_pair(unit, std::move(o));
}
// ------------------------------------------------------------------------------------------------
};
// ------------------------------------------------------------------------------------------------
void TerminatePrivileges()
@ -331,7 +211,7 @@ void TerminatePrivileges()
void Register_Privilege(HSQUIRRELVM vm)
{
Table pns(vm);
/*
pns.Bind(_SC("Unit"),
Class< PvUnit, NoConstructor< PvUnit > >(vm, UnitTn::Str)
// Meta-methods
@ -407,8 +287,8 @@ void Register_Privilege(HSQUIRRELVM vm)
.CbFunc(_SC("OnGained"), &PvEntry::SetOnGained)
// Raw functions
);
pns.Bind(_SC("Manager"),
*/
/* pns.Bind(_SC("Manager"),
Class< PvManager, NoCopy< PvManager > >(vm, ManagerTn::Str)
// Constructors
.Ctor()
@ -422,30 +302,12 @@ void Register_Privilege(HSQUIRRELVM vm)
// Core Methods
.FmtFunc(_SC("SetTag"), &PvManager::ApplyTag)
// Member Methods
.Func(_SC("GetEntry"), &PvManager::GetEntryByID)
.Func(_SC("GetEntryByID"), &PvManager::GetEntryByID)
.Func(_SC("HaveEntryWithID"), &PvManager::HaveEntryWithID)
.FmtFunc(_SC("GetEntryByTag"), &PvManager::GetEntryByTag)
.FmtFunc(_SC("HaveEntryWithTag"), &PvManager::HaveEntryWithTag)
.FmtFunc(_SC("CreateEntry"), &PvManager::CreateEntry)
.Func(_SC("GetClass"), &PvManager::GetClassByID)
.Func(_SC("GetClassByID"), &PvManager::GetClassByID)
.Func(_SC("HaveClassWithID"), &PvManager::HaveClassWithID)
.FmtFunc(_SC("GetClassByTag"), &PvManager::GetClassByTag)
.FmtFunc(_SC("HaveClassWithTag"), &PvManager::HaveClassWithTag)
.FmtFunc(_SC("CreateClass"), &PvManager::CreateClass)
.Func(_SC("GetUnit"), &PvManager::GetUnitByID)
.Func(_SC("GetUnitByID"), &PvManager::GetUnitByID)
.Func(_SC("HaveUnitWithID"), &PvManager::HaveUnitWithID)
.FmtFunc(_SC("GetUnitByTag"), &PvManager::GetUnitByTag)
.FmtFunc(_SC("HaveUnitWithTag"), &PvManager::HaveUnitWithTag)
.FmtFunc(_SC("CreateUnit"), &PvManager::CreateUnit)
.CbFunc(_SC("OnQuery"), &PvManager::SetOnQuery)
.CbFunc(_SC("OnLost"), &PvManager::SetOnLost)
.CbFunc(_SC("OnGained"), &PvManager::SetOnGained)
// Raw functions
);
*/
RootTable(vm).Bind(_SC("SqPrivilege"), pns);
}

File diff suppressed because it is too large Load Diff