Skip to content

Commit d09f5e7

Browse files
authored
Merge pull request #1426 from N-Dekker/InputStreamBase
Add InputStreamBase: common base class of InputBinaryStream and InputTextStream
2 parents a473cb9 + 6aac66e commit d09f5e7

File tree

6 files changed

+125
-187
lines changed

6 files changed

+125
-187
lines changed

include/itkInputBinaryStream.h

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

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

23+
#include <ios>
2424
#include <string>
25-
#ifndef ITK_WASM_NO_MEMORY_IO
26-
# include <sstream>
27-
#endif
28-
#ifndef ITK_WASM_NO_FILESYSTEM_IO
29-
# include <fstream>
30-
#endif
31-
32-
#include "WebAssemblyInterfaceExport.h"
3325

3426
namespace itk
3527
{
@@ -40,66 +32,18 @@ namespace wasm
4032
*\class InputBinaryStream
4133
* \brief Input binary std::istream for an itk::wasm::Pipeline
4234
*
43-
* This stream is read from the filesystem or memory when ITK_WASM_PARSE_ARGS is called.
44-
*
45-
* Call `Get()` to get the std::istream & to use an input to a pipeline.
46-
*
4735
* \ingroup WebAssemblyInterface
4836
*/
49-
class WebAssemblyInterface_EXPORT InputBinaryStream
37+
class InputBinaryStream : public InputStreamBase
5038
{
5139
public:
52-
std::istream &
53-
Get()
54-
{
55-
return *m_IStream;
56-
}
57-
58-
void
59-
SetJSON(const std::string & json)
60-
{
61-
if (m_DeleteIStream && m_IStream != nullptr)
62-
{
63-
delete m_IStream;
64-
}
65-
m_DeleteIStream = false;
66-
m_WasmStringStream = WasmStringStream::New();
67-
m_WasmStringStream->SetJSON(json.c_str());
68-
69-
m_IStream = &(m_WasmStringStream->GetStringStream());
70-
}
71-
7240
void
73-
SetFileName(const std::string & fileName)
74-
{
75-
if (m_DeleteIStream && m_IStream != nullptr)
76-
{
77-
delete m_IStream;
78-
}
79-
m_IStream = new std::ifstream(fileName, std::ifstream::in | std::ifstream::binary);
80-
m_DeleteIStream = true;
81-
}
82-
83-
InputBinaryStream() = default;
84-
~InputBinaryStream()
41+
SetFileName(const std::string & fileName) override
8542
{
86-
if (m_DeleteIStream && m_IStream != nullptr)
87-
{
88-
delete m_IStream;
89-
}
43+
InputStreamBase::SetFile(fileName, std::ios_base::binary);
9044
}
91-
92-
private:
93-
std::istream * m_IStream{ nullptr };
94-
bool m_DeleteIStream{ false };
95-
96-
WasmStringStream::Pointer m_WasmStringStream;
9745
};
9846

99-
100-
WebAssemblyInterface_EXPORT bool
101-
lexical_cast(const std::string & input, InputBinaryStream & inputStream);
102-
10347
} // end namespace wasm
10448
} // end namespace itk
10549

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ set(WebAssemblyInterface_SRCS
1414
itkWasmTransformIOFactory.cxx
1515
itkWasmTransformIO.cxx
1616
itkWasmStringStream.cxx
17-
itkInputTextStream.cxx
17+
itkInputStreamBase.cxx
1818
itkOutputTextStream.cxx
19-
itkInputBinaryStream.cxx
2019
itkOutputBinaryStream.cxx
2120
itkIOComponentEnumFromWasmComponentType.cxx
2221
itkIOPixelEnumFromWasmPixelType.cxx

src/itkInputBinaryStream.cxx

Lines changed: 0 additions & 59 deletions
This file was deleted.

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)