From e2b295784b427cfa603dbed86e04503f4895901a Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Fri, 29 Dec 2023 23:31:22 +0700 Subject: [PATCH] fix some potential problem in the type_registry::for_each_type function --- ROADMAP.md | 1 + develop/singles/headers/meta.hpp/meta_all.hpp | 8 +++++--- headers/meta.hpp/meta_detail/state_registry.hpp | 2 +- headers/meta.hpp/meta_detail/type_registry.hpp | 6 ++++-- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ROADMAP.md b/ROADMAP.md index 1978c68..28f0b08 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -6,6 +6,7 @@ - fix all includes to work with the library more flexible - test and support shared libraries - add debug type names +- auto register destructor without class_ ## Version 1.0 diff --git a/develop/singles/headers/meta.hpp/meta_all.hpp b/develop/singles/headers/meta.hpp/meta_all.hpp index 9e0214b..6d37663 100644 --- a/develop/singles/headers/meta.hpp/meta_all.hpp +++ b/develop/singles/headers/meta.hpp/meta_all.hpp @@ -4050,8 +4050,10 @@ namespace meta_hpp::detail void for_each_type(F&& f) const { const locker lock; - for ( const any_type& type : types_ ) { - std::invoke(f, type); + // we use an index based for loop to avoid the iterator invalidation issues + // that can happen when adding a new type inside the loop + for ( std::size_t i{}; i < types_.size(); ++i ) { + std::invoke(f, types_[i]); } } @@ -4259,7 +4261,7 @@ namespace meta_hpp::detail void for_each_scope(F&& f) const { const locker lock; - for ( auto&& [name, scope] : scopes_ ) { + for ( auto&& [_, scope] : scopes_ ) { std::invoke(f, scope); } } diff --git a/headers/meta.hpp/meta_detail/state_registry.hpp b/headers/meta.hpp/meta_detail/state_registry.hpp index 09066e9..f32aa89 100644 --- a/headers/meta.hpp/meta_detail/state_registry.hpp +++ b/headers/meta.hpp/meta_detail/state_registry.hpp @@ -41,7 +41,7 @@ namespace meta_hpp::detail void for_each_scope(F&& f) const { const locker lock; - for ( auto&& [name, scope] : scopes_ ) { + for ( auto&& [_, scope] : scopes_ ) { std::invoke(f, scope); } } diff --git a/headers/meta.hpp/meta_detail/type_registry.hpp b/headers/meta.hpp/meta_detail/type_registry.hpp index 149380b..50c95ec 100644 --- a/headers/meta.hpp/meta_detail/type_registry.hpp +++ b/headers/meta.hpp/meta_detail/type_registry.hpp @@ -41,8 +41,10 @@ namespace meta_hpp::detail void for_each_type(F&& f) const { const locker lock; - for ( const any_type& type : types_ ) { - std::invoke(f, type); + // we use an index based for loop to avoid the iterator invalidation issues + // that can happen when adding a new type inside the loop + for ( std::size_t i{}; i < types_.size(); ++i ) { + std::invoke(f, types_[i]); } }