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 PluginInfo;
class MAPNIK_DECL datasource_cache : public singleton<datasource_cache, CreateStatic>,
class MAPNIK_DECL datasource_cache : public singleton_cxx11<datasource_cache>,
private util::noncopyable
{
friend class CreateStatic<datasource_cache>;
friend class singleton_cxx11<datasource_cache>;
public:
bool plugin_registered(const std::string& plugin_name) 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_;
};
extern template class MAPNIK_DECL singleton<datasource_cache, CreateStatic>;
extern template class MAPNIK_DECL singleton_cxx11<datasource_cache>;
} // namespace mapnik

View file

@ -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<logger, CreateStatic>,
class MAPNIK_DECL logger : public singleton_cxx11<logger>,
private util::noncopyable
{
public:

View file

@ -33,7 +33,7 @@
namespace mapnik {
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:
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;
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
{
friend class CreateUsingNew<freetype_engine>;
friend class singleton_cxx11<freetype_engine>;
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<freetype_engine, CreateUsingNew>;
extern template class MAPNIK_DECL singleton_cxx11<freetype_engine>;
} // namespace mapnik
#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>;
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
{
friend class CreateStatic<mapped_memory_cache>;
friend class singleton_cxx11<mapped_memory_cache>;
std::unordered_map<std::string, mapped_region_ptr> cache_;
public:
@ -68,7 +68,7 @@ class MAPNIK_DECL mapped_memory_cache : public singleton<mapped_memory_cache, Cr
void clear();
};
extern template class MAPNIK_DECL singleton<mapped_memory_cache, CreateStatic>;
extern template class MAPNIK_DECL singleton_cxx11<mapped_memory_cache>;
} // namespace mapnik

View file

@ -36,10 +36,10 @@ namespace mapnik {
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
{
friend class CreateUsingNew<marker_cache>;
friend class singleton_cxx11<marker_cache>;
private:
marker_cache();

View file

@ -37,7 +37,7 @@ namespace formatting {
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
{
public:

View file

@ -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<registry, CreateStatic>,
class registry : public singleton_cxx11<registry>,
private util::noncopyable
{
public:

View file

@ -62,6 +62,22 @@ class CreateStatic
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__
template<typename T, template<typename U> class CreatePolicy = CreateStatic>
class MAPNIK_DECL singleton
@ -130,6 +146,8 @@ class singleton
#ifdef MAPNIK_THREADSAFE
template<typename T, template<typename U> class CreatePolicy>
std::mutex singleton<T, CreatePolicy>::mutex_;
template<typename T>
std::mutex singleton_cxx11<T>::mutex_;
#endif
template<typename T, template<typename U> class CreatePolicy>
std::atomic<T*> singleton<T, CreatePolicy>::pInstance_;

View file

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

View file

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

View file

@ -48,7 +48,7 @@ MAPNIK_DISABLE_WARNING_POP
#include <stdexcept>
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)
{

View file

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