libultraship
Reimplementations of libultra (N64 SDK) functions for modern hardware
Loading...
Searching...
No Matches
Ship::PartList< C, StoredPtr > Class Template Reference

A thread-safe ordered list of Parts. More...

#include <PartList.h>

Inheritance diagram for Ship::PartList< C, StoredPtr >:
[legend]
Collaboration diagram for Ship::PartList< C, StoredPtr >:
[legend]

Public Member Functions

 PartList (const size_t initialAllocation=0)
 Constructs a PartList, optionally pre-allocating storage.
 
virtual ~PartList ()=default
 
bool Has (std::shared_ptr< C > part) const
 Checks whether a specific Part is in the list.
 
template<typename T >
bool Has () const
 Checks whether any Part of type T is in the list.
 
bool Has (const uint64_t id) const
 Checks whether a Part with the given ID is in the list.
 
bool Has () const
 Checks whether the list contains any Parts at all.
 
size_t GetCount () const
 Returns the number of currently live Parts in the list.
 
uint64_t GetMutationVersion () const
 Returns a monotonically increasing counter that increments on every add or remove.
 
std::shared_ptr< C > Get (const uint64_t id) const
 Retrieves a Part by its unique ID.
 
std::shared_ptr< std::vector< std::shared_ptr< C > > > Get () const
 Returns a snapshot (copy) of all currently live Parts in the list.
 
template<typename T >
std::shared_ptr< std::vector< std::shared_ptr< T > > > Get () const
 Returns all Parts that can be dynamic_cast to type T.
 
std::shared_ptr< std::vector< std::shared_ptr< C > > > Get (const std::vector< uint64_t > &ids) const
 Returns all Parts whose IDs appear in the given vector.
 
template<typename T >
std::shared_ptr< T > GetFirst () const
 Returns the first Part that can be dynamic_cast to T.
 
ListReturnCode Add (std::shared_ptr< C > part, const bool force=false)
 Adds a Part to the list if not already present.
 
ListReturnCode Add (const std::vector< std::shared_ptr< C > > &parts, const bool force=false)
 Adds multiple Parts to the list.
 
ListReturnCode Remove (std::shared_ptr< C > part, const bool force=false)
 Removes a specific Part from the list.
 
ListReturnCode Remove (const uint64_t id, const bool force=false)
 Removes a Part by its unique ID.
 
ListReturnCode Remove (const bool force=false)
 Removes all Parts from the list.
 
ListReturnCode Remove (const std::vector< std::shared_ptr< C > > &parts, const bool force=false)
 Removes multiple Parts from the list.
 
template<typename T >
ListReturnCode Remove (const bool force=false)
 Removes all Parts that can be dynamic_cast to type T.
 
ListReturnCode Remove (const std::vector< uint64_t > &ids, const bool force=false)
 Removes all Parts whose IDs appear in the given vector.
 
- Public Member Functions inherited from Ship::Part
 Part ()
 Constructs a Part and assigns it a unique ID.
 
 Part (std::shared_ptr< Context > context)
 Constructs a Part with an explicit Context reference and unique ID.
 
virtual ~Part ()=default
 
uint64_t GetId () const
 Returns the unique identifier for this Part.
 
bool operator== (const Part &other) const
 Compares two Parts for equality by their unique IDs.
 
std::shared_ptr< ContextGetContext () const
 Returns the Context this Part belongs to, or nullptr if unset.
 
void SetContext (std::shared_ptr< Context > ctx)
 Sets the Context this Part belongs to.
 

Protected Member Functions

template<typename P = StoredPtr>
std::enable_if< std::is_same< P, std::shared_ptr< C > >::value, std::vector< P > & >::type GetList ()
 Direct access to the underlying vector for strong-storage lists.
 
template<typename P = StoredPtr>
std::enable_if< std::is_same< P, std::shared_ptr< C > >::value, conststd::vector< P > & >::type GetList () const
 Direct const access to the underlying vector for strong-storage lists.
 
std::recursive_mutex & GetMutex () const
 Returns the internal recursive mutex used to guard list access.
 
virtual bool CanAdd (std::shared_ptr< C > part)
 Permission hook called before adding a Part. Override to deny.
 
virtual bool CanRemove (std::shared_ptr< C > part)
 Permission hook called before removing a Part. Override to deny.
 
virtual void Added (std::shared_ptr< C > part, const bool forced)
 Notification hook called after a Part has been added.
 
virtual void Removed (std::shared_ptr< C > part, const bool forced)
 Notification hook called after a Part has been removed.
 
- Protected Member Functions inherited from Ship::Part
virtual void OnAdded (bool forced)
 Called after this Part has been added to a PartList.
 
virtual void OnRemoved (bool forced)
 Called after this Part has been removed from a PartList.
 

Detailed Description

template<typename C = Part, typename StoredPtr = std::shared_ptr<C>>
class Ship::PartList< C, StoredPtr >

A thread-safe ordered list of Parts.

