@ -12,8 +12,8 @@
# include "table/strings.h"
/** Reset the classes, i.e. clear everything. */
template < typename Tspec , typename Ti d, Ti d Tmax >
void NewGRFClass < Tspec , Ti d, Tmax > : : Reset ( )
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
void NewGRFClass < Tspec , Ti n dex , Tmax > : : Reset ( )
{
NewGRFClass : : classes . clear ( ) ;
NewGRFClass : : classes . shrink_to_fit ( ) ;
@ -28,8 +28,8 @@ void NewGRFClass<Tspec, Tid, Tmax>::Reset()
* @ note Upon allocating the same global class ID for a
* second time , this first allocation will be given .
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
Ti d NewGRFClass < Tspec , Ti d, Tmax > : : Allocate ( uint32_t global_id )
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
Ti n dex NewGRFClass < Tspec , Ti n dex , Tmax > : : Allocate ( uint32_t global_id )
{
auto found = std : : find_if ( std : : begin ( NewGRFClass : : classes ) , std : : end ( NewGRFClass : : classes ) , [ global_id ] ( const auto & cls ) { return cls . global_id = = global_id ; } ) ;
@ -38,56 +38,59 @@ Tid NewGRFClass<Tspec, Tid, Tmax>::Allocate(uint32_t global_id)
/* More slots available, allocate a slot to the global id. */
if ( NewGRFClass : : classes . size ( ) < Tmax ) {
auto & cls = NewGRFClass : : classes . emplace_back ( global_id , STR_EMPTY ) ;
return cls . Index ( ) ;
auto it = NewGRFClass : : classes . emplace ( std : : end ( NewGRFClass : : classes ) , global_id , STR_EMPTY ) ;
it - > index = static_cast < Tindex > ( std : : distance ( std : : begin ( NewGRFClass : : classes ) , it ) ) ;
return it - > Index ( ) ;
}
GrfMsg ( 2 , " ClassAllocate: already allocated {} classes, using default " , Tmax ) ;
return static_cast < Ti d> ( 0 ) ;
return static_cast < Ti n dex > ( 0 ) ;
}
/**
* Insert a spec into the class .
* Insert a spec into the class , and update its index .
* @ param spec The spec to insert .
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
void NewGRFClass < Tspec , Ti d, Tmax > : : Insert ( Tspec * spec )
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
void NewGRFClass < Tspec , Ti n dex , Tmax > : : Insert ( Tspec * spec )
{
this - > spec . push_back ( spec ) ;
auto it = this - > spec . insert ( std : : end ( this - > spec ) , spec ) ;
uint16_t index = static_cast < uint16_t > ( std : : distance ( std : : begin ( this - > spec ) , it ) ) ;
if ( spec ! = nullptr ) ( * it ) - > index = index ;
if ( this - > IsUIAvailable ( static_cast < uint > ( this - > spec . size ( ) - 1 ) ) ) this - > ui_count + + ;
if ( this - > IsUIAvailable ( index ) ) this - > ui_count + + ;
}
/**
* Assign a spec to one of the classes .
* @ param spec The spec to assign .
* @ note The spec must have a valid class i d set .
* @ note The spec must have a valid class i n dex set .
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
void NewGRFClass < Tspec , Ti d, Tmax > : : Assign ( Tspec * spec )
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
void NewGRFClass < Tspec , Ti n dex , Tmax > : : Assign ( Tspec * spec )
{
assert ( static_cast < size_t > ( spec - > cl s_id) < NewGRFClass : : classes . size ( ) ) ;
Get ( spec - > cl s_id) - > Insert ( spec ) ;
assert ( static_cast < size_t > ( spec - > cl as s_in dex ) < NewGRFClass : : classes . size ( ) ) ;
Get ( spec - > cl as s_in dex ) - > Insert ( spec ) ;
}
/**
* Get a particular class .
* @ param cl s_id The id for the class .
* @ pre cl s_id < Tmax
* @ param cl ass_index The index of the class .
* @ pre cl as s_in dex < Tmax
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
NewGRFClass < Tspec , Ti d, Tmax > * NewGRFClass < Tspec , Ti d, Tmax > : : Get ( Ti d cl s_id)
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
NewGRFClass < Tspec , Ti n dex , Tmax > * NewGRFClass < Tspec , Ti n dex , Tmax > : : Get ( Ti n dex cl as s_in dex )
{
assert ( static_cast < size_t > ( cl s_id) < NewGRFClass : : classes . size ( ) ) ;
return & NewGRFClass : : classes [ cl s_id] ;
assert ( static_cast < size_t > ( cl as s_in dex ) < NewGRFClass : : classes . size ( ) ) ;
return & NewGRFClass : : classes [ cl as s_in dex ] ;
}
/**
* Get the number of allocated classes .
* @ return The number of classes .
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
uint NewGRFClass < Tspec , Ti d, Tmax > : : GetClassCount ( )
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
uint NewGRFClass < Tspec , Ti n dex , Tmax > : : GetClassCount ( )
{
return static_cast < uint > ( NewGRFClass : : classes . size ( ) ) ;
}
@ -96,8 +99,8 @@ uint NewGRFClass<Tspec, Tid, Tmax>::GetClassCount()
* Get the number of classes available to the user .
* @ return The number of classes .
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
uint NewGRFClass < Tspec , Ti d, Tmax > : : GetUIClassCount ( )
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
uint NewGRFClass < Tspec , Ti n dex , Tmax > : : GetUIClassCount ( )
{
return std : : count_if ( std : : begin ( NewGRFClass : : classes ) , std : : end ( NewGRFClass : : classes ) , [ ] ( const auto & cls ) { return cls . GetUISpecCount ( ) > 0 ; } ) ;
}
@ -107,8 +110,8 @@ uint NewGRFClass<Tspec, Tid, Tmax>::GetUIClassCount()
* @ param index UI index of a class .
* @ return The class ID of the class .
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
Ti d NewGRFClass < Tspec , Ti d, Tmax > : : GetUIClass ( uint index )
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
Ti n dex NewGRFClass < Tspec , Ti n dex , Tmax > : : GetUIClass ( uint index )
{
for ( const auto & cls : NewGRFClass : : classes ) {
if ( cls . GetUISpecCount ( ) = = 0 ) continue ;
@ -122,8 +125,8 @@ Tid NewGRFClass<Tspec, Tid, Tmax>::GetUIClass(uint index)
* @ param index The index where to find the spec .
* @ return The spec at given location .
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
const Tspec * NewGRFClass < Tspec , Ti d, Tmax > : : GetSpec ( uint index ) const
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
const Tspec * NewGRFClass < Tspec , Ti n dex , Tmax > : : GetSpec ( uint index ) const
{
/* If the custom spec isn't defined any more, then the GRF file probably was not loaded. */
return index < this - > GetSpecCount ( ) ? this - > spec [ index ] : nullptr ;
@ -134,8 +137,8 @@ const Tspec *NewGRFClass<Tspec, Tid, Tmax>::GetSpec(uint index) const
* @ param ui_index UI index of the spec .
* @ return index of the spec , or - 1 if out of range .
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
int NewGRFClass < Tspec , Ti d, Tmax > : : GetIndexFromUI ( int ui_index ) const
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
int NewGRFClass < Tspec , Ti n dex , Tmax > : : GetIndexFromUI ( int ui_index ) const
{
if ( ui_index < 0 ) return - 1 ;
for ( uint i = 0 ; i < this - > GetSpecCount ( ) ; i + + ) {
@ -150,8 +153,8 @@ int NewGRFClass<Tspec, Tid, Tmax>::GetIndexFromUI(int ui_index) const
* @ param index index of the spec .
* @ return UI index of the spec , or - 1 if out of range .
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
int NewGRFClass < Tspec , Ti d, Tmax > : : GetUIFromIndex ( int index ) const
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
int NewGRFClass < Tspec , Ti n dex , Tmax > : : GetUIFromIndex ( int index ) const
{
if ( ( uint ) index > = this - > GetSpecCount ( ) ) return - 1 ;
uint ui_index = 0 ;
@ -168,16 +171,13 @@ int NewGRFClass<Tspec, Tid, Tmax>::GetUIFromIndex(int index) const
* @ param index Pointer to return the index of the spec in its class . If nullptr then not used .
* @ return The spec .
*/
template < typename Tspec , typename Ti d, Ti d Tmax >
const Tspec * NewGRFClass < Tspec , Ti d, Tmax > : : GetByGrf ( uint32_t grfid , uint16_t local_id , int * index )
template < typename Tspec , typename Ti n dex , Ti n dex Tmax >
const Tspec * NewGRFClass < Tspec , Ti n dex , Tmax > : : GetByGrf ( uint32_t grfid , uint16_t local_id )
{
for ( const auto & cls : NewGRFClass : : classes ) {
for ( const auto & spec : cls . spec ) {
if ( spec = = nullptr ) continue ;
if ( spec - > grf_prop . grffile - > grfid = = grfid & & spec - > grf_prop . local_id = = local_id ) {
if ( index ! = nullptr ) * index = static_cast < int > ( std : : distance ( cls . spec . data ( ) , & spec ) ) ;
return spec ;
}
if ( spec - > grf_prop . grffile - > grfid = = grfid & & spec - > grf_prop . local_id = = local_id ) return spec ;
}
}