diff --git a/include/mbgl/util/factory.hpp b/include/mbgl/util/factory.hpp index eb1c22933..1ffcac6f9 100644 --- a/include/mbgl/util/factory.hpp +++ b/include/mbgl/util/factory.hpp @@ -1,10 +1,9 @@ #ifndef MBGL_UTIL_FACTORY_HPP #define MBGL_UTIL_FACTORY_HPP -#include "singleton.hpp" - // stl #include +#include #include namespace mbgl { namespace util { @@ -14,27 +13,28 @@ template typename product_type, typename key_type, typename ...Args > -class factory : public singleton > +class factory { -private: +public: using product_creator = product_type* (*) (Args...); using product_map = std::map; - product_map map_; + static product_map map_; + static std::mutex mutex_; public: - bool register_product(key_type const& key, product_creator creator) + static bool register_product(key_type const& key, product_creator creator) { + std::unique_lock lock(mutex_); return map_.insert(typename product_map::value_type(key,creator)).second; } - bool unregister_product(const key_type& key) + static bool unregister_product(const key_type& key) { + std::unique_lock lock(mutex_); return map_.erase(key)==1; } - product_type* create_object(key_type const& key, Args...args) + static product_type* create_object(key_type const& key, Args...args) { typename product_map::const_iterator pos=map_.find(key); if (pos!=map_.end()) @@ -45,6 +45,11 @@ public: } }; +template +typename factory::product_map factory::map_; +template +typename std::mutex factory::mutex_; + }} #endif diff --git a/include/mbgl/util/image_reader.hpp b/include/mbgl/util/image_reader.hpp index 4e8043946..144127b76 100644 --- a/include/mbgl/util/image_reader.hpp +++ b/include/mbgl/util/image_reader.hpp @@ -38,11 +38,9 @@ struct image_reader : private noncopyable template bool register_image_reader(std::string const& type, image_reader* (* fun)(Args...)) { - return factory::instance().register_product(type, fun); + return factory::register_product(type, fun); } -//image_reader* get_image_reader(std::string const& file,std::string const& type); -//image_reader* get_image_reader(std::string const& file); image_reader* get_image_reader(char const* data, size_t size); }} diff --git a/platform/default/image_reader.cpp b/platform/default/image_reader.cpp index 75d7b0c13..53ab5758a 100644 --- a/platform/default/image_reader.cpp +++ b/platform/default/image_reader.cpp @@ -43,25 +43,10 @@ image_reader* get_image_reader(char const* data, size_t size) boost::optional type = type_from_bytes(data,size); if (type) { - return factory::instance().create_object(*type, data,size); + return factory::create_object(*type, data,size); } else throw image_reader_exception("image_reader: can't determine type from input data"); } -//image_reader* get_image_reader(std::string const& filename,std::string const& type) -//{ -// return factory::instance().create_object(type,filename); -//} - -//image_reader* get_image_reader(std::string const& filename) -//{ - //boost::optional type = type_from_filename(filename); - //if (type) - //{ - // return factory::instance().create_object(*type,filename); - //} -// return 0; -//} - }}