Debian Codegraph @

modules/lsp-runtime-lib/include/lsp-plug.in/io/InMemoryStream.h

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 21 авг. 2019 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #ifndef LSP_PLUG_IN_IO_INMEMORYSTREAM_H_
23 #define LSP_PLUG_IN_IO_INMEMORYSTREAM_H_
24
25 #include <lsp-plug.in/runtime/version.h>
26 #include <lsp-plug.in/io/IInStream.h>
27 #include <lsp-plug.in/io/OutMemoryStream.h>
28
29 namespace lsp
30 {
31 namespace io
32 {
33 /**
34 * Input stream around the flat chunk of memory.
35 */
36 class InMemoryStream: public IInStream
37 {
38 protected:
39 uint8_t *pData;
40 size_t nOffset;
41 size_t nSize;
42 lsp_memdrop_t enDrop;
43
44 public:
45 explicit InMemoryStream();
46
47 /**
48 * Create memory input stream
49 * @param data associated buffer
50 * @param size size of buffer
51 */
52 explicit InMemoryStream(const void *data, size_t size);
53
54 /**
55 * Create memory input stream
56 * @param data associated buffer
57 * @param size size of buffer
58 * @param drop the method how to drop data on close()
59 */
60 explicit InMemoryStream(void *data, size_t size, lsp_memdrop_t drop);
61
62 virtual ~InMemoryStream() override;
63
64 InMemoryStream(const InMemoryStream &) = delete;
65 InMemoryStream(InMemoryStream &&) = delete;
66
67 InMemoryStream & operator = (const InMemoryStream &) = delete;
68 InMemoryStream & operator = (InMemoryStream &&) = delete;
69
70 public:
71 /**
72 * Wrap the memory buffer, drop previous buffer using specified mechanism
73 * @param data associated buffer
74 * @param size size of buffer
75 * @param drop the method how to drop data on close()
76 */
77 void wrap(void *data, size_t size, lsp_memdrop_t drop);
78
79 /**
80 * Wrap the memory buffer, drop previous buffer using specified mechanism
81 * @param data associated buffer
82 * @param size size of buffer
83 */
84 void wrap(const void *data, size_t size);
85
86 /**
87 * Get the memory contents
88 * @return memory contents
89 */
90 inline const uint8_t *data() const { return pData; }
91
92 /**
93 * Drop data using specified drop mechanism
94 * @return true if data has been dropped
95 */
96 bool drop(lsp_memdrop_t drop);
97
98 /**
99 * Drop data using default mechanism specified in constructor
100 * @return true if data has been dropped
101 */
102 inline bool drop() { return drop(enDrop); };
103
104 /**
105 * Obtain the size of memory buffer wrapped by the memory stream
106 * @return the size of memory buffer in bytes
107 */
108 inline size_t size() const { return nSize; }
109
110 void take(OutMemoryStream &src);
111 void take(OutMemoryStream *src);
112
113 public: // io::IInStream
114 virtual wssize_t avail() override;
115 virtual wssize_t position() override;
116 virtual ssize_t read(void *dst, size_t count) override;
117 virtual ssize_t read_byte() override;
118 virtual wssize_t seek(wsize_t position) override;
119 virtual wssize_t skip(wsize_t amount) override;
120 virtual status_t close() override;
121 };
122
123 } /* namespace io */
124 } /* namespace lsp */
125
126 #endif /* LSP_PLUG_IN_IO_INMEMORYSTREAM_H_ */