diff --git a/include/itkInputBinaryStream.h b/include/itkInputBinaryStream.h index 3970d7bf8..35dd8affb 100644 --- a/include/itkInputBinaryStream.h +++ b/include/itkInputBinaryStream.h @@ -18,18 +18,10 @@ #ifndef itkInputBinaryStream_h #define itkInputBinaryStream_h -#include "itkPipeline.h" -#include "itkWasmStringStream.h" +#include "itkInputStreamBase.h" +#include #include -#ifndef ITK_WASM_NO_MEMORY_IO -# include -#endif -#ifndef ITK_WASM_NO_FILESYSTEM_IO -# include -#endif - -#include "WebAssemblyInterfaceExport.h" namespace itk { @@ -40,66 +32,18 @@ namespace wasm *\class InputBinaryStream * \brief Input binary std::istream for an itk::wasm::Pipeline * - * This stream is read from the filesystem or memory when ITK_WASM_PARSE_ARGS is called. - * - * Call `Get()` to get the std::istream & to use an input to a pipeline. - * * \ingroup WebAssemblyInterface */ -class WebAssemblyInterface_EXPORT InputBinaryStream +class InputBinaryStream : public InputStreamBase { public: - std::istream & - Get() - { - return *m_IStream; - } - - void - SetJSON(const std::string & json) - { - if (m_DeleteIStream && m_IStream != nullptr) - { - delete m_IStream; - } - m_DeleteIStream = false; - m_WasmStringStream = WasmStringStream::New(); - m_WasmStringStream->SetJSON(json.c_str()); - - m_IStream = &(m_WasmStringStream->GetStringStream()); - } - void - SetFileName(const std::string & fileName) - { - if (m_DeleteIStream && m_IStream != nullptr) - { - delete m_IStream; - } - m_IStream = new std::ifstream(fileName, std::ifstream::in | std::ifstream::binary); - m_DeleteIStream = true; - } - - InputBinaryStream() = default; - ~InputBinaryStream() + SetFileName(const std::string & fileName) override { - if (m_DeleteIStream && m_IStream != nullptr) - { - delete m_IStream; - } + InputStreamBase::SetFile(fileName, std::ios_base::binary); } - -private: - std::istream * m_IStream{ nullptr }; - bool m_DeleteIStream{ false }; - - WasmStringStream::Pointer m_WasmStringStream; }; - -WebAssemblyInterface_EXPORT bool -lexical_cast(const std::string & input, InputBinaryStream & inputStream); - } // end namespace wasm } // end namespace itk diff --git a/include/itkInputStreamBase.h b/include/itkInputStreamBase.h new file mode 100644 index 000000000..f06fc0a44 --- /dev/null +++ b/include/itkInputStreamBase.h @@ -0,0 +1,112 @@ +/*========================================================================= + * + * Copyright NumFOCUS + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0.txt + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *=========================================================================*/ +#ifndef itkInputStreamBase_h +#define itkInputStreamBase_h + +#include "itkPipeline.h" +#include "itkWasmStringStream.h" + +#include +#include +#include + +#include "WebAssemblyInterfaceExport.h" + +namespace itk +{ +namespace wasm +{ + +/** + *\class InputStreamBase + * \brief Base class of input stream for an itk::wasm::Pipeline + * + * This stream is read from the filesystem or memory when ITK_WASM_PARSE_ARGS is called. + * + * Call `Get()` to get the std::istream & to use an input to a pipeline. + * + * \ingroup WebAssemblyInterface + */ +class WebAssemblyInterface_EXPORT InputStreamBase +{ +public: + std::istream & + Get() + { + return *m_IStream; + } + + std::istream * + GetPointer() + { + return m_IStream; + } + + void + SetJSON(const std::string & json) + { + if (m_DeleteIStream && m_IStream != nullptr) + { + delete m_IStream; + } + m_DeleteIStream = false; + m_WasmStringStream = WasmStringStream::New(); + m_WasmStringStream->SetJSON(json.c_str()); + + m_IStream = &(m_WasmStringStream->GetStringStream()); + } + + virtual void + SetFileName(const std::string & fileName) = 0; + +protected: + void + SetFile(const std::string & fileName, const std::ios_base::openmode openMode) + { + if (m_DeleteIStream && m_IStream != nullptr) + { + delete m_IStream; + } + m_IStream = new std::ifstream(fileName, openMode); + m_DeleteIStream = true; + } + + InputStreamBase() = default; + virtual ~InputStreamBase() + { + if (m_DeleteIStream && m_IStream != nullptr) + { + delete m_IStream; + } + } + +private: + std::istream * m_IStream{ nullptr }; + bool m_DeleteIStream{ false }; + + WasmStringStream::Pointer m_WasmStringStream; +}; + + +WebAssemblyInterface_EXPORT bool +lexical_cast(const std::string & input, InputStreamBase & inputStream); + +} // end namespace wasm +} // end namespace itk + +#endif diff --git a/include/itkInputTextStream.h b/include/itkInputTextStream.h index 6f27393a2..7bb602083 100644 --- a/include/itkInputTextStream.h +++ b/include/itkInputTextStream.h @@ -18,14 +18,10 @@ #ifndef itkInputTextStream_h #define itkInputTextStream_h -#include "itkPipeline.h" -#include "itkWasmStringStream.h" +#include "itkInputStreamBase.h" +#include #include -#include -#include - -#include "WebAssemblyInterfaceExport.h" namespace itk { @@ -36,72 +32,18 @@ namespace wasm *\class InputTextStream * \brief Input text std::istream for an itk::wasm::Pipeline * - * This stream is read from the filesystem or memory when ITK_WASM_PARSE_ARGS is called. - * - * Call `Get()` to get the std::istream & to use an input to a pipeline. - * * \ingroup WebAssemblyInterface */ -class WebAssemblyInterface_EXPORT InputTextStream +class InputTextStream: public InputStreamBase { public: - std::istream & - Get() - { - return *m_IStream; - } - - std::istream * - GetPointer() - { - return m_IStream; - } - void - SetJSON(const std::string & json) + SetFileName(const std::string & fileName) override { - if (m_DeleteIStream && m_IStream != nullptr) - { - delete m_IStream; - } - m_DeleteIStream = false; - m_WasmStringStream = WasmStringStream::New(); - m_WasmStringStream->SetJSON(json.c_str()); - - m_IStream = &(m_WasmStringStream->GetStringStream()); + InputStreamBase::SetFile(fileName, std::ios_base::openmode{}); } - - void - SetFileName(const std::string & fileName) - { - if (m_DeleteIStream && m_IStream != nullptr) - { - delete m_IStream; - } - m_IStream = new std::ifstream(fileName, std::ifstream::in); - m_DeleteIStream = true; - } - - InputTextStream() = default; - ~InputTextStream() - { - if (m_DeleteIStream && m_IStream != nullptr) - { - delete m_IStream; - } - } - -private: - std::istream * m_IStream{ nullptr }; - bool m_DeleteIStream{ false }; - - WasmStringStream::Pointer m_WasmStringStream; }; - -WebAssemblyInterface_EXPORT bool -lexical_cast(const std::string & input, InputTextStream & inputStream); - } // end namespace wasm } // end namespace itk diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index fec2bdba4..675f544a0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -14,9 +14,8 @@ set(WebAssemblyInterface_SRCS itkWasmTransformIOFactory.cxx itkWasmTransformIO.cxx itkWasmStringStream.cxx - itkInputTextStream.cxx + itkInputStreamBase.cxx itkOutputTextStream.cxx - itkInputBinaryStream.cxx itkOutputBinaryStream.cxx itkIOComponentEnumFromWasmComponentType.cxx itkIOPixelEnumFromWasmPixelType.cxx diff --git a/src/itkInputBinaryStream.cxx b/src/itkInputBinaryStream.cxx deleted file mode 100644 index ae6616f34..000000000 --- a/src/itkInputBinaryStream.cxx +++ /dev/null @@ -1,59 +0,0 @@ -/*========================================================================= - * - * Copyright NumFOCUS - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0.txt - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - *=========================================================================*/ -#include "itkInputBinaryStream.h" - -#include -#ifndef ITK_WASM_NO_MEMORY_IO -# include "itkWasmExports.h" -#endif - -namespace itk -{ -namespace wasm -{ - -bool -lexical_cast(const std::string & input, InputBinaryStream & inputStream) -{ - if (input.empty()) - { - return false; - } - if (wasm::Pipeline::get_use_memory_io()) - { -#ifndef ITK_WASM_NO_MEMORY_IO - const unsigned int index = std::stoi(input); - const auto json = getMemoryStoreInputJSON(0, index); - inputStream.SetJSON(json); -#else - return false; -#endif - } - else - { -#ifndef ITK_WASM_NO_FILESYSTEM_IO - inputStream.SetFileName(input); -#else - return false; -#endif - } - return true; -} - -} // end namespace wasm -} // end namespace itk diff --git a/src/itkInputTextStream.cxx b/src/itkInputStreamBase.cxx similarity index 93% rename from src/itkInputTextStream.cxx rename to src/itkInputStreamBase.cxx index 0687a1cab..60050e230 100644 --- a/src/itkInputTextStream.cxx +++ b/src/itkInputStreamBase.cxx @@ -15,7 +15,7 @@ * limitations under the License. * *=========================================================================*/ -#include "itkInputTextStream.h" +#include "itkInputStreamBase.h" #include #ifndef ITK_WASM_NO_MEMORY_IO @@ -28,7 +28,7 @@ namespace wasm { bool -lexical_cast(const std::string & input, InputTextStream & inputStream) +lexical_cast(const std::string & input, InputStreamBase & inputStream) { if (input.empty()) {