Provides add, remove, and lookup operations for a collection of Part-derived objects. Storage pointer type is configurable (shared_ptr or weak_ptr), while the public API remains shared_ptr-based.

Template Parameters
CThe element type; must be derived from Part.
StoredPtrThe stored pointer type (shared_ptr<C> or weak_ptr<C>).

Constructor & Destructor Documentation

◆ PartList()

template<typename C , typename StoredPtr >
Ship::PartList< C, StoredPtr >::PartList ( const size_t  initialAllocation = 0)
explicit

Constructs a PartList, optionally pre-allocating storage.

Parameters
initialAllocationNumber of elements to reserve up front.

◆ ~PartList()

template<typename C = Part, typename StoredPtr = std::shared_ptr<C>>
virtual Ship::PartList< C, StoredPtr >::~PartList ( )
virtualdefault

Member Function Documentation

◆ Add() [1/2]

template<typename C , typename StoredPtr >
ListReturnCode Ship::PartList< C, StoredPtr >::Add ( const std::vector< std::shared_ptr< C > > &  parts,
const bool  force = false 
)

Adds multiple Parts to the list.

Parameters
partsThe Parts to add.
forceIf true, bypass the CanAdd() permission check for each Part.
Returns
The aggregate ListReturnCode for the batch operation.

◆ Add() [2/2]

template<typename C , typename StoredPtr >
ListReturnCode Ship::PartList< C, StoredPtr >::Add ( std::shared_ptr< C >  part,
const bool  force = false 
)

Adds a Part to the list if not already present.

Parameters
partThe Part to add.
forceIf true, bypass the CanAdd() permission check.
Returns
ListReturnCode indicating the result.

◆ Added()

template<typename C , typename StoredPtr >
void Ship::PartList< C, StoredPtr >::Added ( std::shared_ptr< C >  part,
const bool  forced 
)
protectedvirtual

Notification hook called after a Part has been added.

Parameters
partThe Part that was added.
forcedWhether the addition bypassed permission checks.

Reimplemented in Ship::BasicComponentList< StoredPtr >.

◆ CanAdd()

template<typename C , typename StoredPtr >
bool Ship::PartList< C, StoredPtr >::CanAdd ( std::shared_ptr< C >  part)
protectedvirtual

Permission hook called before adding a Part. Override to deny.

Implementations must not perform cross-list add/remove operations. This hook may be called outside the target list's lock to avoid lock-order inversions between lists.

Parameters
partThe Part about to be added.
Returns
True if the addition is permitted.

◆ CanRemove()

template<typename C , typename StoredPtr >
bool Ship::PartList< C, StoredPtr >::CanRemove ( std::shared_ptr< C >  part)
protectedvirtual

Permission hook called before removing a Part. Override to deny.

Implementations must not perform cross-list add/remove operations. This hook may be called outside the target list's lock to avoid lock-order inversions between lists.

Parameters
partThe Part about to be removed.
Returns
True if the removal is permitted.

◆ Get() [1/4]

template<typename C , typename StoredPtr >
std::shared_ptr< std::vector< std::shared_ptr< C > > > Ship::PartList< C, StoredPtr >::Get ( ) const

Returns a snapshot (copy) of all currently live Parts in the list.

◆ Get() [2/4]

template<typename C , typename StoredPtr >
template<typename T >
std::shared_ptr< std::vector< std::shared_ptr< T > > > Ship::PartList< C, StoredPtr >::Get ( ) const

Returns all Parts that can be dynamic_cast to type T.

Template Parameters
TThe target derived type.
Returns
A vector of matching Parts cast to T.

◆ Get() [3/4]

template<typename C , typename StoredPtr >
std::shared_ptr< std::vector< std::shared_ptr< C > > > Ship::PartList< C, StoredPtr >::Get ( const std::vector< uint64_t > &  ids) const

Returns all Parts whose IDs appear in the given vector.

Parameters
idsThe IDs to filter by.
Returns
A vector of matching Parts.

◆ Get() [4/4]

template<typename C , typename StoredPtr >
std::shared_ptr< C > Ship::PartList< C, StoredPtr >::Get ( const uint64_t  id) const

Retrieves a Part by its unique ID.

Parameters
idThe Part ID to look up.
Returns
The matching Part, or nullptr if not found.

◆ GetCount()

template<typename C , typename StoredPtr >
size_t Ship::PartList< C, StoredPtr >::GetCount ( ) const

Returns the number of currently live Parts in the list.

◆ GetFirst()

template<typename C , typename StoredPtr >
template<typename T >
std::shared_ptr< T > Ship::PartList< C, StoredPtr >::GetFirst ( ) const

Returns the first Part that can be dynamic_cast to T.

Template Parameters
TThe target derived type.
Returns
The first matching Part, or nullptr if none found.

◆ GetList() [1/2]

template<typename C , typename StoredPtr >
template<typename P >
std::enable_if< std::is_same< P, std::shared_ptr< C > >::value, std::vector< P > & >::type Ship::PartList< C, StoredPtr >::GetList ( )
protected

