Skip to content

Commit 0c4f034

Browse files
committed
feat: Add InputStreamBase, base class of InputTextStream
1 parent 3818ff2 commit 0c4f034

File tree

4 files changed

+115
-59
lines changed

4 files changed

+115
-59
lines changed

include/itkInputStreamBase.h

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
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 itkInputStreamBase_h
19+
#define itkInputStreamBase_h
20+
21+
#include "itkPipeline.h"
22+
#include "itkWasmStringStream.h"
23+
24+
#include <string>
25+
#include <sstream>
26+
#include <fstream>
27+
28+
#include "WebAssemblyInterfaceExport.h"
29+
30+
namespace itk
31+
{
32+
namespace wasm
33+
{
34+
35+
/**
36+
*\class InputStreamBase
37+
* \brief Base class of input stream for an itk::wasm::Pipeline
38+
*
39+
* This stream is read from the filesystem or memory when ITK_WASM_PARSE_ARGS is called.
40+
*
41+
* Call `Get()` to get the std::istream & to use an input to a pipeline.
42+
*
43+
* \ingroup WebAssemblyInterface
44+
*/
45+
class WebAssemblyInterface_EXPORT InputStreamBase
46+
{
47+
public:
48+
std::istream &
49+
Get()
50+
{
51+
return *m_IStream;
52+
}
53+
54+
std::istream *
55+
GetPointer()
56+
{
57+
return m_IStream;
58+
}
59+
60+
void
61+
SetJSON(const std::string & json)
62+
{
63+
if (m_DeleteIStream && m_IStream != nullptr)
64+
{
65+
delete m_IStream;
66+
}
67+
m_DeleteIStream = false;
68+
m_WasmStringStream = WasmStringStream::New();
69+
m_WasmStringStream->SetJSON(json.c_str());
70+
71+
m_IStream = &(m_WasmStringStream->GetStringStream());
72+
}
73+
74+
protected:
75+
void
76+
SetFile(const std::string & fileName, const std::ios_base::openmode openMode)
77+
{
78+
if (m_DeleteIStream && m_IStream != nullptr)
79+
{
80+
delete m_IStream;
81+
}
82+
m_IStream = new std::ifstream(fileName, openMode);
83+
m_DeleteIStream = true;
84+
}
85+
86+
InputStreamBase() = default;
87+
virtual ~InputStreamBase() = 0
88+
{
89+
if (m_DeleteIStream && m_IStream != nullptr)
90+
{
91+
delete m_IStream;
92+
}
93+
}
94+
95+
private:
96+
std::istream * m_IStream{ nullptr };
97+
bool m_DeleteIStream{ false };
98+
99+
WasmStringStream::Pointer m_WasmStringStream;
100+
};
101+
102+
103+
WebAssemblyInterface_EXPORT bool
104+
lexical_cast(const std::string & input, InputStreamBase & inputStream);
105+
106+
} // end namespace wasm
107+
} // end namespace itk
108+
109+
#endif

include/itkInputTextStream.h

Lines changed: 3 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "itkPipeline.h"
2222
#include "itkWasmStringStream.h"
23+
#include "itkInputStreamBase.h"
2324

2425
#include <string>
2526
#include <sstream>
@@ -36,72 +37,18 @@ namespace wasm
3637
*\class InputTextStream
3738
* \brief Input text std::istream for an itk::wasm::Pipeline
3839
*
39-
* This stream is read from the filesystem or memory when ITK_WASM_PARSE_ARGS is called.
40-
*
41-
* Call `Get()` to get the std::istream & to use an input to a pipeline.
42-
*
4340
* \ingroup WebAssemblyInterface
4441
*/
45-
class WebAssemblyInterface_EXPORT InputTextStream
42+
class WebAssemblyInterface_EXPORT InputTextStream: public InputStreamBase
4643
{
4744
public:
48-
std::istream &
49-
Get()
50-
{
51-
return *m_IStream;
52-
}
53-
54-
std::istream *
55-
GetPointer()
56-
{
57-
return m_IStream;
58-
}
59-
60-
void
61-
SetJSON(const std::string & json)
62-
{
63-
if (m_DeleteIStream && m_IStream != nullptr)
64-
{
65-
delete m_IStream;
66-
}
67-
m_DeleteIStream = false;
68-
m_WasmStringStream = WasmStringStream::New();
69-
m_WasmStringStream->SetJSON(json.c_str());
70-
71-
m_IStream = &(m_WasmStringStream->GetStringStream());
72-
}
73-
7445
void
7546
SetFileName(const std::string & fileName)
7647
{
77-
if (m_DeleteIStream && m_IStream != nullptr)
78-
{
79-
delete m_IStream;
80-
}
81-
m_IStream = new std::ifstream(fileName, std::ifstream::in);
82-
m_DeleteIStream = true;
83-
}
84-
85-
InputTextStream() = default;
86-
~InputTextStream()
87-
{
88-
if (m_DeleteIStream && m_IStream != nullptr)
89-
{
90-
delete m_IStream;
91-
}
48+
InputStreamBase::SetFile(fileName, std::ios_base::openmode{});
9249
}
93-
94-
private:
95-
std::istream * m_IStream{ nullptr };
96-
bool m_DeleteIStream{ false };
97-
98-
WasmStringStream::Pointer m_WasmStringStream;
9950
};
10051

101-
102-
WebAssemblyInterface_EXPORT bool
103-
lexical_cast(const std::string & input, InputTextStream & inputStream);
104-
10552
} // end namespace wasm
10653
} // end namespace itk
10754

src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ set(WebAssemblyInterface_SRCS
1414
itkWasmTransformIOFactory.cxx
1515
itkWasmTransformIO.cxx
1616
itkWasmStringStream.cxx
17-
itkInputTextStream.cxx
17+
itkInputStreamBase.cxx
1818
itkOutputTextStream.cxx
1919
itkInputBinaryStream.cxx
2020
itkOutputBinaryStream.cxx

src/itkInputTextStream.cxx renamed to src/itkInputStreamBase.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*
1717
*=========================================================================*/
18-
#include "itkInputTextStream.h"
18+
#include "itkInputStreamBase.h"
1919

2020
#include <string>
2121
#ifndef ITK_WASM_NO_MEMORY_IO
@@ -28,7 +28,7 @@ namespace wasm
2828
{
2929

3030
bool
31-
lexical_cast(const std::string & input, InputTextStream & inputStream)
31+
lexical_cast(const std::string & input, InputStreamBase & inputStream)
3232
{
3333
if (input.empty())
3434
{

0 commit comments

Comments
 (0)