From e1a09e48c45d3198ab84a76ff6373c3e462a6c4c Mon Sep 17 00:00:00 2001 From: Artem Pavlenko Date: Thu, 9 Mar 2023 21:06:55 +0000 Subject: [PATCH] c++11 style singleton implementation - singleton_cxx11 --- include/mapnik/datasource_cache.hpp | 7 +++---- include/mapnik/debug.hpp | 2 +- include/mapnik/factory.hpp | 2 +- include/mapnik/font_engine_freetype.hpp | 6 +++--- include/mapnik/mapped_memory_cache.hpp | 6 +++--- include/mapnik/marker_cache.hpp | 4 ++-- include/mapnik/text/formatting/registry.hpp | 2 +- include/mapnik/text/placements/registry.hpp | 2 +- include/mapnik/util/singleton.hpp | 18 ++++++++++++++++++ plugins/input/postgis/connection_manager.hpp | 7 +++---- src/datasource_cache.cpp | 2 +- src/font_engine_freetype.cpp | 2 +- src/mapped_memory_cache.cpp | 2 +- 13 files changed, 39 insertions(+), 23 deletions(-) diff --git a/include/mapnik/datasource_cache.hpp b/include/mapnik/datasource_cache.hpp index 4e321c9ad..38a5ad918 100644 --- a/include/mapnik/datasource_cache.hpp +++ b/include/mapnik/datasource_cache.hpp @@ -41,11 +41,10 @@ class datasource; class parameters; class PluginInfo; -class MAPNIK_DECL datasource_cache : public singleton, +class MAPNIK_DECL datasource_cache : public singleton_cxx11, private util::noncopyable { - friend class CreateStatic; - + friend class singleton_cxx11; public: bool plugin_registered(const std::string& plugin_name) const; std::vector plugin_names() const; @@ -66,7 +65,7 @@ class MAPNIK_DECL datasource_cache : public singleton; +extern template class MAPNIK_DECL singleton_cxx11; } // namespace mapnik diff --git a/include/mapnik/debug.hpp b/include/mapnik/debug.hpp index 161bb8298..4ce37f081 100644 --- a/include/mapnik/debug.hpp +++ b/include/mapnik/debug.hpp @@ -45,7 +45,7 @@ namespace mapnik { // Global logger class that holds the configuration of severity, format // and file/console redirection. -class MAPNIK_DECL logger : public singleton, +class MAPNIK_DECL logger : public singleton_cxx11, private util::noncopyable { public: diff --git a/include/mapnik/factory.hpp b/include/mapnik/factory.hpp index 680b5dbe0..c9751c9cb 100644 --- a/include/mapnik/factory.hpp +++ b/include/mapnik/factory.hpp @@ -33,7 +33,7 @@ namespace mapnik { template -class factory : public singleton> +class factory : public singleton_cxx11> { private: using product_creator = product_type* (*)(Args...); diff --git a/include/mapnik/font_engine_freetype.hpp b/include/mapnik/font_engine_freetype.hpp index d2c0d034c..033b8bcc1 100644 --- a/include/mapnik/font_engine_freetype.hpp +++ b/include/mapnik/font_engine_freetype.hpp @@ -50,10 +50,10 @@ using face_set_ptr = std::unique_ptr; class font_face; using face_ptr = std::shared_ptr; -class MAPNIK_DECL freetype_engine : public singleton, +class MAPNIK_DECL freetype_engine : public singleton_cxx11, private util::noncopyable { - friend class CreateUsingNew; + friend class singleton_cxx11; friend class Map; public: @@ -132,7 +132,7 @@ class MAPNIK_DECL face_manager }; using face_manager_freetype = face_manager; -extern template class MAPNIK_DECL singleton; +extern template class MAPNIK_DECL singleton_cxx11; } // namespace mapnik #endif // MAPNIK_FONT_ENGINE_FREETYPE_HPP diff --git a/include/mapnik/mapped_memory_cache.hpp b/include/mapnik/mapped_memory_cache.hpp index 4b3e01797..3ab74425d 100644 --- a/include/mapnik/mapped_memory_cache.hpp +++ b/include/mapnik/mapped_memory_cache.hpp @@ -48,10 +48,10 @@ namespace mapnik { using mapped_region_ptr = std::shared_ptr; -class MAPNIK_DECL mapped_memory_cache : public singleton, +class MAPNIK_DECL mapped_memory_cache : public singleton_cxx11, private util::noncopyable { - friend class CreateStatic; + friend class singleton_cxx11; std::unordered_map cache_; public: @@ -68,7 +68,7 @@ class MAPNIK_DECL mapped_memory_cache : public singleton; +extern template class MAPNIK_DECL singleton_cxx11; } // namespace mapnik diff --git a/include/mapnik/marker_cache.hpp b/include/mapnik/marker_cache.hpp index 5fb2c929f..7b9ba89cb 100644 --- a/include/mapnik/marker_cache.hpp +++ b/include/mapnik/marker_cache.hpp @@ -36,10 +36,10 @@ namespace mapnik { struct marker; -class MAPNIK_DECL marker_cache : public singleton, +class MAPNIK_DECL marker_cache : public singleton_cxx11, private util::noncopyable { - friend class CreateUsingNew; + friend class singleton_cxx11; private: marker_cache(); diff --git a/include/mapnik/text/formatting/registry.hpp b/include/mapnik/text/formatting/registry.hpp index c7b5dfdaa..423967915 100644 --- a/include/mapnik/text/formatting/registry.hpp +++ b/include/mapnik/text/formatting/registry.hpp @@ -37,7 +37,7 @@ namespace formatting { using from_xml_function_ptr = node_ptr (*)(xml_node const&, fontset_map const&); -class registry : public singleton, +class registry : public singleton_cxx11, private util::noncopyable { public: diff --git a/include/mapnik/text/placements/registry.hpp b/include/mapnik/text/placements/registry.hpp index 1967a923b..918d903ce 100644 --- a/include/mapnik/text/placements/registry.hpp +++ b/include/mapnik/text/placements/registry.hpp @@ -36,7 +36,7 @@ namespace placements { using from_xml_function_ptr = text_placements_ptr (*)(xml_node const&, fontset_map const&, bool); -class registry : public singleton, +class registry : public singleton_cxx11, private util::noncopyable { public: diff --git a/include/mapnik/util/singleton.hpp b/include/mapnik/util/singleton.hpp index 897fcee69..7e920c8e1 100644 --- a/include/mapnik/util/singleton.hpp +++ b/include/mapnik/util/singleton.hpp @@ -62,6 +62,22 @@ class CreateStatic static void destroy(volatile T* obj) { obj->~T(); } }; +template +class singleton_cxx11 +{ +public: + static T& instance() + { + static T instance; + return instance; + } +protected: +#ifdef MAPNIK_THREADSAFE + static std::mutex mutex_; +#endif +}; + + #ifdef __GNUC__ template class CreatePolicy = CreateStatic> class MAPNIK_DECL singleton @@ -130,6 +146,8 @@ class singleton #ifdef MAPNIK_THREADSAFE template class CreatePolicy> std::mutex singleton::mutex_; +template +std::mutex singleton_cxx11::mutex_; #endif template class CreatePolicy> std::atomic singleton::pInstance_; diff --git a/plugins/input/postgis/connection_manager.hpp b/plugins/input/postgis/connection_manager.hpp index d8014d0bf..a5b005d28 100644 --- a/plugins/input/postgis/connection_manager.hpp +++ b/plugins/input/postgis/connection_manager.hpp @@ -37,9 +37,8 @@ #include #include -using mapnik::CreateStatic; using mapnik::Pool; -using mapnik::singleton; +using mapnik::singleton_cxx11; template class ConnectionCreator @@ -110,13 +109,13 @@ class ConnectionCreator boost::optional application_name_; }; -class ConnectionManager : public singleton +class ConnectionManager : public singleton_cxx11 { public: using PoolType = Pool; private: - friend class CreateStatic; + friend class singleton_cxx11; using ContType = std::map>; using HolderType = std::shared_ptr; diff --git a/src/datasource_cache.cpp b/src/datasource_cache.cpp index 5539e3fc6..5857bfcd7 100644 --- a/src/datasource_cache.cpp +++ b/src/datasource_cache.cpp @@ -44,7 +44,7 @@ MAPNIK_DISABLE_WARNING_POP namespace mapnik { -template class singleton; +template class singleton_cxx11; extern datasource_ptr create_static_datasource(parameters const& params); extern std::vector get_static_datasource_names(); diff --git a/src/font_engine_freetype.cpp b/src/font_engine_freetype.cpp index c905aaedd..05411893b 100644 --- a/src/font_engine_freetype.cpp +++ b/src/font_engine_freetype.cpp @@ -48,7 +48,7 @@ MAPNIK_DISABLE_WARNING_POP #include namespace mapnik { -template class MAPNIK_DECL singleton; +template class MAPNIK_DECL singleton_cxx11; bool freetype_engine::is_font_file(std::string const& file_name) { diff --git a/src/mapped_memory_cache.cpp b/src/mapped_memory_cache.cpp index 8da68a1c5..828c1b3e5 100644 --- a/src/mapped_memory_cache.cpp +++ b/src/mapped_memory_cache.cpp @@ -37,7 +37,7 @@ MAPNIK_DISABLE_WARNING_POP namespace mapnik { -template class singleton; +template class singleton_cxx11; void mapped_memory_cache::clear() {