Skip to content

Commit 6f9573b

Browse files
committed
style: Use std::unique_ptr<std::ifstream> in InputTextStream
Avoided manual memory management. Followed the C++ Rule of Zero, by removing the user declared default-constructor and destructor of `InputTextStream`.
1 parent 3818ff2 commit 6f9573b

File tree

1 file changed

+10
-28
lines changed

1 file changed

+10
-28
lines changed

include/itkInputTextStream.h

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "itkPipeline.h"
2222
#include "itkWasmStringStream.h"
2323

24+
#include <memory> // For unique_ptr.
2425
#include <string>
2526
#include <sstream>
2627
#include <fstream>
@@ -48,54 +49,35 @@ class WebAssemblyInterface_EXPORT InputTextStream
4849
std::istream &
4950
Get()
5051
{
51-
return *m_IStream;
52+
return *GetPointer();
5253
}
5354

5455
std::istream *
5556
GetPointer()
5657
{
57-
return m_IStream;
58+
using ReturnType = std::istream *;
59+
return m_WasmStringStream ? ReturnType{ &(m_WasmStringStream->GetStringStream()) }
60+
: ReturnType{ m_InputFileStream.get() };
5861
}
5962

6063
void
6164
SetJSON(const std::string & json)
6265
{
63-
if (m_DeleteIStream && m_IStream != nullptr)
64-
{
65-
delete m_IStream;
66-
}
67-
m_DeleteIStream = false;
66+
m_InputFileStream.reset();
6867
m_WasmStringStream = WasmStringStream::New();
6968
m_WasmStringStream->SetJSON(json.c_str());
70-
71-
m_IStream = &(m_WasmStringStream->GetStringStream());
7269
}
7370

7471
void
7572
SetFileName(const std::string & fileName)
7673
{
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-
}
74+
m_WasmStringStream = nullptr;
75+
m_InputFileStream = std::make_unique<std::ifstream>(fileName);
9276
}
9377

9478
private:
95-
std::istream * m_IStream{ nullptr };
96-
bool m_DeleteIStream{ false };
97-
98-
WasmStringStream::Pointer m_WasmStringStream;
79+
std::unique_ptr<std::ifstream> m_InputFileStream;
80+
WasmStringStream::Pointer m_WasmStringStream;
9981
};
10082

10183

0 commit comments

Comments
 (0)