| 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 "instruments_manager.hpp" |
| 32 |
#include "bank.hpp" |
| 33 |
#include "binary_container.hpp" |
| 34 |
#include "io_utils.hpp" |
| 35 |
|
| 36 |
namespace io |
| 37 |
{ |
| 38 |
class AbstractBankIO |
| 39 |
{ |
| 40 |
public: |
| 41 |
AbstractBankIO(const std::string& ext, const std::string& desc, bool loadable, bool savable) |
| 42 |
: ext_(ext), desc_(desc), loadable_(loadable), savable_(savable) {} |
| 43 |
virtual ~AbstractBankIO() = default; |
| 44 |
virtual AbstractBank* load(const BinaryContainer& ctr) const; |
| 45 |
virtual void save(BinaryContainer& ctr, const std::weak_ptr<InstrumentsManager> instMan, |
| 46 |
const std::vector<int>& instNums) const; |
| 47 |
inline std::string getExtension() const noexcept { return ext_; } |
| 48 |
inline std::string getFilterText() const { return desc_ + " (*." + ext_ + ")"; } |
| 49 |
inline bool isLoadable() const noexcept { return loadable_; } |
| 50 |
inline bool isSavable() const noexcept { return savable_; } |
| 51 |
|
| 52 |
private: |
| 53 |
const std::string ext_, desc_; |
| 54 |
bool loadable_, savable_; |
| 55 |
}; |
| 56 |
|
| 57 |
class BankIO |
| 58 |
{ |
| 59 |
public: |
| 60 |
static BankIO& getInstance(); |
| 61 |
|
| 62 |
void saveBank(BinaryContainer& ctr, const std::weak_ptr<InstrumentsManager> instMan, |
| 63 |
const std::vector<int>& instNums); |
| 64 |
AbstractBank* loadBank(const BinaryContainer& ctr, const std::string& path); |
| 65 |
|
| 66 |
inline bool testLoadableFormat(const std::string& ext) const |
| 67 |
{ |
| 68 |
return handler_.testLoadableExtension(ext); |
| 69 |
} |
| 70 |
|
| 71 |
inline bool testSavableFormat(const std::string& ext) const |
| 72 |
{ |
| 73 |
return handler_.testSavableExtension(ext); |
| 74 |
} |
| 75 |
|
| 76 |
inline std::vector<std::string> getLoadFilter() const |
| 77 |
{ |
| 78 |
return handler_.getLoadFilterList(); |
| 79 |
} |
| 80 |
|
| 81 |
inline std::vector<std::string> getSaveFilter() const |
| 82 |
{ |
| 83 |
return handler_.getSaveFilterList(); |
| 84 |
} |
| 85 |
|
| 86 |
private: |
| 87 |
BankIO(); |
| 88 |
|
| 89 |
static std::unique_ptr<BankIO> instance_; |
| 90 |
FileIOHandlerMap<AbstractBankIO> handler_; |
| 91 |
}; |
| 92 |
} |