|
21 | 21 | #include "itkPipeline.h" |
22 | 22 | #include "itkWasmStringStream.h" |
23 | 23 |
|
| 24 | +#include <memory> // For unique_ptr. |
24 | 25 | #include <string> |
25 | 26 | #include <sstream> |
26 | 27 | #include <fstream> |
@@ -48,54 +49,35 @@ class WebAssemblyInterface_EXPORT InputTextStream |
48 | 49 | std::istream & |
49 | 50 | Get() |
50 | 51 | { |
51 | | - return *m_IStream; |
| 52 | + return *GetPointer(); |
52 | 53 | } |
53 | 54 |
|
54 | 55 | std::istream * |
55 | 56 | GetPointer() |
56 | 57 | { |
57 | | - return m_IStream; |
| 58 | + using ReturnType = std::istream *; |
| 59 | + return m_WasmStringStream ? ReturnType{ &(m_WasmStringStream->GetStringStream()) } |
| 60 | + : ReturnType{ m_InputFileStream.get() }; |
58 | 61 | } |
59 | 62 |
|
60 | 63 | void |
61 | 64 | SetJSON(const std::string & json) |
62 | 65 | { |
63 | | - if (m_DeleteIStream && m_IStream != nullptr) |
64 | | - { |
65 | | - delete m_IStream; |
66 | | - } |
67 | | - m_DeleteIStream = false; |
| 66 | + m_InputFileStream.reset(); |
68 | 67 | m_WasmStringStream = WasmStringStream::New(); |
69 | 68 | m_WasmStringStream->SetJSON(json.c_str()); |
70 | | - |
71 | | - m_IStream = &(m_WasmStringStream->GetStringStream()); |
72 | 69 | } |
73 | 70 |
|
74 | 71 | void |
75 | 72 | SetFileName(const std::string & fileName) |
76 | 73 | { |
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); |
92 | 76 | } |
93 | 77 |
|
94 | 78 | 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; |
99 | 81 | }; |
100 | 82 |
|
101 | 83 |
|
|
0 commit comments