ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
itkWasmStringStream.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright NumFOCUS
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18#ifndef itkWasmStringStream_h
19#define itkWasmStringStream_h
20
21#include "itkWasmDataObject.h"
22#include <string_view>
23
24#include "WebAssemblyInterfaceExport.h"
25
26#include "glaze/glaze.hpp"
27
28namespace itk
29{
30
32{
33 std::string data;
34 size_t size;
35};
36
54class WebAssemblyInterface_EXPORT WasmStringStream : public WasmDataObject
55{
56public:
57 ITK_DISALLOW_COPY_AND_MOVE(WasmStringStream);
58
64
65 itkNewMacro(Self);
67 itkOverrideGetNameOfClassMacro(WasmStringStream);
68
69 void
70 SetString(const std::string & string)
71 {
72 this->m_StringStream.str(string);
73 this->UpdateJSON();
74 }
75
76 const std::string &
78 {
79 this->m_String = m_StringStream.str();
80 return this->m_String;
81 }
82
83 std::stringstream &
85 {
86 return this->m_StringStream;
87 }
88
89 void
90 SetJSON(const char * jsonChar) override
91 {
92 std::string json(jsonChar);
93 std::string deserialized;
94 auto deserializedAttempt = glz::read_json<StringStreamJSON>(json);
95 if (!deserializedAttempt)
96 {
97 const std::string descriptiveError = glz::format_error(deserializedAttempt, json);
98 throw std::runtime_error("Failed to deserialize StringStreamJSON: " + descriptiveError);
99 }
100 auto stringStream = deserializedAttempt.value();
101 const std::string dataString = stringStream.data;
102 const char * dataPtr = reinterpret_cast<char *>(std::strtoull(dataString.substr(35).c_str(), nullptr, 10));
103 size_t size = stringStream.size;
104 const std::string_view string(dataPtr, size);
105 m_StringStream.str(std::string{ string });
106
107 Superclass::SetJSON(jsonChar);
108 }
109
110protected:
111 WasmStringStream() = default;
112 ~WasmStringStream() override = default;
113
114 void
116 {
117 std::ostringstream jsonStream;
118 jsonStream << "{ \"data\": \"data:application/vnd.itk.address,0:";
119 jsonStream << reinterpret_cast<size_t>(m_StringStream.str().data());
120 jsonStream << "\", \"size\": ";
121 jsonStream << m_StringStream.str().size() + 1;
122 jsonStream << "}";
123 this->m_JSON = jsonStream.str();
124 }
125
126 void
127 PrintSelf(std::ostream & os, Indent indent) const override;
128
129private:
130 std::stringstream m_StringStream;
131 std::string m_String;
132};
133
134} // namespace itk
135
136#endif
Control indentation during Print() invocation.
Definition itkIndent.h:53
Implements transparent reference counting.
virtual void SetJSON(const char *_arg)
WasmDataObject()=default
std::stringstream m_StringStream
void SetString(const std::string &string)
std::stringstream & GetStringStream()
const std::string & GetString()
void PrintSelf(std::ostream &os, Indent indent) const override
SmartPointer< const Self > ConstPointer
SmartPointer< Self > Pointer
WasmStringStream()=default
~WasmStringStream() override=default
void SetJSON(const char *jsonChar) override
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes....