Skip to content

Commit b737cf4

Browse files
committed
feat: Add InputStreamBase, base class of InputTextStream
Paved the way to share its source code with InputBinaryStream.
1 parent 1b05ac7 commit b737cf4

File tree

4 files changed

+120
-66
lines changed

4 files changed

+120
-66
lines changed

include/itkInputStreamBase.h

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
virtual void
75+
SetFileName(const std::string & fileName) = 0;
76+
77+
protected:
78+
void
79+
SetFile(const std::string & fileName, const std::ios_base::openmode openMode)
80+
{
81+
if (m_DeleteIStream && m_IStream != nullptr)
82+
{
83+
delete m_IStream;
84+
}
85+
m_IStream = new std::ifstream(fileName, openMode);
86+
m_DeleteIStream = true;
87+
}
88+
89+
InputStreamBase() = default;
90+
virtual ~InputStreamBase()
91+
{
92+
if (m_DeleteIStream && m_IStream != nullptr)
93+
{
94+
delete m_IStream;
95+
}
96+
}
97+
98+
private:
99+
std::istream * m_IStream{ nullptr };
100+
bool m_DeleteIStream{ false };
101+
102+
WasmStringStream::Pointer m_WasmStringStream;
103+
};
104+
105+
106+
WebAssemblyInterface_EXPORT bool
107+
lexical_cast(const std::string & input, InputStreamBase & inputStream);
108+
109+
} // end namespace wasm
110+
} // end namespace itk
111+
112+
#endif

include/itkInputTextStream.h

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,10 @@
1818
#ifndef itkInputTextStream_h
1919
#define itkInputTextStream_h
2020

21-
#include "itkPipeline.h"
22-
#include "itkWasmStringStream.h"
21+
#include "itkInputStreamBase.h"
2322

23+
#include <ios>
2424
#include <string>
25-
#include <sstream>
26-
#include <fstream>
27-
28-
#include "WebAssemblyInterfaceExport.h"
2925

3026
namespace itk
3127
{
@@ -36,72 +32,18 @@ namespace wasm
3632
*\class InputTextStream
3733
* \brief Input text std::istream for an itk::wasm::Pipeline
3834
*
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-
*
4335
* \ingroup WebAssemblyInterface
4436
*/
45-
class WebAssemblyInterface_EXPORT InputTextStream
37+
class InputTextStream: public InputStreamBase
4638
{
4739
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-
6040
void
61-
SetJSON(const std::string & json)
41+
SetFileName(const std::string & fileName) override
6242
{
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());
43+
InputStreamBase::SetFile(fileName, std::ios_base::openmode{});
7244
}
73-
74-
void
75-
SetFileName(const std::string & fileName)
76-
{
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-
}
92-
}
93-
94-
private:
95-
std::istream * m_IStream{ nullptr };
96-
bool m_DeleteIStream{ false };
97-
98-
WasmStringStream::Pointer m_WasmStringStream;
9945
};
10046

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

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)