| 1 |
/* |
| 2 |
* Copyright (C) 2019-2020 Rerrah |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person |
| 5 |
* obtaining a copy of this software and associated documentation |
| 6 |
* files (the "Software"), to deal in the Software without |
| 7 |
* restriction, including without limitation the rights to use, |
| 8 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
* copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following |
| 11 |
* conditions: |
| 12 |
* |
| 13 |
* The above copyright notice and this permission notice shall be |
| 14 |
* included in all copies or substantial portions of the Software. |
| 15 |
* |
| 16 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
*/ |
| 25 |
|
| 26 |
#pragma once |
| 27 |
|
| 28 |
#include <memory> |
| 29 |
#include <string> |
| 30 |
#include <vector> |
| 31 |
#include "module.hpp" |
| 32 |
#include "instruments_manager.hpp" |
| 33 |
#include "binary_container.hpp" |
| 34 |
#include "io_utils.hpp" |
| 35 |
|
| 36 |
namespace io |
| 37 |
{ |
| 38 |
class AbstractModuleIO |
| 39 |
{ |
| 40 |
public: |
| 41 |
AbstractModuleIO(const std::string& ext, const std::string& desc, bool loadable, bool savable) |
| 42 |
: ext_(ext), desc_(desc), loadable_(loadable), savable_(savable) {} |
| 43 |
virtual ~AbstractModuleIO() = default; |
| 44 |
virtual void load(const BinaryContainer& ctr, std::weak_ptr<Module> mod, |
| 45 |
std::weak_ptr<InstrumentsManager> instMan) const; |
| 46 |
virtual void save(BinaryContainer& ctr, const std::weak_ptr<Module> mod, |
| 47 |
const std::weak_ptr<InstrumentsManager> instMan) const; |
| 48 |
inline std::string getExtension() const noexcept { return ext_; } |
| 49 |
inline std::string getFilterText() const { return desc_ + " (*." + ext_ + ")"; } |
| 50 |
inline bool isLoadable() const noexcept { return loadable_; } |
| 51 |
inline bool isSavable() const noexcept { return savable_; } |
| 52 |
|
| 53 |
private: |
| 54 |
const std::string ext_, desc_; |
| 55 |
bool loadable_, savable_; |
| 56 |
}; |
| 57 |
|
| 58 |
class ModuleIO |
| 59 |
{ |
| 60 |
public: |
| 61 |
static ModuleIO& getInstance(); |
| 62 |
|
| 63 |
void saveModule(BinaryContainer& ctr, const std::weak_ptr<Module> mod, |
| 64 |
const std::weak_ptr<InstrumentsManager> instMan); |
| 65 |
void loadModule(const BinaryContainer& ctr, std::weak_ptr<Module> mod, |
| 66 |
std::weak_ptr<InstrumentsManager> instMan); |
| 67 |
|
| 68 |
inline bool testLoadableFormat(const std::string& ext) const |
| 69 |
{ |
| 70 |
return handler_.testLoadableExtension(ext); |
| 71 |
} |
| 72 |
|
| 73 |
inline bool testSavableFormat(const std::string& ext) const |
| 74 |
{ |
| 75 |
return handler_.testSavableExtension(ext); |
| 76 |
} |
| 77 |
|
| 78 |
inline std::vector<std::string> getLoadFilter() const |
| 79 |
{ |
| 80 |
return handler_.getLoadFilterList(); |
| 81 |
} |
| 82 |
|
| 83 |
inline std::vector<std::string> getSaveFilter() const |
| 84 |
{ |
| 85 |
return handler_.getSaveFilterList(); |
| 86 |
} |
| 87 |
|
| 88 |
private: |
| 89 |
ModuleIO(); |
| 90 |
|
| 91 |
static std::unique_ptr<ModuleIO> instance_; |
| 92 |
FileIOHandlerMap<AbstractModuleIO> handler_; |
| 93 |
}; |
| 94 |
} |