add state registry mutex

This commit is contained in:
BlackMATov
2022-02-12 05:06:53 +07:00
parent c9a6f2d071
commit 201fdbf1cb
2 changed files with 13 additions and 8 deletions

View File

@@ -19,28 +19,29 @@ namespace meta_hpp::detail
} }
[[nodiscard]] scope get_scope_by_name(std::string_view name) const noexcept { [[nodiscard]] scope get_scope_by_name(std::string_view name) const noexcept {
const std::lock_guard<std::mutex> lock{mutex_};
if ( auto iter = scopes_.find(name); iter != scopes_.end() ) { if ( auto iter = scopes_.find(name); iter != scopes_.end() ) {
return iter->second; return iter->second;
} }
return scope{}; return scope{};
} }
[[nodiscard]] scope resolve_scope(std::string_view name) { [[nodiscard]] scope resolve_scope(std::string_view name) {
return ensure_scope(name); const std::lock_guard<std::mutex> lock{mutex_};
}
private:
state_registry() = default;
scope ensure_scope(std::string_view name) {
if ( auto iter = scopes_.find(name); iter != scopes_.end() ) { if ( auto iter = scopes_.find(name); iter != scopes_.end() ) {
return iter->second; return iter->second;
} }
return scopes_.emplace( auto state = scope_state::make(std::string{name}, metadata_map{});
std::string{name}, return scopes_.emplace(std::string{name}, std::move(state)).first->second;
scope_state::make(std::string{name})).first->second;
} }
private: private:
state_registry() = default;
private:
mutable std::mutex mutex_;
std::map<std::string, scope, std::less<>> scopes_; std::map<std::string, scope, std::less<>> scopes_;
}; };
} }

View File

@@ -20,17 +20,21 @@ namespace meta_hpp::detail
[[nodiscard]] any_type get_type_by_id(type_id id) const noexcept { [[nodiscard]] any_type get_type_by_id(type_id id) const noexcept {
const std::lock_guard<std::mutex> lock{mutex_}; const std::lock_guard<std::mutex> lock{mutex_};
if ( auto iter = type_by_id_.find(id); iter != type_by_id_.end() ) { if ( auto iter = type_by_id_.find(id); iter != type_by_id_.end() ) {
return iter->second; return iter->second;
} }
return any_type{}; return any_type{};
} }
[[nodiscard]] any_type get_type_by_rtti(const std::type_index& index) const noexcept { [[nodiscard]] any_type get_type_by_rtti(const std::type_index& index) const noexcept {
const std::lock_guard<std::mutex> lock{mutex_}; const std::lock_guard<std::mutex> lock{mutex_};
if ( auto iter = type_by_rtti_.find(index); iter != type_by_rtti_.end() ) { if ( auto iter = type_by_rtti_.find(index); iter != type_by_rtti_.end() ) {
return iter->second; return iter->second;
} }
return any_type{}; return any_type{};
} }