Commit 5abbbd79 authored by Stéphane Albert's avatar Stéphane Albert

ENH: Implemented SerializableInterface to factorize code ('template method'...

ENH: Implemented SerializableInterface to factorize code ('template method' pattern); Derived DatasetDescriptor from SerializableInterface.
parent 4f33373d
......@@ -38,8 +38,8 @@
//
// Monteverdi includes (sorted by alphabetic order)
#include "mvdVectorImageModel.h"
#include "mvdSystemError.h"
#include "mvdVectorImageModel.h"
/*****************************************************************************/
......@@ -327,28 +327,7 @@ DatasetDescriptor
/*******************************************************************************/
void
DatasetDescriptor
::Read( const QString& filename )
{
// File instance.
QFile file( filename );
// Open file on device.
if( !file.open( QIODevice::ReadOnly | QIODevice::Text ) )
throw SystemError(
ToStdString(
QString( "('%1')" ).arg( filename ) )
);
// Read file context.
Read( &file );
// File is closed by automatic scope detruction of QFile instance.
}
/*******************************************************************************/
void
DatasetDescriptor
::Read( QIODevice* device )
::virtual_Read( QIODevice* device )
{
qDebug() << "Reading XML descriptor...";
......@@ -391,45 +370,7 @@ DatasetDescriptor
/*******************************************************************************/
void
DatasetDescriptor
::Write( const QString& filename ) const
{
// File instance.
QFile file( filename );
// Open file on device.
if( !file.open( QIODevice::WriteOnly | QIODevice::Text ) )
throw SystemError(
ToStdString(
QString( "('%1')" ).arg( filename ) )
);
try
{
// Read file context.
Write( file );
}
catch( SystemError& syserr )
{
// Catch any SystemError thrown by DatasetDescriptor::Write() and
// morph it into the same SystemError containing filename
// information.
syserr = SystemError(
syserr.GetErrorCode(),
ToStdString(
QString( "('%1')" ).arg( filename ) )
);
// Throw morphed SystemError.
throw syserr;
}
// File is closed by automatic scope detruction of QFile instance.
}
/*******************************************************************************/
void
DatasetDescriptor
::Write( QIODevice& device ) const
::virtual_Write( QIODevice& device ) const
{
#if 0
qDebug()
......
......@@ -47,6 +47,7 @@
// Monteverdi includes (sorted by alphabetic order)
#include "mvdTypes.h"
#include "mvdAbstractModel.h"
#include "mvdSerializableInterface.h"
/*****************************************************************************/
......@@ -73,7 +74,8 @@ class AbstractImageModel;
* \brief WIP.
*/
class Monteverdi2_EXPORT DatasetDescriptor :
public AbstractModel
public AbstractModel,
private SerializableInterface
{
/*-[ QOBJECT SECTION ]-----------------------------------------------------*/
......@@ -148,7 +150,7 @@ public:
/**
*/
void Write( const QString& filename ) const;
inline void Write( const QString& filename ) const;
/*-[ SIGNALS SECTION ]-----------------------------------------------------*/
......@@ -217,19 +219,23 @@ private:
/**
*/
void Read( const QString& filename );
void BuildDocument();
/**
*/
void Read( QIODevice* device );
inline void Read( const QString& filename );
/**
*/
void Write( QIODevice& device ) const;
//
// SerializableInterface overrides.
//
using SerializableInterface::Read;
using SerializableInterface::Write;
virtual void virtual_Read( QIODevice* device );
virtual void virtual_Write( QIODevice& device ) const;
/**
*/
void BuildDocument();
//
// Private attributes.
......@@ -290,6 +296,24 @@ private slots:
namespace mvd
{
/*****************************************************************************/
inline
void
DatasetDescriptor
::Read( const QString& filename )
{
Read( filename, SerializableInterface::MODE_TEXT );
}
/*****************************************************************************/
inline
void
DatasetDescriptor
::Write( const QString& filename ) const
{
Write( filename, SerializableInterface::MODE_TEXT );
}
/*****************************************************************************/
inline
QDomElement
......
......@@ -87,11 +87,12 @@ HistogramModel
// itk::NumericsTraits<>::Clamp(...) was removed
// TODO : when otb::Clamp will be developped, use this function
measurement[0] =
measurement[0]<histogram->GetBinMin(0, 0)?
histogram->GetBinMin(0, 0):(measurement[0]>histogram->GetBinMax(0, histogram->GetSize(0) - 1)?
histogram->GetBinMax(0, histogram->GetSize(0) - 1):measurement[0]);
measurement[0] < histogram->GetBinMin(0, 0)
? histogram->GetBinMin(0, 0)
: ( measurement[0] > histogram->GetBinMax(0, histogram->GetSize(0) - 1)
? histogram->GetBinMax(0, histogram->GetSize(0) - 1)
: measurement[0] );
// Get the index of measurement in 1D-histogram.
Histogram::IndexType index;
if( !histogram->GetIndex( measurement, index ) )
......
......@@ -38,6 +38,8 @@
//
// Monteverdi includes (sorted by alphabetic order)
#include "mvdAlgorithm.h"
#include "mvdSystemError.h"
namespace mvd
{
......@@ -65,6 +67,94 @@ SerializableInterface
{
}
/*******************************************************************************/
void
SerializableInterface
::Read( const QString& filename, Mode mode )
{
// File instance.
QFile file( filename );
// Setup open-mode.
QIODevice::OpenMode openMode( QIODevice::ReadOnly );
if( mode==MODE_TEXT )
openMode |= QIODevice::Text;
// Open file on device.
if( !file.open( openMode ) )
throw SystemError(
ToStdString(
QString( "('%1')" ).arg( filename ) )
);
try
{
// Read file context.
virtual_Read( &file );
}
catch( SystemError& syserr )
{
// Catch any SystemError thrown by DatasetDescriptor::Write() and
// morph it into the same SystemError containing filename
// information.
syserr = SystemError(
syserr.GetErrorCode(),
ToStdString(
QString( "('%1')" ).arg( filename ) )
);
// Throw morphed SystemError.
throw syserr;
}
// File is closed by automatic scope detruction of QFile instance.
}
/*******************************************************************************/
void
SerializableInterface
::Write( const QString& filename, Mode mode ) const
{
// File instance.
QFile file( filename );
// Setup open-mode.
QIODevice::OpenMode openMode( QIODevice::WriteOnly );
if( mode==MODE_TEXT )
openMode |= QIODevice::Text;
// Open file on device.
if( !file.open( openMode ) )
throw SystemError(
ToStdString(
QString( "('%1')" ).arg( filename ) )
);
try
{
// Write file context.
virtual_Write( file );
}
catch( SystemError& syserr )
{
// Catch any SystemError thrown by DatasetDescriptor::Write() and
// morph it into the same SystemError containing filename
// information.
syserr = SystemError(
syserr.GetErrorCode(),
ToStdString(
QString( "('%1')" ).arg( filename ) )
);
// Throw morphed SystemError.
throw syserr;
}
// File is closed by automatic scope detruction of QFile instance.
}
/*******************************************************************************/
} // end namespace 'mvd'
......@@ -32,6 +32,7 @@
//
// Qt includes (sorted by alphabetic order)
//// Must be included before system/custom includes.
#include <QtCore>
//
// System includes (sorted by alphabetic order)
......@@ -44,7 +45,6 @@
//
// Monteverdi includes (sorted by alphabetic order)
#include "mvdModifiableInterface.h"
/*****************************************************************************/
......@@ -65,15 +65,27 @@ namespace mvd
/*****************************************************************************/
/* CLASS DEFINITION SECTION */
/** \class SerializableInterface
/**
* \class SerializableInterface
*
* \brief 'Template method' pattern applied to serializable objects.
*/
class Monteverdi2_EXPORT SerializableInterface :
public ModifiableInterface
class Monteverdi2_EXPORT SerializableInterface
{
/*-[ PUBLIC SECTION ]------------------------------------------------------*/
//
// Public types.
public:
/**
*/
enum Mode
{
MODE_BINARY = 0,
MODE_TEXT = 1,
};
//
// Public methods.
public:
......@@ -83,7 +95,11 @@ public:
/**
*/
inline void Save();
void Write( const QString& filename, Mode mode ) const;
/**
*/
void Read( const QString& filename, Mode mode );
/*-[ PROTECTED SECTION ]---------------------------------------------------*/
......@@ -95,7 +111,11 @@ protected:
/**
*/
virtual void virtual_Save() =0;
virtual void virtual_Write( QIODevice& device ) const =0;
/**
*/
virtual void virtual_Read( QIODevice* device ) =0;
//
// Protected attributes.
......@@ -121,17 +141,6 @@ private:
namespace mvd
{
/*****************************************************************************/
inline
void
SerializableInterface
::Save()
{
virtual_Save();
ClearModified();
}
} // end namespace 'mvd'
#endif // __mvdSerializableInterface_h
......@@ -280,15 +280,12 @@ I18nMainWindow
dockArea
);
#if 0
TController* controller =
#endif
new TController(
// wraps:
widget,
// as chid of:
dockWidget
);
new TController(
// wraps:
widget,
// as chid of:
dockWidget
);
return dockWidget;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment