VTK  9.4.20241118
vtkMemoryResourceStream.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
3#ifndef vtkMemoryResourceStream_h
4#define vtkMemoryResourceStream_h
5
6#include "vtkBuffer.h" // vtkBuffer
7#include "vtkIOCoreModule.h" // For export macro
8#include "vtkResourceStream.h"
9
10#include <memory> // for std::unique_ptr
11#include <string> // for std::basic_string
12#include <utility> // for std::forward
13#include <vector> // for std::vector
14
15VTK_ABI_NAMESPACE_BEGIN
16
24class VTKIOCORE_EXPORT vtkMemoryResourceStream : public vtkResourceStream
25{
29 struct BasicHolder
30 {
31 BasicHolder() = default;
32 virtual ~BasicHolder() = default;
33 BasicHolder(const BasicHolder&) = delete;
34 BasicHolder& operator=(const BasicHolder&) = delete;
35 BasicHolder(BasicHolder&&) noexcept = delete;
36 BasicHolder& operator=(BasicHolder&&) noexcept = delete;
37 };
38
45 template <typename T>
46 struct DataHolder : public BasicHolder
47 {
48 template <typename... Args>
49 DataHolder(Args&&... args)
50 : Data{ std::forward<Args>(args)... }
51 {
52 }
53
54 ~DataHolder() override = default;
55 DataHolder(const DataHolder&) = delete;
56 DataHolder& operator=(const DataHolder&) = delete;
57 DataHolder(DataHolder&&) noexcept = delete;
58 DataHolder& operator=(DataHolder&&) noexcept = delete;
59
60 T Data;
61 };
62
63 // Small utility function to easily create a DataHolder with type deduction
64 template <typename T>
65 static std::unique_ptr<DataHolder<T>> MakeHolder(T&& value)
66 {
67 return std::unique_ptr<DataHolder<T>>{ new DataHolder<T>{ std::forward<T>(value) } };
68 }
69
70public:
73 void PrintSelf(ostream& os, vtkIndent indent) override;
74
76
79 std::size_t Read(void* buffer, std::size_t bytes) override;
80 bool EndOfStream() override;
81 vtkTypeInt64 Seek(vtkTypeInt64 pos, SeekDirection dir) override;
82 vtkTypeInt64 Tell() override;
84
104 void SetBuffer(const void* buffer, std::size_t size, bool copy = false);
105
122 template <typename T>
124 {
125 if (buffer)
126 {
127 this->SetBuffer(buffer->GetBuffer(), buffer->GetSize() * sizeof(T));
128 this->Holder = MakeHolder(std::move(buffer));
129 }
130 else
131 {
132 this->SetBuffer(nullptr, 0);
133 }
134 }
135
152 template <typename T, typename Allocator>
153 void SetBuffer(std::vector<T, Allocator> vec)
154 {
155 this->SetBuffer(vec.data(), vec.size() * sizeof(T));
156 this->Holder = MakeHolder(std::move(vec));
157 }
158
166 template <typename CharT, typename Traits, typename Allocator>
167 void SetBuffer(std::basic_string<CharT, Traits, Allocator> str)
168 {
169 // unlike std::vector (and other containers), std::basic_string does not guarantee
170 // iterators validity after a move (mainly for SSO).
171 // So we have to move it prior to getting the pointer.
172 auto holder = MakeHolder(std::move(str));
173 this->SetBuffer(holder->Data.data(), holder->Data.size() * sizeof(CharT));
174 this->Holder = std::move(holder);
175 }
176
185 bool OwnsBuffer() const { return this->Holder != nullptr; }
186
187protected:
189 ~vtkMemoryResourceStream() override = default;
192
193private:
194 const unsigned char* Buffer = nullptr; // for pointer arithmetic
195 std::size_t Size = 0;
196 vtkTypeInt64 Pos = 0;
197 bool Eos = false;
198 std::unique_ptr<BasicHolder> Holder;
199};
200
201VTK_ABI_NAMESPACE_END
202
203#endif
internal storage class used by vtkSOADataArrayTemplate, vtkAOSDataArrayTemplate, and others.
Definition vtkBuffer.h:27
a simple class to control print indentation
Definition vtkIndent.h:108
vtkResourceStream implementation for memory input.
void SetBuffer(const void *buffer, std::size_t size, bool copy=false)
Set buffer to stream.
std::size_t Read(void *buffer, std::size_t bytes) override
Override vtkResourceStream functions.
vtkMemoryResourceStream(const vtkMemoryResourceStream &)=delete
vtkTypeInt64 Seek(vtkTypeInt64 pos, SeekDirection dir) override
Override vtkResourceStream functions.
vtkTypeInt64 Tell() override
Override vtkResourceStream functions.
void SetBuffer(std::vector< T, Allocator > vec)
Set buffer to stream.
void SetBuffer(vtkSmartPointer< vtkBuffer< T > > buffer)
Set buffer to stream.
static vtkMemoryResourceStream * New()
~vtkMemoryResourceStream() override=default
bool OwnsBuffer() const
Check if this has a internally managed buffer.
vtkMemoryResourceStream & operator=(const vtkMemoryResourceStream &)=delete
void SetBuffer(std::basic_string< CharT, Traits, Allocator > str)
Set buffer to stream.
void PrintSelf(ostream &os, vtkIndent indent) override
Methods invoked by print to print information about the object including superclasses.
bool EndOfStream() override
Override vtkResourceStream functions.
Abstract class used for custom streams.
Hold a reference to a vtkObjectBase instance.