Direct access to the underlying vector for strong-storage lists.

Available only when StoredPtr is std::shared_ptr<C>.

◆ GetList() [2/2]

template<typename C , typename StoredPtr >
template<typename P >
std::enable_if< std::is_same< P, std::shared_ptr< C > >::value, conststd::vector< P > & >::type Ship::PartList< C, StoredPtr >::GetList ( ) const
protected

Direct const access to the underlying vector for strong-storage lists.

Available only when StoredPtr is std::shared_ptr<C>.

◆ GetMutationVersion()

template<typename C = Part, typename StoredPtr = std::shared_ptr<C>>
uint64_t Ship::PartList< C, StoredPtr >::GetMutationVersion ( ) const
inline

Returns a monotonically increasing counter that increments on every add or remove.

Callers can store this value and compare it on subsequent frames to detect whether the list has changed since they last inspected it.

◆ GetMutex()

template<typename C , typename StoredPtr >
std::recursive_mutex & Ship::PartList< C, StoredPtr >::GetMutex ( ) const
protected

Returns the internal recursive mutex used to guard list access.

◆ Has() [1/4]

template<typename C , typename StoredPtr >
template<typename T >
bool Ship::PartList< C, StoredPtr >::Has ( ) const

Checks whether any Part of type T is in the list.

Template Parameters
TThe derived type to search for via dynamic_cast.
Returns
True if at least one matching Part is found.

◆ Has() [2/4]

template<typename C , typename StoredPtr >
bool Ship::PartList< C, StoredPtr >::Has ( ) const

Checks whether the list contains any Parts at all.

◆ Has() [3/4]

template<typename C , typename StoredPtr >
bool Ship::PartList< C, StoredPtr >::Has ( const uint64_t  id) const

Checks whether a Part with the given ID is in the list.

Parameters
idThe unique Part ID to search for.
Returns
True if found.

◆ Has() [4/4]

template<typename C , typename StoredPtr >
bool Ship::PartList< C, StoredPtr >::Has ( std::shared_ptr< C >  part) const

Checks whether a specific Part is in the list.

Parameters
partThe Part to search for.
Returns
True if the Part is present.

◆ Remove() [1/6]

template<typename C , typename StoredPtr >
ListReturnCode Ship::PartList< C, StoredPtr >::Remove ( const bool  force = false)

Removes all Parts from the list.

Parameters
forceIf true, bypass the CanRemove() permission check for each Part.
Returns
ListReturnCode indicating the result.

◆ Remove() [2/6]

template<typename C , typename StoredPtr >
template<typename T >
ListReturnCode Ship::PartList< C, StoredPtr >::Remove ( const bool  force = false)

Removes all Parts that can be dynamic_cast to type T.

Template Parameters
TThe derived type to match for removal.
Parameters
forceIf true, bypass the CanRemove() permission check.
Returns
ListReturnCode indicating the result.

◆ Remove() [3/6]

template<typename C , typename StoredPtr >
ListReturnCode Ship::PartList< C, StoredPtr >::Remove ( const std::vector< std::shared_ptr< C > > &  parts,
const bool  force = false 
)

Removes multiple Parts from the list.

Parameters
partsThe Parts to remove.
forceIf true, bypass the CanRemove() permission check.
Returns
The aggregate ListReturnCode for the batch operation.

◆ Remove() [4/6]

template<typename C , typename StoredPtr >
ListReturnCode Ship::PartList< C, StoredPtr >::Remove ( const std::vector< uint64_t > &  ids,
const bool  force = false 
)

Removes all Parts whose IDs appear in the given vector.

Parameters
idsThe IDs to remove.
forceIf true, bypass the CanRemove() permission check.
Returns
The aggregate ListReturnCode for the batch operation.

◆ Remove() [5/6]

template<typename C , typename StoredPtr >
ListReturnCode Ship::PartList< C, StoredPtr >::Remove ( const uint64_t  id,
const bool  force = false 
)

Removes a Part by its unique ID.

Parameters
idThe Part ID to remove.
forceIf true, bypass the CanRemove() permission check.
Returns
ListReturnCode indicating the result.

◆ Remove() [6/6]

template<typename C , typename StoredPtr >
ListReturnCode Ship::PartList< C, StoredPtr >::Remove ( std::shared_ptr< C >  part,
const bool  force = false 
)

Removes a specific Part from the list.

Parameters
partThe Part to remove.
forceIf true, bypass the CanRemove() permission check.
Returns
ListReturnCode indicating the result.

◆ Removed()

template<typename C , typename StoredPtr >
void Ship::PartList< C, StoredPtr >::Removed ( std::shared_ptr< C >  part,
const bool  forced 
)
protectedvirtual

Notification hook called after a Part has been removed.

Parameters
partThe Part that was removed.
forcedWhether the removal bypassed permission checks.

Reimplemented in Ship::BasicComponentList< StoredPtr >.


The documentation for this class was generated from the following file: