Debian Codegraph @

BambooTracker/io/instrument_io.hpp

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