Create SimpleFormat

This commit is contained in:
sabrina-shanman 2018-11-30 16:49:28 -08:00
parent 39d1a2be6f
commit 2178baf1a8
2 changed files with 82 additions and 0 deletions

View file

@ -0,0 +1,35 @@
//
// HFMSimpleFormat.cpp
// 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
//
#include "HFMSimpleFormat.h"
namespace hfm {
template<typename T>
std::shared_ptr<Serializer> SimpleFactory<T>::get() {
return std::make_shared<T>();
}
template<typename T>
SimpleFormat<T>::SimpleFormat(const MIMEType& mimeType) : Format(),
_mimeType(mimeType) {
}
template<typename T>
void SimpleFormat<T>::registerFormat(FormatRegistry& registry) {
_mimeTypeID = registry.registerMIMEType(_mimeType, std::make_shared<SimpleFactory<T>>());
}
template<typename T>
void SimpleFormat<T>::unregisterFormat(FormatRegistry& registry) {
registry.unregisterMIMEType(_mimeTypeID);
_mimeTypeID = hfm::FormatRegistry::INVALID_MIME_TYPE_ID;
}
};

View file

@ -0,0 +1,47 @@
//
// HFMSimpleFormat.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_HFMSimpleFormat_h
#define hifi_HFMSimpleFormat_h
#include "HFMFormat.h"
#include "HFMSerializer.h"
namespace hfm {
template<typename T>
class SimpleFactory : public Serializer::Factory {
std::shared_ptr<Serializer> get() override {
return std::make_shared<T>();
}
};
template<typename T> // T is an implementation of hfm::Serializer
class SimpleFormat : public Format {
public:
SimpleFormat(const MIMEType& mimeType) : Format(),
_mimeType(mimeType) {
}
void registerFormat(FormatRegistry& registry) override {
_mimeTypeID = registry.registerMIMEType(_mimeType, std::make_unique<SimpleFactory<T>>());
}
void unregisterFormat(FormatRegistry& registry) override {
registry.unregisterMIMEType(_mimeTypeID);
_mimeTypeID = hfm::FormatRegistry::INVALID_MIME_TYPE_ID;
}
protected:
MIMEType _mimeType;
hfm::FormatRegistry::MIMETypeID _mimeTypeID;
};
};
#endif // hifi_HFMSimpleFormat_h