Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 5 additions & 61 deletions include/itkInputBinaryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@
#ifndef itkInputBinaryStream_h
#define itkInputBinaryStream_h

#include "itkPipeline.h"
#include "itkWasmStringStream.h"
#include "itkInputStreamBase.h"

#include <ios>
#include <string>
#ifndef ITK_WASM_NO_MEMORY_IO
# include <sstream>
#endif
#ifndef ITK_WASM_NO_FILESYSTEM_IO
# include <fstream>
#endif

#include "WebAssemblyInterfaceExport.h"

namespace itk
{
Expand All @@ -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

Expand Down
112 changes: 112 additions & 0 deletions include/itkInputStreamBase.h
Original file line number Diff line number Diff line change
@@ -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 <string>
#include <sstream>
#include <fstream>

#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
68 changes: 5 additions & 63 deletions include/itkInputTextStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@
#ifndef itkInputTextStream_h
#define itkInputTextStream_h

#include "itkPipeline.h"
#include "itkWasmStringStream.h"
#include "itkInputStreamBase.h"

#include <ios>
#include <string>
#include <sstream>
#include <fstream>

#include "WebAssemblyInterfaceExport.h"

namespace itk
{
Expand All @@ -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

Expand Down
3 changes: 1 addition & 2 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
59 changes: 0 additions & 59 deletions src/itkInputBinaryStream.cxx

This file was deleted.

4 changes: 2 additions & 2 deletions src/itkInputTextStream.cxx → src/itkInputStreamBase.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*
*=========================================================================*/
#include "itkInputTextStream.h"
#include "itkInputStreamBase.h"

#include <string>
#ifndef ITK_WASM_NO_MEMORY_IO
Expand All @@ -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())
{
Expand Down
Loading