simplify factory class to not rely on singleton + cleanups

This commit is contained in:
artemp
2014-11-14 12:23:27 +01:00
parent e3adc4664b
commit 761b2ba583
3 changed files with 17 additions and 29 deletions

View File

@@ -1,10 +1,9 @@
#ifndef MBGL_UTIL_FACTORY_HPP
#define MBGL_UTIL_FACTORY_HPP
#include "singleton.hpp"
// stl
#include <stdexcept>
#include <mutex>
#include <map>
namespace mbgl { namespace util {
@@ -14,27 +13,28 @@ template
typename product_type,
typename key_type,
typename ...Args >
class factory : public singleton<factory <product_type,
key_type,
Args...> >
class factory
{
private:
public:
using product_creator = product_type* (*) (Args...);
using product_map = std::map<key_type,product_creator>;
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<std::mutex> 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<std::mutex> 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 product_type, typename key_type, typename ...Args>
typename factory<product_type, key_type, Args...>::product_map factory<product_type, key_type, Args...>::map_;
template <typename product_type, typename key_type, typename ...Args>
typename std::mutex factory<product_type, key_type, Args...>::mutex_;
}}
#endif

View File

@@ -38,11 +38,9 @@ struct image_reader : private noncopyable
template <typename...Args>
bool register_image_reader(std::string const& type, image_reader* (* fun)(Args...))
{
return factory<image_reader,std::string, Args...>::instance().register_product(type, fun);
return factory<image_reader,std::string, Args...>::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);
}}

View File

@@ -43,25 +43,10 @@ image_reader* get_image_reader(char const* data, size_t size)
boost::optional<std::string> type = type_from_bytes(data,size);
if (type)
{
return factory<image_reader,std::string,char const*,size_t>::instance().create_object(*type, data,size);
return factory<image_reader,std::string,char const*,size_t>::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<image_reader,std::string,std::string const&>::instance().create_object(type,filename);
//}
//image_reader* get_image_reader(std::string const& filename)
//{
//boost::optional<std::string> type = type_from_filename(filename);
//if (type)
//{
// return factory<image_reader,std::string,std::string const&>::instance().create_object(*type,filename);
//}
// return 0;
//}
}}