Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-Studio-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 :
23 : #pragma once
24 :
25 : #include <list>
26 : #include <string>
27 :
28 : #include "types/string.h"
29 : #include "types/bytearray.h"
30 : #include "io/path.h"
31 :
32 : #include "modularity/ioc.h"
33 : #include "global/icryptographichash.h"
34 :
35 : namespace mu::engraving {
36 : class Image;
37 : class Score;
38 :
39 : //---------------------------------------------------------
40 : // ImageStoreItem
41 : //---------------------------------------------------------
42 :
43 : class ImageStoreItem
44 : {
45 : muse::GlobalInject<muse::ICryptographicHash> cryptographicHash;
46 :
47 : public:
48 : ImageStoreItem(const std::string& p);
49 : void dereference(Image*);
50 : void reference(Image*);
51 :
52 21 : muse::ByteArray& buffer() { return m_buffer; }
53 0 : const muse::ByteArray& buffer() const { return m_buffer; }
54 : bool loaded() const { return !m_buffer.empty(); }
55 : bool isUsed(Score*) const;
56 14 : bool isUsed() const { return !m_references.empty(); }
57 : std::string hashName() const;
58 19 : const muse::ByteArray& hash() const { return m_hash; }
59 0 : const std::string& type() const { return m_type; }
60 2 : void set(const muse::ByteArray& b, const muse::ByteArray& h) { m_buffer = b; m_hash = h; }
61 :
62 : private:
63 :
64 : std::list<Image*> m_references;
65 : std::string m_type; // image type (file extension)
66 : muse::ByteArray m_buffer;
67 : muse::ByteArray m_hash; // 16 byte md4 hash of _buffer
68 : };
69 :
70 : //---------------------------------------------------------
71 : // ImageStore
72 : //---------------------------------------------------------
73 :
74 : class ImageStore
75 : {
76 : muse::GlobalInject<muse::ICryptographicHash> cryptographicHash;
77 :
78 : public:
79 19 : ImageStore() = default;
80 : ImageStore(const ImageStore&) = delete;
81 : ImageStore& operator=(const ImageStore&) = delete;
82 : ~ImageStore();
83 :
84 : ImageStoreItem* getImage(std::string name) const;
85 : ImageStoreItem* add(const std::string& name, const muse::ByteArray&);
86 : void clearUnused();
87 :
88 : typedef std::vector<ImageStoreItem*> ItemList;
89 : typedef ItemList::iterator iterator;
90 : typedef ItemList::const_iterator const_iterator;
91 :
92 4 : iterator begin() { return m_items.begin(); }
93 : const_iterator begin() const { return m_items.begin(); }
94 4 : iterator end() { return m_items.end(); }
95 : const_iterator end() const { return m_items.end(); }
96 :
97 : private:
98 :
99 : ItemList m_items;
100 : };
101 :
102 : extern ImageStore imageStore; // this is the global imageStore
103 : } // namespace mu::engraving
|