c++11 style singleton implementation - singleton_cxx11<T>

This commit is contained in:
Artem Pavlenko 2023-03-09 21:06:55 +00:00
parent c646d245b4
commit e1a09e48c4
13 changed files with 39 additions and 23 deletions

View file

@ -41,11 +41,10 @@ class datasource;
class parameters; class parameters;
class PluginInfo; class PluginInfo;
class MAPNIK_DECL datasource_cache : public singleton<datasource_cache, CreateStatic>, class MAPNIK_DECL datasource_cache : public singleton_cxx11<datasource_cache>,
private util::noncopyable private util::noncopyable
{ {
friend class CreateStatic<datasource_cache>; friend class singleton_cxx11<datasource_cache>;
public: public:
bool plugin_registered(const std::string& plugin_name) const; bool plugin_registered(const std::string& plugin_name) const;
std::vector<std::string> plugin_names() const; std::vector<std::string> plugin_names() const;
@ -66,7 +65,7 @@ class MAPNIK_DECL datasource_cache : public singleton<datasource_cache, CreateSt
mutable std::recursive_mutex instance_mutex_; mutable std::recursive_mutex instance_mutex_;
}; };
extern template class MAPNIK_DECL singleton<datasource_cache, CreateStatic>; extern template class MAPNIK_DECL singleton_cxx11<datasource_cache>;
} // namespace mapnik } // namespace mapnik

View file

