Add initial bits for Qt6 support
[carla.git] / source / modules / water / streams / MemoryOutputStream.h
blob2687d1f923572f1831f42d7c195640373fc915fb
1 /*
2 ==============================================================================
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
23 ==============================================================================
26 #ifndef WATER_MEMORYOUTPUTSTREAM_H_INCLUDED
27 #define WATER_MEMORYOUTPUTSTREAM_H_INCLUDED
29 #include "OutputStream.h"
30 #include "../memory/MemoryBlock.h"
32 namespace water {
34 //==============================================================================
35 /**
36 Writes data to an internal memory buffer, which grows as required.
38 The data that was written into the stream can then be accessed later as
39 a contiguous block of memory.
41 class MemoryOutputStream : public OutputStream
43 public:
44 //==============================================================================
45 /** Creates an empty memory stream, ready to be written into.
46 @param initialSize the intial amount of capacity to allocate for writing into
48 MemoryOutputStream (size_t initialSize = 256);
50 /** Creates a memory stream for writing into into a pre-existing MemoryBlock object.
52 Note that the destination block will always be larger than the amount of data
53 that has been written to the stream, because the MemoryOutputStream keeps some
54 spare capactity at its end. To trim the block's size down to fit the actual
55 data, call flush(), or delete the MemoryOutputStream.
57 @param memoryBlockToWriteTo the block into which new data will be written.
58 @param appendToExistingBlockContent if this is true, the contents of the block will be
59 kept, and new data will be appended to it. If false,
60 the block will be cleared before use
62 MemoryOutputStream (MemoryBlock& memoryBlockToWriteTo,
63 bool appendToExistingBlockContent);
65 /** Destructor.
66 This will free any data that was written to it.
68 ~MemoryOutputStream();
70 //==============================================================================
71 /** Returns a pointer to the data that has been written to the stream.
72 @see getDataSize
74 const void* getData() const noexcept;
76 /** Returns a pointer to the data that has been written to the stream and releases the buffer pointer.
77 @see getDataSize
79 void* getDataAndRelease() noexcept;
81 /** Returns the number of bytes of data that have been written to the stream.
82 @see getData
84 size_t getDataSize() const noexcept { return size; }
86 /** Resets the stream, clearing any data that has been written to it so far. */
87 void reset() noexcept;
89 /** Increases the internal storage capacity to be able to contain at least the specified
90 amount of data without needing to be resized.
92 void preallocate (size_t bytesToPreallocate);
94 /** Appends the utf-8 bytes for a unicode character */
95 bool appendUTF8Char (water_uchar character);
97 /** Returns a String created from the (UTF8) data that has been written to the stream. */
98 String toUTF8() const;
100 /** Attempts to detect the encoding of the data and convert it to a string.
101 @see String::createStringFromData
103 String toString() const;
105 /** Returns a copy of the stream's data as a memory block. */
106 MemoryBlock getMemoryBlock() const;
108 //==============================================================================
109 /** If the stream is writing to a user-supplied MemoryBlock, this will trim any excess
110 capacity off the block, so that its length matches the amount of actual data that
111 has been written so far.
113 void flush() override;
115 bool write (const void*, size_t) override;
116 int64 getPosition() override { return (int64) position; }
117 bool setPosition (int64) override;
118 int64 writeFromInputStream (InputStream&, int64 maxNumBytesToWrite) override;
119 bool writeRepeatedByte (uint8 byte, size_t numTimesToRepeat) override;
121 private:
122 //==============================================================================
123 MemoryBlock internalBlock;
124 MemoryBlock& blockToUse;
125 size_t position, size;
126 bool usingInternalBlock;
128 void trimExternalBlockSize();
129 char* prepareToWrite (size_t);
131 CARLA_DECLARE_NON_COPYABLE (MemoryOutputStream)
134 /** Copies all the data that has been written to a MemoryOutputStream into another stream. */
135 OutputStream& operator<< (OutputStream& stream, const MemoryOutputStream& streamToRead);
139 #endif // WATER_MEMORYOUTPUTSTREAM_H_INCLUDED