Audacity 3.2.0
libraries/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 "MemoryX.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};
Vector operator*(const Vector &left, const Vector &right)
Definition: Matrix.cpp:153
bool operator==(const Tags &lhs, const Tags &rhs)
Definition: Tags.cpp:332
bool operator!=(const Tags &lhs, const Tags &rhs)
Definition: Tags.h:133
Append([](My &table) -> Registry::BaseItemPtr { if(WaveTrackSubViews::slots() > 1) return 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=WaveTrackView::Get(track);menu.Check(id, view.GetMultiView());});else return nullptr;})
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::vector< uint8_t > StreamData
MemoryStream(MemoryStream &&)=default
auto end(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:159
auto begin(const Ptr< Type, BaseDeleter > &p)
Enables range-for.
Definition: PackedArray.h:150
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: MemoryX.h:246