@ -45,7 +45,7 @@ namespace mapnik {
// Global logger class that holds the configuration of severity, format // Global logger class that holds the configuration of severity, format
// and file/console redirection. // and file/console redirection.
class MAPNIK_DECL logger : public singleton<logger, CreateStatic>, class MAPNIK_DECL logger : public singleton_cxx11<logger>,
private util::noncopyable private util::noncopyable
{ {
public: public:

View file

@ -33,7 +33,7 @@
namespace mapnik { namespace mapnik {
template<typename product_type, typename key_type, typename... Args> template<typename product_type, typename key_type, typename... Args>
class factory : public singleton<factory<product_type, key_type, Args...>> class factory : public singleton_cxx11<factory<product_type, key_type, Args...>>
{ {
private: private:
using product_creator = product_type* (*)(Args...); using product_creator = product_type* (*)(Args...);

View file

@ -50,10 +50,10 @@ using face_set_ptr = std::unique_ptr<font_face_set>;
class font_face; class font_face;
using face_ptr = std::shared_ptr<font_face>; using face_ptr = std::shared_ptr<font_face>;
class MAPNIK_DECL freetype_engine : public singleton<freetype_engine, CreateUsingNew>, class MAPNIK_DECL freetype_engine : public singleton_cxx11<freetype_engine>,
private util::noncopyable private util::noncopyable
{ {
friend class CreateUsingNew<freetype_engine>; friend class singleton_cxx11<freetype_engine>;
friend class Map; friend class Map;
public: public:
@ -132,7 +132,7 @@ class MAPNIK_DECL face_manager
}; };
using face_manager_freetype = face_manager; using face_manager_freetype = face_manager;
extern template class MAPNIK_DECL singleton<freetype_engine, CreateUsingNew>; extern template class MAPNIK_DECL singleton_cxx11<freetype_engine>;
} // namespace mapnik } // namespace mapnik
#endif // MAPNIK_FONT_ENGINE_FREETYPE_HPP #endif // MAPNIK_FONT_ENGINE_FREETYPE_HPP

View file

@ -48,10 +48,10 @@ namespace mapnik {
using mapped_region_ptr = std::shared_ptr<boost::interprocess::mapped_region>; using mapped_region_ptr = std::shared_ptr<boost::interprocess::mapped_region>;
class MAPNIK_DECL mapped_memory_cache : public singleton<mapped_memory_cache, CreateStatic>, class MAPNIK_DECL mapped_memory_cache : public singleton_cxx11<mapped_memory_cache>,
private util::noncopyable private util::noncopyable
{ {
friend class CreateStatic<mapped_memory_cache>; friend class singleton_cxx11<mapped_memory_cache>;
std::unordered_map<std::string, mapped_region_ptr> cache_; std::unordered_map<std::string, mapped_region_ptr> cache_;
public: public:
@ -68,7 +68,7 @@ class MAPNIK_DECL mapped_memory_cache : public singleton<mapped_memory_cache, Cr
void clear(); void clear();
}; };
extern template class MAPNIK_DECL singleton<mapped_memory_cache, CreateStatic>; extern template class MAPNIK_DECL singleton_cxx11<mapped_memory_cache>;
} // namespace mapnik } // namespace mapnik

View file

@ -36,10 +36,10 @@ namespace mapnik {
struct marker; struct marker;
class MAPNIK_DECL marker_cache : public singleton<marker_cache, CreateUsingNew>, class MAPNIK_DECL marker_cache : public singleton_cxx11<marker_cache>,
private util::noncopyable private util::noncopyable
{ {
friend class CreateUsingNew<marker_cache>; friend class singleton_cxx11<marker_cache>;
private: private:
marker_cache(); marker_cache();

View file

@ -37,7 +37,7 @@ namespace formatting {
using from_xml_function_ptr = node_ptr (*)(xml_node const&, fontset_map const&); using from_xml_function_ptr = node_ptr (*)(xml_node const&, fontset_map const&);
class registry : public singleton<registry, CreateStatic>, class registry : public singleton_cxx11<registry>,
private util::noncopyable private util::noncopyable
{ {
public: public:

View file

@ -36,7 +36,7 @@ namespace placements {
using from_xml_function_ptr = text_placements_ptr (*)(xml_node const&, fontset_map const&, bool); using from_xml_function_ptr = text_placements_ptr (*)(xml_node const&, fontset_map const&, bool);
class registry : public singleton<registry, CreateStatic>, class registry : public singleton_cxx11<registry>,
private util::noncopyable private util::noncopyable
{ {
public: public:

View file

@ -62,6 +62,22 @@ class CreateStatic
static void destroy(volatile T* obj) { obj->~T(); } static void destroy(volatile T* obj) { obj->~T(); }
}; };
template<typename T>
class singleton_cxx11
{
public:
static T& instance()
{
static T instance;
return instance;
}
protected:
#ifdef MAPNIK_THREADSAFE
static std::mutex mutex_;
#endif
};
#ifdef __GNUC__ #ifdef __GNUC__
template<typename T, template<typename U> class CreatePolicy = CreateStatic> template<typename T, template<typename U> class CreatePolicy = CreateStatic>
class MAPNIK_DECL singleton class MAPNIK_DECL singleton
@ -130,6 +146,8 @@ class singleton
#ifdef MAPNIK_THREADSAFE #ifdef MAPNIK_THREADSAFE
template<typename T, template<typename U> class CreatePolicy> template<typename T, template<typename U> class CreatePolicy>
std::mutex singleton<T, CreatePolicy>::mutex_; std::mutex singleton<T, CreatePolicy>::mutex_;
template<typename T>
std::mutex singleton_cxx11<T>::mutex_;
#endif #endif
template<typename T, template<typename U> class CreatePolicy> template<typename T, template<typename U> class CreatePolicy>
std::atomic<T*> singleton<T, CreatePolicy>::pInstance_; std::atomic<T*> singleton<T, CreatePolicy>::pInstance_;

View file

@ -37,9 +37,8 @@
#include <string> #include <string>
#include <memory> #include <memory>
using mapnik::CreateStatic;
using mapnik::Pool; using mapnik::Pool;
using mapnik::singleton; using mapnik::singleton_cxx11;
template<typename T> template<typename T>
class ConnectionCreator class ConnectionCreator
@ -110,13 +109,13 @@ class ConnectionCreator
boost::optional<std::string> application_name_; boost::optional<std::string> application_name_;
}; };
class ConnectionManager : public singleton<ConnectionManager, CreateStatic> class ConnectionManager : public singleton_cxx11<ConnectionManager>
{ {
public: public:
using PoolType = Pool<Connection, ConnectionCreator>; using PoolType = Pool<Connection, ConnectionCreator>;
private: private:
friend class CreateStatic<ConnectionManager>; friend class singleton_cxx11<ConnectionManager>;
using ContType = std::map<std::string, std::shared_ptr<PoolType>>; using ContType = std::map<std::string, std::shared_ptr<PoolType>>;
using HolderType = std::shared_ptr<Connection>; using HolderType = std::shared_ptr<Connection>;

View file

@ -44,7 +44,7 @@ MAPNIK_DISABLE_WARNING_POP
namespace mapnik { namespace mapnik {
template class singleton<datasource_cache, CreateStatic>; template class singleton_cxx11<datasource_cache>;
extern datasource_ptr create_static_datasource(parameters const& params); extern datasource_ptr create_static_datasource(parameters const& params);
extern std::vector<std::string> get_static_datasource_names(); extern std::vector<std::string> get_static_datasource_names();

View file

@ -48,7 +48,7 @@ MAPNIK_DISABLE_WARNING_POP
#include <stdexcept> #include <stdexcept>
namespace mapnik { namespace mapnik {
template class MAPNIK_DECL singleton<freetype_engine, CreateUsingNew>; template class MAPNIK_DECL singleton_cxx11<freetype_engine>;
bool freetype_engine::is_font_file(std::string const& file_name) bool freetype_engine::is_font_file(std::string const& file_name)
{ {

View file

@ -37,7 +37,7 @@ MAPNIK_DISABLE_WARNING_POP
namespace mapnik { namespace mapnik {
template class singleton<mapped_memory_cache, CreateStatic>; template class singleton_cxx11<mapped_memory_cache>;
void mapped_memory_cache::clear() void mapped_memory_cache::clear()
{ {