diff --git a/libraries/hfm/src/hfm/HFMFormat.h b/libraries/hfm/src/hfm/HFMFormat.h new file mode 100644 index 0000000000..4430bca3f4 --- /dev/null +++ b/libraries/hfm/src/hfm/HFMFormat.h @@ -0,0 +1,25 @@ +// +// HFMFormat.h +// libraries/hfm/src/hfm +// +// Created by Sabrina Shanman on 2018/11/30. +// Copyright 2018 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_HFMFormat_h +#define hifi_HFMFormat_h + +#include "HFMFormatRegistry.h" + +namespace hfm { + class Format { + public: + virtual void registerFormat(FormatRegistry& registry) = 0; + virtual void unregisterFormat(FormatRegistry& registry) = 0; + }; +}; + +#endif // hifi_HFMFormat_h diff --git a/libraries/hfm/src/hfm/HFMFormatRegistry.cpp b/libraries/hfm/src/hfm/HFMFormatRegistry.cpp new file mode 100644 index 0000000000..08f13c414a --- /dev/null +++ b/libraries/hfm/src/hfm/HFMFormatRegistry.cpp @@ -0,0 +1,52 @@ +// +// HFMFormatRegistry.cpp +// libraries/hfm/src/hfm +// +// Created by Sabrina Shanman on 2018/11/29. +// Copyright 2018 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "HFMFormatRegistry.h" + +namespace hfm { + +FormatRegistry::MIMETypeID FormatRegistry::registerMIMEType(const MIMEType& mimeType, const std::shared_ptr& supportedFactory) { + MIMETypeID id = _mimeTypeLibrary.registerMIMEType(mimeType); + withWriteLock([&](){ + _supportedFormats.emplace_back(id, supportedFactory); + }); + return id; +} + +void FormatRegistry::unregisterMIMEType(const MIMETypeID& mimeTypeID) { + withWriteLock([&](){ + for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) { + if ((*it).mimeTypeID == mimeTypeID) { + _supportedFormats.erase(it); + break; + } + } + }); + _mimeTypeLibrary.unregisterMIMEType(mimeTypeID); +} + +std::shared_ptr FormatRegistry::getFactoryForMIMETypeID(FormatRegistry::MIMETypeID mimeTypeID) const { + return resultWithReadLock>([&](){ + for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) { + if ((*it).mimeTypeID == mimeTypeID) { + return (*it).factory; + } + } + return std::shared_ptr(); + }); +} + +std::shared_ptr FormatRegistry::getFactoryForMIMEType(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); + return getFactoryForMIMETypeID(id); +} + +}; diff --git a/libraries/hfm/src/hfm/HFMFormatRegistry.h b/libraries/hfm/src/hfm/HFMFormatRegistry.h new file mode 100644 index 0000000000..e807d98c46 --- /dev/null +++ b/libraries/hfm/src/hfm/HFMFormatRegistry.h @@ -0,0 +1,48 @@ +// +// HFMFormatRegistry.h +// libraries/hfm/src/hfm +// +// Created by Sabrina Shanman on 2018/11/28. +// Copyright 2018 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_HFMFormatRegistry_h +#define hifi_HFMFormatRegistry_h + +#include "HFMSerializer.h" +#include +#include + +namespace hfm { + +class FormatRegistry : public ReadWriteLockable { +public: + using MIMETypeID = MIMETypeLibrary::ID; + static const MIMETypeID INVALID_MIME_TYPE_ID { MIMETypeLibrary::INVALID_ID }; + + MIMETypeID registerMIMEType(const MIMEType& mimeType, const std::shared_ptr& supportedFactory); + void unregisterMIMEType(const MIMETypeID& id); + + std::shared_ptr getFactoryForMIMEType(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url, const std::string& webMediaType) const; + std::shared_ptr getFactoryForMIMETypeID(MIMETypeID id) const; + +protected: + MIMETypeLibrary _mimeTypeLibrary; + class SupportedFormat { + public: + SupportedFormat(const MIMETypeID& mimeTypeID, const std::shared_ptr& factory) : + mimeTypeID(mimeTypeID), + factory(factory) { + } + MIMETypeID mimeTypeID; + std::shared_ptr factory; + }; + std::vector _supportedFormats; +}; + +}; + +#endif // hifi_HFMFormatRegistry_h diff --git a/libraries/hfm/src/hfm/HFMSerializer.h b/libraries/hfm/src/hfm/HFMSerializer.h index db18f21e06..a8ff4a3fa0 100644 --- a/libraries/hfm/src/hfm/HFMSerializer.h +++ b/libraries/hfm/src/hfm/HFMSerializer.h @@ -19,6 +19,12 @@ namespace hfm { class Serializer { +public: + class Factory { + public: + virtual std::shared_ptr get() = 0; + }; + virtual Model::Pointer read(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url = hifi::URL()) = 0; };