Make Serializer::Factory stored by HFMFormatRegistry unique_ptr

This commit is contained in:
sabrina-shanman 2018-12-03 10:48:50 -08:00
parent 30c4830bd8
commit bf1c5f2fa5
4 changed files with 17 additions and 17 deletions

View file

@ -13,7 +13,7 @@
namespace hfm { namespace hfm {
FormatRegistry::MIMETypeID FormatRegistry::registerMIMEType(const MIMEType& mimeType, const std::shared_ptr<Serializer::Factory>& supportedFactory) { FormatRegistry::MIMETypeID FormatRegistry::registerMIMEType(const MIMEType& mimeType, std::unique_ptr<Serializer::Factory>& supportedFactory) {
MIMETypeID id = _mimeTypeLibrary.registerMIMEType(mimeType); MIMETypeID id = _mimeTypeLibrary.registerMIMEType(mimeType);
withWriteLock([&](){ withWriteLock([&](){
_supportedFormats.emplace_back(id, supportedFactory); _supportedFormats.emplace_back(id, supportedFactory);
@ -33,20 +33,20 @@ void FormatRegistry::unregisterMIMEType(const MIMETypeID& mimeTypeID) {
_mimeTypeLibrary.unregisterMIMEType(mimeTypeID); _mimeTypeLibrary.unregisterMIMEType(mimeTypeID);
} }
std::shared_ptr<Serializer::Factory> FormatRegistry::getFactoryForMIMETypeID(FormatRegistry::MIMETypeID mimeTypeID) const { std::shared_ptr<Serializer> FormatRegistry::getSerializerForMIMETypeID(FormatRegistry::MIMETypeID mimeTypeID) const {
return resultWithReadLock<std::shared_ptr<Serializer::Factory>>([&](){ return resultWithReadLock<std::shared_ptr<Serializer>>([&](){
for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) { for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) {
if ((*it).mimeTypeID == mimeTypeID) { if ((*it).mimeTypeID == mimeTypeID) {
return (*it).factory; return (*it).factory->get();
} }
} }
return std::shared_ptr<Serializer::Factory>(); return std::shared_ptr<Serializer>();
}); });
} }
std::shared_ptr<Serializer::Factory> FormatRegistry::getFactoryForMIMEType(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const { std::shared_ptr<Serializer> FormatRegistry::getSerializerForMIMEType(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const {
MIMETypeID id = _mimeTypeLibrary.findMatchingMIMEType(data, mapping, url, webMediaType); MIMETypeID id = _mimeTypeLibrary.findMatchingMIMEType(data, mapping, url, webMediaType);
return getFactoryForMIMETypeID(id); return getSerializerForMIMETypeID(id);
} }
}; };

View file

@ -23,22 +23,22 @@ public:
using MIMETypeID = MIMETypeLibrary::ID; using MIMETypeID = MIMETypeLibrary::ID;
static const MIMETypeID INVALID_MIME_TYPE_ID { MIMETypeLibrary::INVALID_ID }; static const MIMETypeID INVALID_MIME_TYPE_ID { MIMETypeLibrary::INVALID_ID };
MIMETypeID registerMIMEType(const MIMEType& mimeType, const std::shared_ptr<Serializer::Factory>& supportedFactory); MIMETypeID registerMIMEType(const MIMEType& mimeType, std::unique_ptr<Serializer::Factory>& supportedFactory);
void unregisterMIMEType(const MIMETypeID& id); void unregisterMIMEType(const MIMETypeID& id);
std::shared_ptr<Serializer::Factory> getFactoryForMIMEType(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const; std::shared_ptr<Serializer> getSerializerForMIMEType(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const;
std::shared_ptr<Serializer::Factory> getFactoryForMIMETypeID(MIMETypeID id) const; std::shared_ptr<Serializer> getSerializerForMIMETypeID(MIMETypeID id) const;
protected: protected:
MIMETypeLibrary _mimeTypeLibrary; MIMETypeLibrary _mimeTypeLibrary;
class SupportedFormat { class SupportedFormat {
public: public:
SupportedFormat(const MIMETypeID& mimeTypeID, const std::shared_ptr<Serializer::Factory>& factory) : SupportedFormat(const MIMETypeID& mimeTypeID, std::unique_ptr<Serializer::Factory>& factory) :
mimeTypeID(mimeTypeID), mimeTypeID(mimeTypeID),
factory(factory) { factory(std::move(factory)) {
} }
MIMETypeID mimeTypeID; MIMETypeID mimeTypeID;
std::shared_ptr<Serializer::Factory> factory; std::unique_ptr<Serializer::Factory> factory;
}; };
std::vector<SupportedFormat> _supportedFormats; std::vector<SupportedFormat> _supportedFormats;
}; };

View file

@ -24,7 +24,7 @@ namespace hfm {
template<typename T> template<typename T>
void SimpleFormat<T>::registerFormat(FormatRegistry& registry) { void SimpleFormat<T>::registerFormat(FormatRegistry& registry) {
_mimeTypeID = registry.registerMIMEType(_mimeType, std::make_shared<SimpleFactory<T>>()); _mimeTypeID = registry.registerMIMEType(_mimeType, std::make_unique<SimpleFactory<T>>());
} }
template<typename T> template<typename T>

View file

@ -16,9 +16,9 @@
hfm::Model::Pointer ModelLoader::load(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const { hfm::Model::Pointer ModelLoader::load(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const {
auto factory = DependencyManager::get<ModelFormatRegistry>()->getFactoryForMIMEType(data, mapping, url, webMediaType); auto serializer = DependencyManager::get<ModelFormatRegistry>()->getSerializerForMIMEType(data, mapping, url, webMediaType);
if (!factory) { if (!serializer) {
return hfm::Model::Pointer(); return hfm::Model::Pointer();
} }
return factory->get()->read(data, mapping, url); return serializer->read(data, mapping, url);
} }