Skip to content

Commit

Permalink
More objects containers moved to smart pointer containers.
Browse files Browse the repository at this point in the history
  • Loading branch information
cbnolok committed Oct 17, 2023
1 parent 9a67037 commit ec419a2
Show file tree
Hide file tree
Showing 21 changed files with 134 additions and 117 deletions.
2 changes: 1 addition & 1 deletion src/common/CDataBase.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

#include "../game/CServerConfig.h"
#include "../game/CServer.h"
#include "../game/CServerTime.h"
#include "../sphere/asyncdb.h"
#include "resource/sections/CResourceNamedDef.h"
#include "CLog.h"
#include "CException.h"
#include "CScriptTriggerArgs.h"
Expand Down
2 changes: 1 addition & 1 deletion src/common/CScriptObj.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ bool CScriptObj::r_Call( size_t uiFunctionIndex, CTextConsole * pSrc, CScriptTri
EXC_TRY("Call by index");
ASSERT(r_CanCall(uiFunctionIndex));

CResourceNamedDef * pFunction = static_cast <CResourceNamedDef *>( g_Cfg.m_Functions[uiFunctionIndex] );
CResourceNamedDef * pFunction = g_Cfg.m_Functions[uiFunctionIndex].get();
ASSERT(pFunction);
CResourceLock sFunction;
if ( pFunction->ResourceLock(sFunction) )
Expand Down
2 changes: 1 addition & 1 deletion src/common/resource/CResourceSortedArrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ int CMultiDefArray::CompareKey(MULTI_TYPE id, CUOMulti* pBase, bool fNoSpaces) c
}

