Create hfm::FormatRegistry, for more general model format definition

This commit is contained in:
sabrina-shanman 2018-11-29 16:51:56 -08:00
parent 142e787a9c
commit d1ac501967
4 changed files with 131 additions and 0 deletions

View file

@ -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

View file

@ -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<Serializer::Factory>& 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<Serializer::Factory> FormatRegistry::getFactoryForMIMETypeID(FormatRegistry::MIMETypeID mimeTypeID) const {
return resultWithReadLock<std::shared_ptr<Serializer::Factory>>([&](){
for (auto it = _supportedFormats.begin(); it != _supportedFormats.end(); it++) {
if ((*it).mimeTypeID == mimeTypeID) {
return (*it).factory;
}
}
return std::shared_ptr<Serializer::Factory>();
});
}
std::shared_ptr<Serializer::Factory> 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);
}
};

View file

@ -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 <shared/MIMETypeLibrary.h>
#include <shared/ReadWriteLockable.h>
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<Serializer::Factory>& supportedFactory);
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::Factory> getFactoryForMIMETypeID(MIMETypeID id) const;
protected:
MIMETypeLibrary _mimeTypeLibrary;
class SupportedFormat {
public:
SupportedFormat(const MIMETypeID& mimeTypeID, const std::shared_ptr<Serializer::Factory>& factory) :
mimeTypeID(mimeTypeID),
factory(factory) {
}
MIMETypeID mimeTypeID;
std::shared_ptr<Serializer::Factory> factory;
};
std::vector<SupportedFormat> _supportedFormats;
};
};
#endif // hifi_HFMFormatRegistry_h

View file

@ -19,6 +19,12 @@
namespace hfm {
class Serializer {
public:
class Factory {
public:
virtual std::shared_ptr<Serializer> get() = 0;
};
virtual Model::Pointer read(const hifi::ByteArray& data, const hifi::VariantHash& mapping, const hifi::URL& url = hifi::URL()) = 0;
};