Audacity 3.2.0
lib-utility/memorystream.h
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 MemoryStream.h
6
7 Dmitry Vedenko
8
9**********************************************************************/
10
11#pragma once
12
13#include <array>
14#include <cstdint>
15#include <list>
16#include <vector>
17
18#include "IteratorX.h"
19
30class UTILITY_API MemoryStream final
31{
32public:
33 using StreamData = std::vector<uint8_t>;
34 using StreamChunk = std::pair<const void*, size_t>;
35
36private:
37 static constexpr size_t ChunkSize =
38 1024 * 1024 - // 1Mb
39 2 * sizeof(void*) - // account for the list node pointers
40 sizeof(size_t); // account for the bytes used member
41
42 struct Chunk final
43 {
44 std::array<uint8_t, ChunkSize> Data;
45 size_t BytesUsed { 0 };
46
47 // Returns data size left to append
48 size_t Append(StreamChunk& dataView);
49 };
50
51 using ChunksList = std::list<Chunk>;
52
53public:
54
55 MemoryStream() = default;
57
58 void Clear();
59
60 void AppendByte(char data);
61 void AppendData(const void* data, const size_t length);
62
63 // This function possibly has O(size) complexity as it may
64 // require copying bytes to a linear chunk
65 const void* GetData() const;
66 const size_t GetSize() const noexcept;
67
68 bool IsEmpty() const noexcept;
69
70 struct UTILITY_API Iterator :
71 ValueIterator<const StreamChunk, std::forward_iterator_tag>
72 {
73 Iterator(const Iterator&) = default;
74
75 Iterator& operator++();
76
77 Iterator operator++(int);
78
79 StreamChunk operator*() const;
80 StreamChunk operator->() const;
81
82 bool operator==(const Iterator& rhs) const noexcept;
83 bool operator!=(const Iterator& rhs) const noexcept;
84 private:
85 Iterator(const MemoryStream* stream, bool isBegin);
86
87 const MemoryStream* mStream { nullptr };
88 ChunksList::const_iterator mListIterator;
89 bool mShowLinearPart { false };
90
91 friend class MemoryStream;
92 };
93
94 Iterator begin() const;
95 Iterator end() const;
96
97private:
98 // This structures are lazily updated by get data
101
102 size_t mDataSize { 0 };
103};
auto operator*(PffftAlignedCount x, Integral y) -> std::enable_if_t< std::is_unsigned_v< Integral > &&sizeof(Integral)<=sizeof(size_t), PffftAlignedCount >
Append(Adapt< My >([](My &table) { return(WaveChannelSubViews::numFactories() > 1) ? std::make_unique< Entry >("MultiView", Entry::CheckItem, OnMultiViewID, XXO("&Multi-view"), POPUP_MENU_FN(OnMultiView), table, [](PopupMenuHandler &handler, wxMenu &menu, int id){ auto &table=static_cast< WaveTrackMenuTable & >(handler);auto &track=table.FindWaveTrack();const auto &view=WaveChannelView::GetFirst(track);menu.Check(id, view.GetMultiView());}) :nullptr;}))
bool operator==(const WaveTrackLocation &a, const WaveTrackLocation &b)
bool operator!=(const WaveTrackLocation &a, const WaveTrackLocation &b)
A low overhead memory stream with O(1) append, low heap fragmentation and a linear memory view.
MemoryStream()=default
std::pair< const void *, size_t > StreamChunk
std::list< Chunk > ChunksList
std::vector< uint8_t > StreamData
MemoryStream(MemoryStream &&)=default
const char * end(const char *str) noexcept
Definition: StringUtils.h:106
const char * begin(const char *str) noexcept
Definition: StringUtils.h:101
STL namespace.
std::array< uint8_t, ChunkSize > Data
Iterator(const Iterator &)=default
ChunksList::const_iterator mListIterator
A convenience for defining iterators that return rvalue types, so that they cooperate correctly with ...
Definition: IteratorX.h:25