Skip to content

Commit 323d263

Browse files
authored
Merge pull request #1432 from N-Dekker/InputStreamBase-unique_ptr-istream
style: Use `std::unique_ptr<std::istream>` in InputStreamBase
2 parents 7da4067 + 965f201 commit 323d263

File tree

2 files changed

+37
-39
lines changed

2 files changed

+37
-39
lines changed

include/itkInputStreamBase.h

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

21-
#include "itkPipeline.h"
22-
#include "itkWasmStringStream.h"
23-
21+
#include <ios>
22+
#include <iosfwd> // For istream.
23+
#include <memory> // For unique_ptr.
2424
#include <string>
25-
#include <sstream>
26-
#include <fstream>
2725

2826
#include "WebAssemblyInterfaceExport.h"
2927

@@ -54,52 +52,30 @@ class WebAssemblyInterface_EXPORT InputStreamBase
5452
std::istream *
5553
GetPointer()
5654
{
57-
return m_IStream;
55+
return m_IStream.get();
5856
}
5957

6058
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-
}
59+
SetJSON(const std::string & json);
7360

7461
virtual void
7562
SetFileName(const std::string & fileName) = 0;
7663

7764
protected:
7865
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-
}
66+
SetFile(const std::string & fileName, const std::ios_base::openmode openMode);
8867

8968
InputStreamBase() = default;
90-
virtual ~InputStreamBase()
91-
{
92-
if (m_DeleteIStream && m_IStream != nullptr)
93-
{
94-
delete m_IStream;
95-
}
96-
}
9769

98-
private:
99-
std::istream * m_IStream{ nullptr };
100-
bool m_DeleteIStream{ false };
70+
// Move semantics for its derived classes:
71+
InputStreamBase(InputStreamBase &&) = default;
72+
InputStreamBase &
73+
operator=(InputStreamBase &&) = default;
10174

102-
WasmStringStream::Pointer m_WasmStringStream;
75+
virtual ~InputStreamBase();
76+
77+
private:
78+
std::unique_ptr<std::istream> m_IStream;
10379
};
10480

10581

src/itkInputStreamBase.cxx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,38 @@
1616
*
1717
*=========================================================================*/
1818
#include "itkInputStreamBase.h"
19+
#include "itkPipeline.h"
20+
#include "itkWasmStringStream.h"
1921

20-
#include <string>
2122
#ifndef ITK_WASM_NO_MEMORY_IO
2223
# include "itkWasmExports.h"
2324
#endif
2425

26+
#include <fstream>
27+
#include <sstream>
28+
2529
namespace itk
2630
{
2731
namespace wasm
2832
{
33+
void
34+
InputStreamBase::SetJSON(const std::string & json)
35+
{
36+
const auto wasmStringStream = WasmStringStream::New();
37+
wasmStringStream->SetJSON(json.c_str());
38+
m_IStream = std::make_unique<std::stringstream>(std::move(wasmStringStream->GetStringStream()));
39+
}
40+
41+
42+
void
43+
InputStreamBase::SetFile(const std::string & fileName, const std::ios_base::openmode openMode)
44+
{
45+
m_IStream = std::make_unique<std::ifstream>(fileName, openMode);
46+
}
47+
48+
49+
InputStreamBase::~InputStreamBase() = default;
50+
2951

3052
bool
3153
lexical_cast(const std::string & input, InputStreamBase & inputStream)

0 commit comments

Comments
 (0)