Curves - Loader/writer separated from manager

The goal being to re-use the loader in the builder tool.
This commit is contained in:
Emmanuel BENOîT 2017-12-03 10:06:49 +01:00
parent d77943f953
commit 68ca08ceba
2 changed files with 107 additions and 55 deletions

139
c-sync.cc
View file

@ -15,13 +15,7 @@ using ebcl::T_SRDParserConfig;
namespace {
struct T_ParserOutput_
{
T_SyncCurves curves;
T_Optional< T_SyncTime > time;
ebcl::T_SRDLocation tLocation;
};
using T_ParserOutput_ = T_SyncCurvesIO::T_Data;
using namespace ebcl;
bool CPEnterCurve_( T_SRDParserData const& data )
@ -331,6 +325,78 @@ int32_t T_SyncCurves::indexOf(
}
/*= T_SyncCurvesIO ===========================================================*/
namespace {
struct T_SCIOImpl_
{
T_SCIOImpl_( ) noexcept;
T_SyncCurvesIO::T_Data load( T_String const& path ) const;
T_SRDParserConfig pConfig;
};
/*----------------------------------------------------------------------------*/
inline T_SCIOImpl_::T_SCIOImpl_( ) noexcept
: pConfig{ MakeCurvesParser_( ) }
{ }
inline T_SyncCurvesIO::T_Data T_SCIOImpl_::load(
T_String const& path ) const
{
T_File file( path , E_FileMode::READ_ONLY );
file.open( );
T_FileInputStream fis( file );
T_SRDParser parser( pConfig );
T_SRDTextReader reader( parser );
reader.read( path , fis );
return std::move( *parser.getData< T_SharedPtr< T_ParserOutput_ > >( ) );
}
/*----------------------------------------------------------------------------*/
} // namespace <anon>
T_SyncCurvesIO::T_SyncCurvesIO( ) noexcept
: A_PrivateImplementation( new T_SCIOImpl_( ) )
{ }
T_SyncCurvesIO::T_Data T_SyncCurvesIO::load(
T_String const& path ) const
{
return p< T_SCIOImpl_ >( ).load( path );
}
void T_SyncCurvesIO::save( T_String const& path ,
T_SyncCurves const& curves ,
T_SyncTime const& time ) const
{
T_StringBuilder sb;
sb << "(duration " << time.uDuration << ' ' << time.iDuration
<< ")\n";
for ( auto const& curve : curves.curves.values( ) ) {
sb << "\n(" << curve.name << '\n';
for ( auto const& s : curve.segments ) {
sb << "\t(segment " << s.type << "\n\t\t(values";
for ( auto v : s.values ) {
sb << ' ' << v;
}
sb << ")\n\t\t(durations";
for ( auto d : s.durations ) {
sb << ' ' << d;
}
sb << ")\n\t)\n";
}
sb << ")\n";
}
T_File{ path , E_FileMode::OVERWRITE }.write( sb.data( ) , sb.size( ) );
}
/*= T_SyncCurveCache =========================================================*/
T_SyncCurveCache::T_SyncCurveCache(
@ -613,8 +679,7 @@ T_SyncOverrideVisitor::T_OpElement T_SyncOverrideVisitor::nodeBrowser(
/*= T_SyncManager ============================================================*/
T_SyncManager::T_SyncManager( ) noexcept
: pConfig_( MakeCurvesParser_( ) ) ,
watcher_{ Common::Watcher( ) , [this](){ curvesChanged_( ); } } ,
: io_{ } , watcher_{ Common::Watcher( ) , [this](){ curvesChanged_( ); } } ,
soRoot_( "*root*" )
{
auto& p{ Common::Project( ) };
@ -704,19 +769,9 @@ bool T_SyncManager::loadCurves(
const bool undoable )
{
printf( "Loading curves data\n" );
T_SharedPtr< T_ParserOutput_ > p;
T_ParserOutput_ p;
try {
using namespace ebcl;
const T_SRDParserConfig cfg( MakeCurvesParser_( ) );
T_File file( curvesFile_ , E_FileMode::READ_ONLY );
file.open( );
T_FileInputStream fis( file );
T_SRDParser parser( cfg );
T_SRDTextReader reader( parser );
reader.read( curvesFile_ , fis );
p = parser.getData< T_SharedPtr< T_ParserOutput_ > >( );
p = io_.load( curvesFile_ );
} catch ( ebcl::X_StreamError const& e ) {
printf( "... ERR %s\n" , e.what( ) );
return false;
@ -734,14 +789,13 @@ bool T_SyncManager::loadCurves(
}
printf( "... success\n" );
assert( p );
if ( undoable ) {
addReloadUndoData_( (void*)(T_ParserOutput_*) p );
addReloadUndoData_( p );
}
curves_ = std::move( p->curves );
if ( p->time ) {
time_.iDuration = p->time->iDuration;
time_.uDuration = p->time->uDuration;
curves_ = std::move( p.curves );
if ( p.time ) {
time_.iDuration = p.time->iDuration;
time_.uDuration = p.time->uDuration;
} else {
time_.iDuration = 3600;
time_.uDuration = 1.f / 60.f;
@ -754,30 +808,10 @@ bool T_SyncManager::loadCurves(
bool T_SyncManager::saveCurves( )
{
T_StringBuilder sb;
sb << "(duration " << time_.uDuration << ' ' << time_.iDuration
<< ")\n";
for ( auto const& curve : curves_.curves.values( ) ) {
sb << "\n(" << curve.name << '\n';
for ( auto const& s : curve.segments ) {
sb << "\t(segment " << s.type << "\n\t\t(values";
for ( auto v : s.values ) {
sb << ' ' << v;
}
sb << ")\n\t\t(durations";
for ( auto d : s.durations ) {
sb << ' ' << d;
}
sb << ")\n\t)\n";
}
sb << ")\n";
}
saving_ = true;
printf( "Saving curves...\n" );
try {
T_File out{ "curves.srd" , E_FileMode::OVERWRITE };
out.write( sb.data( ) , sb.size( ) );
io_.save( curvesFile_ , curves_ , time_ );
} catch ( ebcl::X_StreamError const& e ) {
printf( "... ERR %s\n" , e.what( ) );
return false;
@ -788,16 +822,15 @@ bool T_SyncManager::saveCurves( )
}
void T_SyncManager::addReloadUndoData_(
void* const data ) const noexcept
T_SyncCurvesIO::T_Data const& data ) const noexcept
{
T_ParserOutput_& p{ *(T_ParserOutput_*)data };
auto& undo{ Common::Undo( ).add< T_UndoDurationChanges >(
p.time ? p.time->iDuration : 3600 ,
data.time ? data.time->iDuration : 3600 ,
time_.iDuration ,
p.time ? p.time->uDuration : ( 1.f / 60.f ) ,
data.time ? data.time->uDuration : ( 1.f / 60.f ) ,
time_.uDuration ) };
auto& nCurves{ p.curves.curves };
auto& nCurves{ data.curves.curves };
for ( auto const& curve : curves_.curves.values( ) ) {
auto const* const nc{ nCurves.get( curve.name ) };
if ( nc ) {

View file

@ -145,6 +145,24 @@ struct T_SyncCurves
}
};
// Curves data file loader/writer
struct T_SyncCurvesIO : public ebcl::A_PrivateImplementation
{
T_SyncCurvesIO( ) noexcept;
struct T_Data
{
T_SyncCurves curves;
T_Optional< T_SyncTime > time;
ebcl::T_SRDLocation tLocation;
};
T_Data load( T_String const& path ) const;
void save( T_String const& path ,
T_SyncCurves const& curves ,
T_SyncTime const& time ) const;
};
/*----------------------------------------------------------------------------*/
// Pre-computed data for a curve
@ -412,7 +430,8 @@ struct T_SyncManager : public virtual A_ProjectPathListener
private:
void curvesChanged_( );
void addReloadUndoData_( void* data ) const noexcept;
void addReloadUndoData_(
T_SyncCurvesIO::T_Data const& data ) const noexcept;
// ---------------------------------------------------------------------
@ -444,7 +463,7 @@ struct T_SyncManager : public virtual A_ProjectPathListener
// Private data
private:
ebcl::T_SRDParserConfig pConfig_; // Parser config for curves
T_SyncCurvesIO io_; // Curves loader/writer
T_String curvesFile_; // Path to the curves file
T_WatchedFiles watcher_; // Curves file watcher
bool saving_{ false }; // True if file is being saved