Skip to content

Commit cd81bb3

Browse files
authored
Merge pull request #1434 from N-Dekker/OutputStreamBase
Add OutputStreamBase: common base class of OutputBinaryStream and OutputTextStream
2 parents 3ea37dd + 5cd7fe3 commit cd81bb3

File tree

6 files changed

+127
-223
lines changed

6 files changed

+127
-223
lines changed

include/itkOutputBinaryStream.h

Lines changed: 6 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,10 @@
1818
#ifndef itkOutputBinaryStream_h
1919
#define itkOutputBinaryStream_h
2020

21-
#include "itkPipeline.h"
22-
#include "itkWasmStringStream.h"
21+
#include "itkOutputStreamBase.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
{
@@ -38,70 +30,20 @@ namespace wasm
3830

3931
/**
4032
*\class OutputBinaryStream
41-
* \brief Output text std::ostream for an itk::wasm::Pipeline
42-
*
43-
* This stream is written to the filesystem or memory when the object goes out of scope.
44-
*
45-
* Call `Get()` to get the std::ostream & to use an output for a pipeline.
33+
* \brief Output binary std::ostream for an itk::wasm::Pipeline
4634
*
4735
* \ingroup WebAssemblyInterface
4836
*/
49-
class WebAssemblyInterface_EXPORT OutputBinaryStream
37+
class OutputBinaryStream: public OutputStreamBase
5038
{
5139
public:
52-
std::ostream &
53-
Get()
54-
{
55-
return *m_OStream;
56-
}
57-
5840
void
59-
SetFileName(const std::string & fileName)
41+
SetFileName(const std::string & fileName) override
6042
{
61-
if (m_DeleteOStream && m_OStream != nullptr)
62-
{
63-
delete m_OStream;
64-
}
65-
m_OStream = new std::ofstream(fileName, std::ofstream::out | std::ofstream::binary);
66-
m_DeleteOStream = true;
43+
OutputStreamBase::SetFile(fileName, std::ios_base::binary);
6744
}
68-
69-
OutputBinaryStream() = default;
70-
~OutputBinaryStream();
71-
72-
/** Output index. */
73-
void
74-
SetIdentifier(const std::string & identifier)
75-
{
76-
if (m_DeleteOStream && m_OStream != nullptr)
77-
{
78-
delete m_OStream;
79-
}
80-
m_DeleteOStream = false;
81-
m_WasmStringStream = WasmStringStream::New();
82-
83-
m_OStream = &(m_WasmStringStream->GetStringStream());
84-
this->m_Identifier = identifier;
85-
}
86-
const std::string &
87-
GetIdentifier() const
88-
{
89-
return this->m_Identifier;
90-
}
91-
92-
private:
93-
std::ostream * m_OStream{ nullptr };
94-
bool m_DeleteOStream{ false };
95-
96-
std::string m_Identifier;
97-
98-
WasmStringStream::Pointer m_WasmStringStream;
9945
};
10046

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

include/itkOutputStreamBase.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 itkOutputStreamBase_h
19+
#define itkOutputStreamBase_h
20+
21+
#include "itkPipeline.h"
22+
#include "itkWasmStringStream.h"
23+
24+
#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"
33+
34+
namespace itk
35+
{
36+
namespace wasm
37+
{
38+
39+
/**
40+
*\class OutputStreamBase
41+
* \brief Base class of output stream for an itk::wasm::Pipeline
42+
*
43+
* This stream is written to the filesystem or memory when the object goes out of scope.
44+
*
45+
* Call `Get()` to get the std::ostream & to use an output for a pipeline.
46+
*
47+
* \ingroup WebAssemblyInterface
48+
*/
49+
class WebAssemblyInterface_EXPORT OutputStreamBase
50+
{
51+
public:
52+
std::ostream &
53+
Get()
54+
{
55+
return *m_OStream;
56+
}
57+
58+
virtual void
59+
SetFileName(const std::string & fileName) = 0;
60+
61+
/** Output index. */
62+
void
63+
SetIdentifier(const std::string & identifier)
64+
{
65+
if (m_DeleteOStream && m_OStream != nullptr)
66+
{
67+
delete m_OStream;
68+
}
69+
m_DeleteOStream = false;
70+
m_WasmStringStream = WasmStringStream::New();
71+
72+
m_OStream = &(m_WasmStringStream->GetStringStream());
73+
this->m_Identifier = identifier;
74+
}
75+
const std::string &
76+
GetIdentifier() const
77+
{
78+
return this->m_Identifier;
79+
}
80+
81+
protected:
82+
OutputStreamBase() = default;
83+
virtual ~OutputStreamBase();
84+
85+
void
86+
SetFile(const std::string & fileName, const std::ios_base::openmode openMode)
87+
{
88+
if (m_DeleteOStream && m_OStream != nullptr)
89+
{
90+
delete m_OStream;
91+
}
92+
m_OStream = new std::ofstream(fileName, openMode);
93+
m_DeleteOStream = true;
94+
}
95+
96+
private:
97+
std::ostream * m_OStream{ nullptr };
98+
bool m_DeleteOStream{ false };
99+
100+
std::string m_Identifier;
101+
102+
WasmStringStream::Pointer m_WasmStringStream;
103+
};
104+
105+
106+
WebAssemblyInterface_EXPORT bool
107+
lexical_cast(const std::string & output, OutputStreamBase & outputStream);
108+
109+
} // end namespace wasm
110+
} // end namespace itk
111+
112+
#endif

include/itkOutputTextStream.h

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

21-
#include "itkPipeline.h"
22-
#include "itkWasmStringStream.h"
21+
#include "itkOutputStreamBase.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,68 +32,18 @@ namespace wasm
4032
*\class OutputTextStream
4133
* \brief Output text std::ostream for an itk::wasm::Pipeline
4234
*
43-
* This stream is written to the filesystem or memory when the object goes out of scope.
44-
*
45-
* Call `Get()` to get the std::ostream & to use an output for a pipeline.
46-
*
4735
* \ingroup WebAssemblyInterface
4836
*/
49-
class WebAssemblyInterface_EXPORT OutputTextStream
37+
class OutputTextStream: public OutputStreamBase
5038
{
5139
public:
52-
std::ostream &
53-
Get()
54-
{
55-
return *m_OStream;
56-
}
57-
5840
void
59-
SetFileName(const std::string & fileName)
41+
SetFileName(const std::string & fileName) override
6042
{
61-
if (m_DeleteOStream && m_OStream != nullptr)
62-
{
63-
delete m_OStream;
64-
}
65-
m_OStream = new std::ofstream(fileName, std::ofstream::out);
66-
m_DeleteOStream = true;
43+
OutputStreamBase::SetFile(fileName, std::ios_base::openmode{});
6744
}
68-
69-
OutputTextStream() = default;
70-
~OutputTextStream();
71-
72-
/** Output index. */
73-
void
74-
SetIdentifier(const std::string & identifier)
75-
{
76-
if (m_DeleteOStream && m_OStream != nullptr)
77-
{
78-
delete m_OStream;
79-
}
80-
m_DeleteOStream = false;
81-
m_WasmStringStream = WasmStringStream::New();
82-
83-
m_OStream = &(m_WasmStringStream->GetStringStream());
84-
this->m_Identifier = identifier;
85-
}
86-
const std::string &
87-
GetIdentifier() const
88-
{
89-
return this->m_Identifier;
90-
}
91-
92-
private:
93-
std::ostream * m_OStream{ nullptr };
94-
bool m_DeleteOStream{ false };
95-
96-
std::string m_Identifier;
97-
98-
WasmStringStream::Pointer m_WasmStringStream;
9945
};
10046

101-
102-
WebAssemblyInterface_EXPORT bool
103-
lexical_cast(const std::string & output, OutputTextStream & outputStream);
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
@@ -15,8 +15,7 @@ set(WebAssemblyInterface_SRCS
1515
itkWasmTransformIO.cxx
1616
itkWasmStringStream.cxx
1717
itkInputStreamBase.cxx
18-
itkOutputTextStream.cxx
19-
itkOutputBinaryStream.cxx
18+
itkOutputStreamBase.cxx
2019
itkIOComponentEnumFromWasmComponentType.cxx
2120
itkIOPixelEnumFromWasmPixelType.cxx
2221
itkWasmComponentTypeFromIOComponentEnum.cxx

0 commit comments

Comments
 (0)