datasource.h (3471B)
1 #pragma once 2 3 #include <string> 4 5 #include "patchdbtypes.h" 6 7 namespace baseLib 8 { 9 class BinaryStream; 10 } 11 12 namespace pluginLib::patchDB 13 { 14 struct PatchKey; 15 16 struct DataSource 17 { 18 SourceType type = SourceType::Invalid; 19 DataSourceOrigin origin = DataSourceOrigin::Invalid; 20 std::string name; 21 uint32_t bank = g_invalidBank; 22 // uint32_t program = g_invalidProgram; 23 Timestamp timestamp; 24 std::set<PatchPtr> patches; 25 26 virtual ~DataSource() = default; 27 28 bool createConsecutiveProgramNumbers(); // returns true if any patch was modified 29 static bool createConsecutiveProgramNumbers(const std::vector<PatchPtr>& _patches); // returns true if any patch was modified 30 31 bool makeSpaceForNewPatches(uint32_t _insertPosition, uint32_t _count) const; 32 33 std::pair<uint32_t, uint32_t> getProgramNumberRange() const; 34 uint32_t getMaxProgramNumber() const; 35 static void sortByProgram(std::vector<PatchPtr>& _patches); 36 37 bool contains(const PatchPtr& _patch) const; 38 39 template<typename T> 40 bool containsAll(const T& _patches) const 41 { 42 for (auto p : _patches) 43 { 44 if(!contains(p)) 45 return false; 46 } 47 return true; 48 } 49 50 template<typename T> 51 bool containsAny(const T& _patches) const 52 { 53 for (auto p : _patches) 54 { 55 if(contains(p)) 56 return true; 57 } 58 return false; 59 } 60 61 bool movePatchesTo(uint32_t _position, const std::vector<PatchPtr>& _patches); 62 63 template<typename T> 64 bool remove(const T& _patches) 65 { 66 if(!containsAll(_patches)) 67 return false; 68 69 for (const auto& patch : _patches) 70 patches.erase(patch); 71 72 return true; 73 } 74 75 bool remove(const PatchPtr& _patch); 76 77 bool operator == (const DataSource& _ds) const 78 { 79 return type == _ds.type && name == _ds.name && bank == _ds.bank;//&& program == _ds.program; 80 } 81 82 bool operator != (const DataSource& _ds) const 83 { 84 return !(*this == _ds); 85 } 86 87 PatchPtr getPatch(const PatchKey& _key) const; 88 89 bool operator < (const DataSource& _ds) const 90 { 91 // if (parent < _ds.parent) return true; 92 // if (parent > _ds.parent) return false; 93 94 if (type < _ds.type) return true; 95 if (type > _ds.type) return false; 96 97 if (bank < _ds.bank) return true; 98 if (bank > _ds.bank) return false; 99 100 /* 101 if (program < _ds.program) return true; 102 if (program > _ds.program) return false; 103 */ 104 105 if (name < _ds.name) return true; 106 if (name > _ds.name) return false; 107 108 return false; 109 } 110 111 bool operator > (const DataSource& _ds) const 112 { 113 return _ds < *this; 114 } 115 116 std::string toString() const; 117 118 void write(baseLib::BinaryStream& _outStream) const; 119 bool read(baseLib::BinaryStream& _inStream); 120 }; 121 122 struct DataSourceNode final : DataSource, std::enable_shared_from_this<DataSourceNode> 123 { 124 DataSourceNode() = default; 125 DataSourceNode(const DataSourceNode&) = delete; 126 127 explicit DataSourceNode(const DataSource& _ds); 128 explicit DataSourceNode(DataSourceNode&&) = delete; 129 130 ~DataSourceNode() override; 131 132 DataSourceNode& operator = (const DataSourceNode&) = delete; 133 DataSourceNode& operator = (DataSourceNode&& _other) noexcept = delete; 134 135 auto& getParent() const { return m_parent; } 136 auto hasParent() const { return getParent() != nullptr; } 137 const auto& getChildren() const { return m_children; } 138 139 void setParent(const DataSourceNodePtr& _parent); 140 141 bool isChildOf(const DataSourceNode* _ds) const; 142 void removeAllChildren(); 143 144 private: 145 DataSourceNodePtr m_parent; 146 147 std::vector<std::weak_ptr<DataSourceNode>> m_children; 148 }; 149 }