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
70 changes: 6 additions & 64 deletions include/itkOutputBinaryStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@
#ifndef itkOutputBinaryStream_h
#define itkOutputBinaryStream_h

#include "itkPipeline.h"
#include "itkWasmStringStream.h"
#include "itkOutputStreamBase.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 @@ -38,70 +30,20 @@ namespace wasm

/**
*\class OutputBinaryStream
* \brief Output text std::ostream for an itk::wasm::Pipeline
*
* This stream is written to the filesystem or memory when the object goes out of scope.
*
* Call `Get()` to get the std::ostream & to use an output for a pipeline.
* \brief Output binary std::ostream for an itk::wasm::Pipeline
*
* \ingroup WebAssemblyInterface
*/
class WebAssemblyInterface_EXPORT OutputBinaryStream
class OutputBinaryStream: public OutputStreamBase
{
public:
std::ostream &
Get()
{
return *m_OStream;
}

void
SetFileName(const std::string & fileName)
SetFileName(const std::string & fileName) override
{
if (m_DeleteOStream && m_OStream != nullptr)
{
delete m_OStream;
}
m_OStream = new std::ofstream(fileName, std::ofstream::out | std::ofstream::binary);
m_DeleteOStream = true;
OutputStreamBase::SetFile(fileName, std::ios_base::binary);
}

OutputBinaryStream() = default;
~OutputBinaryStream();

/** Output index. */
void
SetIdentifier(const std::string & identifier)
{
if (m_DeleteOStream && m_OStream != nullptr)
{
delete m_OStream;
}
m_DeleteOStream = false;
m_WasmStringStream = WasmStringStream::New();

m_OStream = &(m_WasmStringStream->GetStringStream());
this->m_Identifier = identifier;
}
const std::string &
GetIdentifier() const
{
return this->m_Identifier;
}

private:
std::ostream * m_OStream{ nullptr };
bool m_DeleteOStream{ false };

std::string m_Identifier;

WasmStringStream::Pointer m_WasmStringStream;
};


WebAssemblyInterface_EXPORT bool
lexical_cast(const std::string & output, OutputBinaryStream & outputStream);

} // end namespace wasm
} // end namespace itk

Expand Down
112 changes: 112 additions & 0 deletions include/itkOutputStreamBase.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 itkOutputStreamBase_h
#define itkOutputStreamBase_h

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

#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
{
namespace wasm
{

/**
*\class OutputStreamBase
* \brief Base class of output stream for an itk::wasm::Pipeline
*
* This stream is written to the filesystem or memory when the object goes out of scope.
*
* Call `Get()` to get the std::ostream & to use an output for a pipeline.
*
* \ingroup WebAssemblyInterface
*/
class WebAssemblyInterface_EXPORT OutputStreamBase
{
public:
std::ostream &
Get()
{
return *m_OStream;
}

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

/** Output index. */
void
SetIdentifier(const std::string & identifier)
{
if (m_DeleteOStream && m_OStream != nullptr)
{
delete m_OStream;
}
m_DeleteOStream = false;
m_WasmStringStream = WasmStringStream::New();

m_OStream = &(m_WasmStringStream->GetStringStream());
this->m_Identifier = identifier;
}
const std::string &
GetIdentifier() const
{
return this->m_Identifier;
}

protected:
OutputStreamBase() = default;
virtual ~OutputStreamBase();

void
SetFile(const std::string & fileName, const std::ios_base::openmode openMode)
{
if (m_DeleteOStream && m_OStream != nullptr)
{
delete m_OStream;
}
m_OStream = new std::ofstream(fileName, openMode);
m_DeleteOStream = true;
}

private:
std::ostream * m_OStream{ nullptr };
bool m_DeleteOStream{ false };

std::string m_Identifier;

WasmStringStream::Pointer m_WasmStringStream;
};


WebAssemblyInterface_EXPORT bool
lexical_cast(const std::string & output, OutputStreamBase & outputStream);

} // end namespace wasm
} // end namespace itk

#endif
68 changes: 5 additions & 63 deletions include/itkOutputTextStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@
#ifndef itkOutputTextStream_h
#define itkOutputTextStream_h

#include "itkPipeline.h"
#include "itkWasmStringStream.h"
#include "itkOutputStreamBase.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,68 +32,18 @@ namespace wasm
*\class OutputTextStream
* \brief Output text std::ostream for an itk::wasm::Pipeline
*
* This stream is written to the filesystem or memory when the object goes out of scope.
*
* Call `Get()` to get the std::ostream & to use an output for a pipeline.
*
* \ingroup WebAssemblyInterface
*/
class WebAssemblyInterface_EXPORT OutputTextStream
class OutputTextStream: public OutputStreamBase
{
public:
std::ostream &
Get()
{
return *m_OStream;
}

void
SetFileName(const std::string & fileName)
SetFileName(const std::string & fileName) override
{
if (m_DeleteOStream && m_OStream != nullptr)
{
delete m_OStream;
}
m_OStream = new std::ofstream(fileName, std::ofstream::out);
m_DeleteOStream = true;
OutputStreamBase::SetFile(fileName, std::ios_base::openmode{});
}

OutputTextStream() = default;
~OutputTextStream();

/** Output index. */
void
SetIdentifier(const std::string & identifier)
{
if (m_DeleteOStream && m_OStream != nullptr)
{
delete m_OStream;
}
m_DeleteOStream = false;
m_WasmStringStream = WasmStringStream::New();

m_OStream = &(m_WasmStringStream->GetStringStream());
this->m_Identifier = identifier;
}
const std::string &
GetIdentifier() const
{
return this->m_Identifier;
}

private:
std::ostream * m_OStream{ nullptr };
bool m_DeleteOStream{ false };

std::string m_Identifier;

WasmStringStream::Pointer m_WasmStringStream;
};


WebAssemblyInterface_EXPORT bool
lexical_cast(const std::string & output, OutputTextStream & outputStream);

} // 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 @@ -15,8 +15,7 @@ set(WebAssemblyInterface_SRCS
itkWasmTransformIO.cxx
itkWasmStringStream.cxx
itkInputStreamBase.cxx
itkOutputTextStream.cxx
itkOutputBinaryStream.cxx
itkOutputStreamBase.cxx
itkIOComponentEnumFromWasmComponentType.cxx
itkIOPixelEnumFromWasmPixelType.cxx
itkWasmComponentTypeFromIOComponentEnum.cxx
Expand Down
Loading
Loading