generic_factory: ordered init. of static globals

This commit is contained in:
andrei 2022-04-15 18:50:26 +03:00
parent 4a59e7d76d
commit 99afac529b
11 changed files with 89 additions and 56 deletions

View File

@ -6,53 +6,52 @@
#include "generic_factory.h"
#include "json.h"
namespace
generic_factory<ammo_effect> &get_all_ammo_effects()
{
generic_factory<ammo_effect> all_ammo_effects( "ammo effects" );
} // namespace
static generic_factory<ammo_effect> all_ammo_effects( "ammo effects" );
return all_ammo_effects;
}
/** @relates int_id */
template<>
bool int_id<ammo_effect>::is_valid() const
{
return all_ammo_effects.is_valid( *this );
return get_all_ammo_effects().is_valid( *this );
}
/** @relates int_id */
template<>
const ammo_effect &int_id<ammo_effect>::obj() const
{
return all_ammo_effects.obj( *this );
return get_all_ammo_effects().obj( *this );
}
/** @relates int_id */
template<>
const string_id<ammo_effect> &int_id<ammo_effect>::id() const
{
return all_ammo_effects.convert( *this );
return get_all_ammo_effects().convert( *this );
}
/** @relates string_id */
template<>
bool string_id<ammo_effect>::is_valid() const
{
return all_ammo_effects.is_valid( *this );
return get_all_ammo_effects().is_valid( *this );
}
/** @relates string_id */
template<>
const ammo_effect &string_id<ammo_effect>::obj() const
{
return all_ammo_effects.obj( *this );
return get_all_ammo_effects().obj( *this );
}
/** @relates string_id */
template<>
int_id<ammo_effect> string_id<ammo_effect>::id() const
{
return all_ammo_effects.convert( *this, AE_NULL );
return get_all_ammo_effects().convert( *this, AE_NULL );
}
/** @relates int_id */
@ -142,35 +141,35 @@ void ammo_effect::check() const
size_t ammo_effect::count()
{
return all_ammo_effects.size();
return get_all_ammo_effects().size();
}
void ammo_effects::load( const JsonObject &jo, const std::string &src )
{
all_ammo_effects.load( jo, src );
get_all_ammo_effects().load( jo, src );
}
void ammo_effects::finalize_all()
{
all_ammo_effects.finalize();
for( const ammo_effect &ae : all_ammo_effects.get_all() ) {
get_all_ammo_effects().finalize();
for( const ammo_effect &ae : get_all_ammo_effects().get_all() ) {
const_cast<ammo_effect &>( ae ).finalize();
}
}
void ammo_effects::check_consistency()
{
all_ammo_effects.check();
get_all_ammo_effects().check();
}
void ammo_effects::reset()
{
all_ammo_effects.reset();
get_all_ammo_effects().reset();
}
const std::vector<ammo_effect> &ammo_effects::get_all()
{
return all_ammo_effects.get_all();
return get_all_ammo_effects().get_all();
}
ammo_effect_id AE_NULL;

View File

@ -13,6 +13,8 @@
class JsonObject;
generic_factory<ammo_effect> &get_all_ammo_effects();
struct ammo_effect {
public:
void load( const JsonObject &jo, const std::string &src );

View File

@ -102,53 +102,52 @@ std::string enum_to_string<description_affix>( description_affix data )
} // namespace io
namespace
generic_factory<field_type> &get_all_field_types()
{
generic_factory<field_type> all_field_types( "field types" );
} // namespace
static generic_factory<field_type> all_field_types( "field types" );
return all_field_types;
}
/** @relates int_id */
template<>
bool int_id<field_type>::is_valid() const
{
return all_field_types.is_valid( *this );
return get_all_field_types().is_valid( *this );
}
/** @relates int_id */
template<>
const field_type &int_id<field_type>::obj() const
{
return all_field_types.obj( *this );
return get_all_field_types().obj( *this );
}
/** @relates int_id */
template<>
const string_id<field_type> &int_id<field_type>::id() const
{
return all_field_types.convert( *this );
return get_all_field_types().convert( *this );
}
/** @relates string_id */
template<>
bool string_id<field_type>::is_valid() const
{
return all_field_types.is_valid( *this );
return get_all_field_types().is_valid( *this );
}
/** @relates string_id */
template<>
const field_type &string_id<field_type>::obj() const
{
return all_field_types.obj( *this );
return get_all_field_types().obj( *this );
}
template<>
int_id<field_type> string_id<field_type>::id_or( const int_id<field_type> &fallback ) const
{
if( all_field_types.initialized ) {
return all_field_types.convert( *this, fallback, false );
if( get_all_field_types().initialized ) {
return get_all_field_types().convert( *this, fallback, false );
}
return fallback;
}
@ -157,7 +156,7 @@ int_id<field_type> string_id<field_type>::id_or( const int_id<field_type> &fallb
template<>
int_id<field_type> string_id<field_type>::id() const
{
return all_field_types.convert( *this, fd_null.id_or( int_id<field_type>() ) );
return get_all_field_types().convert( *this, fd_null.id_or( int_id<field_type>() ) );
}
/** @relates int_id */
@ -356,40 +355,40 @@ void field_type::check() const
size_t field_type::count()
{
return all_field_types.size();
return get_all_field_types().size();
}
void field_types::load( const JsonObject &jo, const std::string &src )
{
all_field_types.load( jo, src );
get_all_field_types().load( jo, src );
}
void field_types::finalize_all()
{
all_field_types.finalize();
for( const field_type &fd : all_field_types.get_all() ) {
get_all_field_types().finalize();
for( const field_type &fd : get_all_field_types().get_all() ) {
const_cast<field_type &>( fd ).finalize();
}
}
void field_types::check_consistency()
{
all_field_types.check();
get_all_field_types().check();
}
void field_types::reset()
{
all_field_types.reset();
get_all_field_types().reset();
}
const std::vector<field_type> &field_types::get_all()
{
return all_field_types.get_all();
return get_all_field_types().get_all();
}
field_type field_types::get_field_type_by_legacy_enum( int legacy_enum_id )
{
for( const auto &ft : all_field_types.get_all() ) {
for( const auto &ft : get_all_field_types().get_all() ) {
if( legacy_enum_id == ft.legacy_enum_id ) {
return ft;
}

View File

@ -54,6 +54,8 @@ struct enum_traits<description_affix> {
static constexpr description_affix last = description_affix::DESCRIPTION_AFFIX_NUM;
};
generic_factory<field_type> &get_all_field_types();
struct field_effect {
efftype_id id;
std::vector<std::pair<efftype_id, mod_id>> src;

View File

@ -41,6 +41,7 @@
#include "memory_fast.h"
#include "options.h"
#include "output.h"
#include "ordered_static_globals.h"
#include "path_info.h"
#include "rng.h"
#include "system_language.h"
@ -569,6 +570,7 @@ extern "C" int SDL_main( int argc, char **argv ) {
int main( int argc, const char *argv[] )
{
#endif
ordered_static_globals();
init_crash_handlers();
reset_floating_point_mode();

View File

@ -16,7 +16,11 @@
namespace Name
{
static std::map< nameFlags, std::vector< std::string > > names;
names_map &get_names()
{
static names_map names;
return names;
}
static const std::map< std::string, nameFlags > usage_flags = {
{ "given", nameFlags::IsGivenName },
@ -76,10 +80,10 @@ static void load( JsonIn &jsin )
// find group type and add name(s) to group
if( jo.has_array( "name" ) ) {
for( const std::string n : jo.get_array( "name" ) ) {
names[type].push_back( n );
get_names()[type].push_back( n );
}
} else {
names[type].push_back( jo.get_string( "name" ) );
get_names()[type].push_back( jo.get_string( "name" ) );
}
}
}
@ -93,11 +97,11 @@ void load_from_file( const std::string &filename )
//
// i.e. if searchFlag is [ Male|Family ]
// it will match any group with those two flags set, such as [ Unisex|Family ]
using names_vec = std::vector< decltype( names.cbegin() ) >;
using names_vec = std::vector < decltype( get_names().cbegin() ) >;
static names_vec get_matching_groups( nameFlags searchFlags )
{
names_vec matching_groups;
for( auto it = names.cbegin(), end = names.cend(); it != end; ++it ) {
for( auto it = get_names().cbegin(), end = get_names().cend(); it != end; ++it ) {
const nameFlags type = it->first;
if( ( searchFlags & type ) == searchFlags ) {
matching_groups.push_back( it );
@ -156,7 +160,7 @@ std::string generate( bool is_male )
void clear()
{
names.clear();
get_names().clear();
}
} // namespace Name

View File

@ -3,7 +3,9 @@
#define CATA_SRC_NAME_H
#include <iosfwd>
#include <map>
#include <string>
#include <vector>
template <typename E> struct enum_traits;
@ -32,6 +34,8 @@ struct enum_traits<nameFlags> {
namespace Name
{
using names_map = std::map< nameFlags, std::vector< std::string > >;
names_map &get_names();
/// Load names from given json file to use for generation
void load_from_file( const std::string &filename );

View File

@ -0,0 +1,14 @@
#include "ordered_static_globals.h"
#include "ammo_effect.h"
#include "field_type.h"
#include "name.h"
#include "overmap.h"
void ordered_static_globals()
{
Name::get_names();
get_city_factory();
get_all_field_types();
get_all_ammo_effects();
}

View File

@ -0,0 +1,6 @@
#ifndef CATA_SRC_ORDERED_STATIC_GLOBALS_H
#define CATA_SRC_ORDERED_STATIC_GLOBALS_H
void ordered_static_globals();
#endif // CATA_SRC_ORDERED_STATIC_GLOBALS_H

View File

@ -342,30 +342,29 @@ bool overmap_special_id::is_valid() const
return specials.is_valid( *this );
}
namespace
generic_factory<city> &get_city_factory()
{
generic_factory<city> city_factory( "city" );
} // namespace
static generic_factory<city> city_factory( "city" );
return city_factory;
}
/** @relates string_id */
template<>
const city &string_id<city>::obj() const
{
return city_factory.obj( *this );
return get_city_factory().obj( *this );
}
/** @relates string_id */
template<>
bool string_id<city>::is_valid() const
{
return city_factory.is_valid( *this );
return get_city_factory().is_valid( *this );
}
void city::load_city( const JsonObject &jo, const std::string &src )
{
city_factory.load( jo, src );
get_city_factory().load( jo, src );
}
void city::finalize()
@ -385,17 +384,17 @@ void city::finalize()
void city::check_consistency()
{
city_factory.check();
get_city_factory().check();
}
const std::vector<city> &city::get_all()
{
return city_factory.get_all();
return get_city_factory().get_all();
}
void city::reset()
{
city_factory.reset();
get_city_factory().reset();
}
void city::load( const JsonObject &jo, const std::string & )

View File

@ -46,6 +46,8 @@ template<typename Point>
struct directed_path;
} // namespace pf
generic_factory<city> &get_city_factory();
struct city {
void load( const JsonObject &, const std::string & );
void check() const;