From 0658d70a6a3be23239e4804eff53c2db1dc60bf2 Mon Sep 17 00:00:00 2001 From: Nicolas Cornu Date: Mon, 1 Jul 2024 19:30:21 +0200 Subject: [PATCH] Replace AtomicSharedPtr by std::atomic> Since C++20 there is a specialization for std::shared_ptr in std::atomic. So this is no more needed (and deprecated) to use std::atomic_load, std::atomic_store. Signed-off-by: Nicolas Cornu --- base/CMakeLists.txt | 1 - base/atomic_shared_ptr.hpp | 27 ------------ editor/config_loader.cpp | 4 +- editor/config_loader.hpp | 5 +-- editor/editor_tests/config_loader_test.cpp | 6 +-- editor/osm_editor.cpp | 50 +++++++++++----------- editor/osm_editor.hpp | 5 +-- 7 files changed, 33 insertions(+), 65 deletions(-) delete mode 100644 base/atomic_shared_ptr.hpp diff --git a/base/CMakeLists.txt b/base/CMakeLists.txt index b2c0f07ed8..d313621fc9 100644 --- a/base/CMakeLists.txt +++ b/base/CMakeLists.txt @@ -9,7 +9,6 @@ set(SRC ../std/windows.hpp array_adapters.hpp assert.hpp - atomic_shared_ptr.hpp base.cpp base.hpp beam.hpp diff --git a/base/atomic_shared_ptr.hpp b/base/atomic_shared_ptr.hpp deleted file mode 100644 index 3059f0dc09..0000000000 --- a/base/atomic_shared_ptr.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#pragma once - -#include "base/macros.hpp" - -#include - -namespace base -{ -// Template which provides methods for concurrently using shared pointers. -template -class AtomicSharedPtr final -{ -public: - using ContentType = T const; - using ValueType = std::shared_ptr; - - AtomicSharedPtr() = default; - - void Set(ValueType value) noexcept { atomic_store(&m_wrapped, value); } - ValueType Get() const noexcept { return atomic_load(&m_wrapped); } - -private: - ValueType m_wrapped = std::make_shared(); - - DISALLOW_COPY_AND_MOVE(AtomicSharedPtr); -}; -} // namespace base diff --git a/editor/config_loader.cpp b/editor/config_loader.cpp index abbe4975ba..a114b43e54 100644 --- a/editor/config_loader.cpp +++ b/editor/config_loader.cpp @@ -63,7 +63,7 @@ void Waiter::Interrupt() m_event.notify_all(); } -ConfigLoader::ConfigLoader(base::AtomicSharedPtr & config) : m_config(config) +ConfigLoader::ConfigLoader(std::atomic> & config) : m_config(config) { pugi::xml_document doc; LoadFromLocal(doc); @@ -126,7 +126,7 @@ void ConfigLoader::ResetConfig(pugi::xml_document const & doc) { auto config = std::make_shared(); config->SetConfig(doc); - m_config.Set(config); + m_config = config; } // static diff --git a/editor/config_loader.hpp b/editor/config_loader.hpp index e8171b2213..e90e240dc1 100644 --- a/editor/config_loader.hpp +++ b/editor/config_loader.hpp @@ -1,6 +1,5 @@ #pragma once -#include "base/atomic_shared_ptr.hpp" #include "base/exception.hpp" #include "base/logging.hpp" @@ -50,7 +49,7 @@ private: class ConfigLoader { public: - explicit ConfigLoader(base::AtomicSharedPtr & config); + explicit ConfigLoader(std::atomic> & config); ~ConfigLoader(); // Static methods for production and testing. @@ -65,7 +64,7 @@ private: bool SaveAndReload(pugi::xml_document const & doc); void ResetConfig(pugi::xml_document const & doc); - base::AtomicSharedPtr & m_config; + std::atomic> & m_config; Waiter m_waiter; std::thread m_loaderThread; diff --git a/editor/editor_tests/config_loader_test.cpp b/editor/editor_tests/config_loader_test.cpp index 0c427d21c2..f9e27b6b39 100644 --- a/editor/editor_tests/config_loader_test.cpp +++ b/editor/editor_tests/config_loader_test.cpp @@ -5,8 +5,6 @@ #include "platform/platform_tests_support/scoped_file.hpp" -#include "base/atomic_shared_ptr.hpp" - #include namespace @@ -24,10 +22,10 @@ void CheckGeneralTags(pugi::xml_document const & doc) UNIT_TEST(ConfigLoader_Base) { - base::AtomicSharedPtr config; + std::atomic> config; ConfigLoader loader(config); - TEST(!config.Get()->GetTypesThatCanBeAdded().empty(), ()); + TEST(!config.load()->GetTypesThatCanBeAdded().empty(), ()); } // This functionality is not used and corresponding server is not working. diff --git a/editor/osm_editor.cpp b/editor/osm_editor.cpp index e4e9eb2a57..a109760650 100644 --- a/editor/osm_editor.cpp +++ b/editor/osm_editor.cpp @@ -164,7 +164,7 @@ void Editor::LoadEdits() if (!m_storage->Load(doc)) return; - m_features.Set(make_shared()); + m_features = make_shared(); auto loadedFeatures = make_shared(); auto rootNode = doc.child(kXmlRootNode); @@ -196,7 +196,7 @@ void Editor::LoadEdits() if (needRewriteEdits) SaveTransaction(loadedFeatures); else - m_features.Set(loadedFeatures); + m_features = loadedFeatures; } bool Editor::Save(FeaturesContainer const & features) const @@ -259,7 +259,7 @@ bool Editor::SaveTransaction(std::shared_ptr const & features if (!Save(*features)) return false; - m_features.Set(features); + m_features = features; return true; } @@ -279,19 +279,19 @@ void Editor::OnMapRegistered(platform::LocalCountryFile const &) FeatureStatus Editor::GetFeatureStatus(MwmId const & mwmId, uint32_t index) const { - auto const features = m_features.Get(); + auto const features = m_features.load(); return GetFeatureStatusImpl(*features, mwmId, index); } FeatureStatus Editor::GetFeatureStatus(FeatureID const & fid) const { - auto const features = m_features.Get(); + auto const features = m_features.load(); return GetFeatureStatusImpl(*features, fid.m_mwmId, fid.m_index); } bool Editor::IsFeatureUploaded(MwmId const & mwmId, uint32_t index) const { - auto const features = m_features.Get(); + auto const features = m_features.load(); return IsFeatureUploadedImpl(*features, mwmId, index); } @@ -299,7 +299,7 @@ void Editor::DeleteFeature(FeatureID const & fid) { CHECK_THREAD_CHECKER(MainThreadChecker, ("")); - auto const features = m_features.Get(); + auto const features = m_features.load(); auto editableFeatures = make_shared(*features); auto const mwm = editableFeatures->find(fid.m_mwmId); @@ -346,7 +346,7 @@ Editor::SaveResult Editor::SaveEditedFeature(EditableMapObject const & emo) FeatureID const & fid = emo.GetID(); FeatureTypeInfo fti; - auto const features = m_features.Get(); + auto const features = m_features.load(); auto const featureStatus = GetFeatureStatusImpl(*features, fid.m_mwmId, fid.m_index); ASSERT_NOT_EQUAL(featureStatus, FeatureStatus::Obsolete, ("Obsolete feature cannot be modified.")); @@ -445,7 +445,7 @@ bool Editor::RollBackChanges(FeatureID const & fid) void Editor::ForEachCreatedFeature(MwmId const & id, FeatureIndexFunctor const & f, m2::RectD const & rect, int /*scale*/) const { - auto const features = m_features.Get(); + auto const features = m_features.load(); auto const mwmFound = features->find(id); if (mwmFound == features->cend()) @@ -465,7 +465,7 @@ void Editor::ForEachCreatedFeature(MwmId const & id, FeatureIndexFunctor const & std::optional Editor::GetEditedFeature(FeatureID const & fid) const { - auto const features = m_features.Get(); + auto const features = m_features.load(); auto const * featureInfo = GetFeatureTypeInfo(*features, fid.m_mwmId, fid.m_index); if (featureInfo == nullptr) return {}; @@ -475,7 +475,7 @@ std::optional Editor::GetEditedFeature(FeatureID const & bool Editor::GetEditedFeatureStreet(FeatureID const & fid, string & outFeatureStreet) const { - auto const features = m_features.Get(); + auto const features = m_features.load(); auto const * featureInfo = GetFeatureTypeInfo(*features, fid.m_mwmId, fid.m_index); if (featureInfo == nullptr) return false; @@ -487,7 +487,7 @@ bool Editor::GetEditedFeatureStreet(FeatureID const & fid, string & outFeatureSt std::vector Editor::GetFeaturesByStatus(MwmId const & mwmId, FeatureStatus status) const { - auto const features = m_features.Get(); + auto const features = m_features.load(); std::vector result; auto const matchedMwm = features->find(mwmId); @@ -505,7 +505,7 @@ std::vector Editor::GetFeaturesByStatus(MwmId const & mwmId, EditableProperties Editor::GetEditableProperties(FeatureType & feature) const { - auto const features = m_features.Get(); + auto const features = m_features.load(); auto const & fid = feature.GetID(); auto const featureStatus = GetFeatureStatusImpl(*features, fid.m_mwmId, fid.m_index); @@ -542,7 +542,7 @@ EditableProperties Editor::GetEditableProperties(FeatureType & feature) const EditableProperties Editor::GetEditablePropertiesForTypes(feature::TypesHolder const & types) const { editor::TypeAggregatedDescription desc; - if (m_config.Get()->GetTypeDescription(types.ToObjectNames(), desc)) + if (m_config.load()->GetTypeDescription(types.ToObjectNames(), desc)) { return { std::move(desc.m_editableFields), desc.IsNameEditable(), desc.IsAddressEditable(), desc.IsCuisineEditable() }; @@ -555,7 +555,7 @@ bool Editor::HaveMapEditsOrNotesToUpload() const if (m_notes->NotUploadedNotesCount() != 0) return true; - auto const features = m_features.Get(); + auto const features = m_features.load(); return HaveMapEditsToUpload(*features); } @@ -564,7 +564,7 @@ bool Editor::HaveMapEditsToUpload(MwmId const & mwmId) const if (!mwmId.IsAlive()) return false; - auto const features = m_features.Get(); + auto const features = m_features.load(); auto const found = features->find(mwmId); if (found != features->cend()) @@ -584,7 +584,7 @@ void Editor::UploadChanges(string const & oauthToken, ChangesetTags tags, if (m_notes->NotUploadedNotesCount()) m_notes->Upload(OsmOAuth::ServerAuth(oauthToken)); - auto const features = m_features.Get(); + auto const features = m_features.load(); if (!HaveMapEditsToUpload(*features)) { @@ -596,7 +596,7 @@ void Editor::UploadChanges(string const & oauthToken, ChangesetTags tags, { int uploadedFeaturesCount = 0, errorsCount = 0; ChangesetWrapper changeset(secret, std::move(tags)); - auto const features = m_features.Get(); + auto const features = m_features.load(); for (auto const & id : *features) { @@ -786,7 +786,7 @@ void Editor::SaveUploadedInformation(FeatureID const & fid, UploadInfo const & u { CHECK_THREAD_CHECKER(MainThreadChecker, ("")); - auto const features = m_features.Get(); + auto const features = m_features.load(); auto editableFeatures = make_shared(*features); auto id = editableFeatures->find(fid.m_mwmId); @@ -860,7 +860,7 @@ bool Editor::RemoveFeatureIfExists(FeatureID const & fid) { CHECK_THREAD_CHECKER(MainThreadChecker, ("")); - auto const features = m_features.Get(); + auto const features = m_features.load(); auto editableFeatures = make_shared(*features); auto matchedMwm = editableFeatures->find(fid.m_mwmId); @@ -887,7 +887,7 @@ bool Editor::MarkFeatureAsObsolete(FeatureID const & fid) { CHECK_THREAD_CHECKER(MainThreadChecker, ("")); - auto const features = m_features.Get(); + auto const features = m_features.load(); auto editableFeatures = make_shared(*features); auto const featureStatus = GetFeatureStatusImpl(*editableFeatures, fid.m_mwmId, fid.m_index); @@ -921,7 +921,7 @@ Editor::Stats Editor::GetStats() const Stats stats; LOG(LDEBUG, ("Edited features status:")); - auto const features = m_features.Get(); + auto const features = m_features.load(); for (auto const & id : *features) { for (auto & index : id.second) @@ -947,7 +947,7 @@ Editor::Stats Editor::GetStats() const NewFeatureCategories Editor::GetNewFeatureCategories() const { - return NewFeatureCategories(*(m_config.Get())); + return NewFeatureCategories(*(m_config.load())); } FeatureID Editor::GenerateNewFeatureId(FeaturesContainer const & features, @@ -983,7 +983,7 @@ bool Editor::CreatePoint(uint32_t type, m2::PointD const & mercator, MwmId const } outFeature.SetMercator(mercator); - outFeature.SetID(GenerateNewFeatureId(*(m_features.Get()), id)); + outFeature.SetID(GenerateNewFeatureId(*(m_features.load()), id)); outFeature.SetType(type); outFeature.SetEditableProperties(GetEditablePropertiesForTypes(outFeature.GetTypes())); // Only point type features can be created at the moment. @@ -1010,7 +1010,7 @@ void Editor::CreateNote(ms::LatLon const & latLon, FeatureID const & fid, sstr << "The place has gone or never existed. A user of Organic Maps application has reported " "that the POI was visible on the map (see snapshot date below), but was not found " "on the ground.\n"; - auto const features = m_features.Get(); + auto const features = m_features.load(); auto const isCreated = GetFeatureStatusImpl(*features, fid.m_mwmId, fid.m_index) == FeatureStatus::Created; auto const createdAndUploaded = diff --git a/editor/osm_editor.hpp b/editor/osm_editor.hpp index 9ababde49d..2c90ca06df 100644 --- a/editor/osm_editor.hpp +++ b/editor/osm_editor.hpp @@ -14,7 +14,6 @@ #include "geometry/rect2d.hpp" -#include "base/atomic_shared_ptr.hpp" #include "base/thread_checker.hpp" #include "base/timer.hpp" @@ -236,7 +235,7 @@ private: static bool IsFeatureUploadedImpl(FeaturesContainer const & features, MwmId const & mwmId, uint32_t index); /// Deleted, edited and created features. - base::AtomicSharedPtr m_features; + std::atomic> m_features; std::unique_ptr m_delegate; @@ -244,7 +243,7 @@ private: InvalidateFn m_invalidateFn; /// Contains information about what and how can be edited. - base::AtomicSharedPtr m_config; + std::atomic> m_config; editor::ConfigLoader m_configLoader; /// Notes to be sent to osm. -- 2.45.3