/*
int CObjNameSortVector::_compare(const CScriptObj* pObj, lpctstr ptcKey) // static
int CObjSharedPtrNameSortVector::_compare(const CScriptObj* pObj, lpctstr ptcKey) // static
{
ASSERT( pObj );
ASSERT( ptcKey );
Expand Down
19 changes: 10 additions & 9 deletions src/common/resource/CResourceSortedArrays.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef _INC_CRESOURCESORTEDARRAYS_H
#define _INC_CRESOURCESORTEDARRAYS_H

#include "../../common/sphere_library/sptr_containers.h"
#include "../CServerMap.h"

struct CValStr;
Expand Down Expand Up @@ -66,26 +67,26 @@ struct CMultiDefArray : public CSObjSortArray< CUOMulti*, MULTI_TYPE >
};


struct CObjNameSorter
template <typename _ObjType>
struct CObjSharedPtrNameVectorSorter
{
inline bool operator()(const CScriptObj * s1, const CScriptObj * s2) const noexcept
inline bool operator()(std::shared_ptr<_ObjType> const& s1, std::shared_ptr<_ObjType> const& s2) const noexcept
{
//return (Str_CmpHeadI(s1->GetName(), s2->GetName()) < 0); // not needed, since the compared strings don't contain whitespaces (arguments or whatsoever)
// Current strcmpi implementation internally converts to lowerCASE the strings, so this will work until Str_CmpHeadI checks with tolower, instead of toupper
return (strcmpi(s1->GetName(), s2->GetName()) < 0);
}
};
class CObjNameSortVector : public CSSortedVector< CScriptObj*, CObjNameSorter >
{
inline static int _compare(const CScriptObj* pObj, lpctstr ptcKey)
inline static int _compare(std::shared_ptr<_ObjType> const& pObj, lpctstr ptcKey)
{
return -Str_CmpHeadI(ptcKey, pObj->GetName()); // We use Str_CmpHeadI to ignore '_' and whitespaces (args to the function or whatever) in ptcKey
}

};
template <typename _ObjType>
class CObjSharedPtrNameSortVector : public CSSharedPtrSortedVector<_ObjType, CObjSharedPtrNameVectorSorter<_ObjType>>
{
public:
//static const char *m_sClassName;

inline size_t find_sorted(lpctstr ptcKey) const noexcept { return this->find_predicate(ptcKey, _compare); }
inline size_t find_sorted(lpctstr ptcKey) const noexcept { return this->find_predicate(ptcKey, CObjSharedPtrNameVectorSorter<_ObjType>::_compare); }
inline bool ContainsKey(lpctstr ptcKey) const noexcept { return (SCONT_BADINDEX != this->find_sorted(ptcKey)); }
};

Expand Down
4 changes: 2 additions & 2 deletions src/common/resource/sections/CWebPageDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,9 @@ bool CWebPageDef::r_Verb( CScript & s, CTextConsole * pSrc ) // some command on
{
if ( ! s.HasArgs())
return false;
CGMPage * pPage = static_cast <CGMPage*>( g_World.m_GMPages.GetContainerHead());
for ( ; pPage!=nullptr; pPage = pPage->GetNext())
for (auto &sptrPage : g_World.m_GMPages)
{
CGMPage* pPage = sptrPage.get();
++sm_iListIndex;
Str_CopyLimitNull( pszTmp2, s.GetArgStr(), STR_TEMPLENGTH);
pPage->ParseScriptText( Str_MakeFiltered( pszTmp2 ), &g_Serv, 1 );
Expand Down
34 changes: 23 additions & 11 deletions src/common/sphere_library/sptr_containers.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ class _CSPtrVectorBase : public std::vector<_PtrWrapperType>
using _base_type = std::vector<_PtrWrapperType>;
using iterator = typename _base_type::iterator;

inline void remove(const size_t index) {
inline void erase_index(const size_t index) {
_base_type::erase(_base_type::begin() + index);
}

void remove(_Type* elem) {
void erase_element(_Type* elem) {
size_t idx = 0;
for (_PtrWrapperType const& ptr : *this) {
if (ptr.get() == elem) {
remove(idx);
this->erase_index(idx);
return;
}
++idx;
Expand Down Expand Up @@ -63,7 +63,7 @@ template <typename _Type>
class CSUniquePtrVector : public _CSPtrVectorBase<_Type, std::unique_ptr<_Type>>
{
public:
template <typename _PtrType = std::unique_ptr<_Type>> // Gets both bare pointer and shared_ptr
template <typename _PtrType = std::unique_ptr<_Type>> // Gets both bare pointer and unique_ptr
void emplace_index_grow(size_t index, _PtrType value) {
if (index >= this->size()) {
this->resize(index + 1); // The capacity will be even greater, since it calls vector::resize
Expand All @@ -74,13 +74,17 @@ class CSUniquePtrVector : public _CSPtrVectorBase<_Type, std::unique_ptr<_Type>>
this->operator[](index) = value;
}

//template <typename _TypePtr = std::nullptr_t>
inline void emplace_index_grow(size_t index, std::nullptr_t) {
this->emplace_index_grow(index, std::unique_ptr<_Type>());
}

//template <>
void emplace_index_grow(size_t, _Type*) = delete;
template <typename... _ArgPackType>
inline void emplace_front(_ArgPackType&&... args) {
_base_type::emplace(_base_type::cbegin(), std::forward<_ArgPackType>(args)...);
}

// Explicitly create a unique_ptr, then add to this container.
//void emplace_index_grow(size_t, _Type*) = delete;

/*
template <typename... _ArgType>
Expand Down Expand Up @@ -134,13 +138,17 @@ class CSSharedPtrVector : public _CSPtrVectorBase<_Type, std::shared_ptr<_Type>>
this->operator[](index) = value;
}

//emplate <typename _PtrType = std::nullptr_t>
inline void emplace_index_grow(size_t index, std::nullptr_t) {
this->emplace_index_grow(index, std::shared_ptr<_Type>());
}

//template <>
void emplace_index_grow(size_t, _Type*) = delete;
template <typename... _ArgPackType>
inline void emplace_front(_ArgPackType&&... args) {
_base_type::emplace(_base_type::cbegin(), std::forward<_ArgPackType>(args)...);
}

// Explicitly create a unique_ptr, then add to this container.
//void emplace_index_grow(size_t, _Type*) = delete;

/*
template <typename... _ArgType>
Expand Down Expand Up @@ -171,7 +179,6 @@ class CSWeakPtrVector : public _CSPtrVectorBase<_Type, std::weak_ptr<_Type>>
this->operator[](index) = std::forward<_ArgType>(value);
}

//template <typename _PtrType = std::nullptr_t>
void emplace_index_grow(size_t index, std::nullptr_t) {
if (index >= this->size()) {
this->resize(index); // The capacity will be even greater, since it calls vector::resize
Expand All @@ -180,6 +187,11 @@ class CSWeakPtrVector : public _CSPtrVectorBase<_Type, std::weak_ptr<_Type>>
this->operator[](index).reset();
}
}

template <typename... _ArgPackType>
inline void emplace_front(_ArgPackType&&... args) {
_base_type::emplace(_base_type::cbegin(), std::forward<_ArgPackType>(args)...);
}
};
template <typename _Type, typename _Comp = std::less<_Type>>
class CSWeakPtrSortedVector : public _CSPtrSortedVectorBase<_Type, std::weak_ptr<_Type>, _Comp>
Expand Down
10 changes: 5 additions & 5 deletions src/game/CServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1328,11 +1328,11 @@ bool CServer::r_WriteVal( lpctstr ptcKey, CSString & sVal, CTextConsole * pSrc,
else if (!strnicmp(ptcKey, "GMPAGE.", 7))
{
ptcKey += 7;
size_t iNum = Exp_GetVal(ptcKey);
if (iNum >= g_World.m_GMPages.GetContentCount())
size_t iNum = Exp_GetULLVal(ptcKey);
if (iNum >= g_World.m_GMPages.size())
return false;

CGMPage* pGMPage = static_cast<CGMPage*>(g_World.m_GMPages.GetContentAt(iNum));
CGMPage* pGMPage = g_World.m_GMPages[iNum].get();
if (!pGMPage)
return false;

Expand Down Expand Up @@ -1483,10 +1483,10 @@ bool CServer::r_Verb( CScript &s, CTextConsole * pSrc )
{
ptcKey += 7;
size_t iNum = Exp_GetVal(ptcKey);
if (iNum >= g_World.m_GMPages.GetContentCount())
if (iNum >= g_World.m_GMPages.size())
return false;

CGMPage* pGMPage = static_cast<CGMPage*>(g_World.m_GMPages.GetContentAt(iNum));
CGMPage* pGMPage = g_World.m_GMPages[iNum].get();
if (!pGMPage)
return false;

Expand Down
8 changes: 4 additions & 4 deletions src/game/CServerConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ const CSkillDef* CServerConfig::FindSkillDef( lpctstr ptcKey ) const
const size_t i = m_SkillNameDefs.find_sorted(ptcKey);
if ( i == SCONT_BADINDEX )
return nullptr;
return static_cast <const CSkillDef*>(m_SkillNameDefs[i]);
return m_SkillNameDefs[i].get();
}

const CSkillDef * CServerConfig::SkillLookup( lpctstr ptcKey )
Expand Down Expand Up @@ -3283,6 +3283,7 @@ bool CServerConfig::LoadResourceSection( CScript * pScript )
m_ResHash.AddSortKey( rid, pNewLink );
}

ASSERT(pScript);
CScriptLineContext LineContext = pScript->GetContext();
pNewLink->r_Load(*pScript);
pScript->SeekContext( LineContext );
Expand Down Expand Up @@ -3570,12 +3571,11 @@ bool CServerConfig::LoadResourceSection( CScript * pScript )
if (pLinkResScript != nullptr)
pNewLink->SetLink(pLinkResScript);

m_Functions.emplace(pNewLink);
m_Functions.emplace(static_cast<CResourceNamedDef*>(pNewLink));
}
else
{
pNewLink = dynamic_cast<CResourceNamedDef*>(m_Functions[uiFunctionIndex]);
ASSERT(pNewLink);
pNewLink = m_Functions[uiFunctionIndex].get();
}
}
break;
Expand Down
5 changes: 3 additions & 2 deletions src/game/CServerConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class CClient;
class CServerDef;
using CServerRef = CServerDef*;

class CResourceNamedDef;

/**
* @enum EF_TYPE
Expand Down Expand Up @@ -587,7 +588,7 @@ extern class CServerConfig : public CResourceBase

CMultiDefArray m_MultiDefs; // read from the MUL files. Cached here on demand.

CObjNameSortVector m_SkillNameDefs; // const CSkillDef* Name sorted.
CObjSharedPtrNameSortVector<CSkillDef> m_SkillNameDefs; // const CSkillDef* Name sorted.
CSSharedPtrVector<CSkillDef> m_SkillIndexDefs; // Defined Skills indexed by number.
CSSharedPtrVector<CSpellDef> m_SpellDefs; // Defined Spells.
CSWeakPtrVector<CSpellDef> m_SpellDefs_Sorted; // Defined Spells, in skill order.
Expand All @@ -596,7 +597,7 @@ extern class CServerConfig : public CResourceBase

public:
CObjNameSortArray m_Servers; // Servers list. we act like the login server with this.
CObjNameSortVector m_Functions; // Subroutines that can be used in scripts.
CObjSharedPtrNameSortVector<CResourceNamedDef> m_Functions; // Subroutines that can be used in scripts.
CRegionLinks m_RegionDefs; // All [REGION ] stored inside.

// static definition stuff from *TABLE.SCP mostly.
Expand Down
2 changes: 1 addition & 1 deletion src/game/CServerDef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ bool CServerDef::r_WriteVal( lpctstr ptcKey, CSString &sVal, CTextConsole * pSrc
sVal.FormatSTVal( StatGet( SERV_STAT_CHARS ) );
break;
case SC_GMPAGES:
sVal.FormatSTVal( g_World.m_GMPages.GetContentCount() );
sVal.FormatSTVal( g_World.m_GMPages.size() );
break;
case SC_TIMEZONE:
sVal.FormatVal( m_TimeZone );
Expand Down
26 changes: 14 additions & 12 deletions src/game/CWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,20 +524,22 @@ void CWorldThread::GarbageCollection_NewObjs()


// Clean up GM pages not linked to an valid char/account
CGMPage* pGMPageNext = nullptr;
for (CGMPage* pGMPage = static_cast<CGMPage*>(g_World.m_GMPages.GetContainerHead()); pGMPage != nullptr; pGMPage = pGMPageNext)
for (auto it = g_World.m_GMPages.begin(); it != g_World.m_GMPages.end();)
{
pGMPageNext = pGMPage->GetNext();

std::unique_ptr<CGMPage>& pGMPage = *it;
if (!pGMPage->m_uidChar.CharFind())
{
DEBUG_ERR(("GC: Deleted GM Page linked to invalid char (UID=0%x)\n", (dword)(pGMPage->m_uidChar)));
delete pGMPage;
it = g_World.m_GMPages.erase(it);
}
else if (!g_Accounts.Account_Find(pGMPage->m_sAccount))
{
DEBUG_ERR(("GC: Deleted GM Page linked to invalid account '%s'\n", pGMPage->GetName()));
delete pGMPage;
it = g_World.m_GMPages.erase(it);
}
else
{
++it;
}
}
EXC_CATCH;
Expand Down Expand Up @@ -810,9 +812,9 @@ bool CWorld::SaveStage() // Save world state in stages.
}

// GM_Pages.
for (CGMPage* pGMPage = static_cast<CGMPage*>(m_GMPages.GetContainerHead()); pGMPage != nullptr; pGMPage = pGMPage->GetNext())
for (auto& sptrGMPage : g_World.m_GMPages)
{
pGMPage->r_Write(m_FileData);
sptrGMPage->r_Write(m_FileData);
}
}
else if ( _iSaveStage == iSectorsQty +1 )
Expand Down Expand Up @@ -1341,8 +1343,8 @@ bool CWorld::LoadWorld() // Load world from script

m_Stones.clear();
m_Multis.clear();
m_Parties.ClearContainer();
m_GMPages.ClearContainer();
m_Parties.clear();
m_GMPages.clear();

_Sectors.Close();
CloseAllUIDs();
Expand Down Expand Up @@ -1664,8 +1666,8 @@ void CWorld::Close()
_Ticker._ObjStatusUpdates.clear();
}

m_Parties.ClearContainer();
m_GMPages.ClearContainer();
m_Parties.clear();
m_GMPages.clear();

// Disconnect the players, so that we have none of them in a sector
ClientIterator it;
Expand Down
Loading

0 comments on commit ec419a2

Please sign in to comment.