From aa1f4b2677cf8ea464f1e9c84d6dfebeb4d0e115 Mon Sep 17 00:00:00 2001 From: Giovanni Bussi <giovanni.bussi@gmail.com> Date: Thu, 30 Jun 2011 19:09:31 +0200 Subject: [PATCH] Added automatic flushing of open files I overloaded fopen and fclose inside an Action in such a manner that a list of open files is kept. This allows to flush them implicitely, without requiring people to write an explicit flush() method. --- src/Action.cpp | 17 +++++++++++++++++ src/Action.h | 9 ++++++++- src/DumpDerivatives.cpp | 5 ----- src/Flush.cpp | 2 +- src/Print.cpp | 5 ----- 5 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/Action.cpp b/src/Action.cpp index 3aedd0b38..f886f7187 100644 --- a/src/Action.cpp +++ b/src/Action.cpp @@ -30,6 +30,23 @@ Action::Action(const ActionOptions&ao): log.printf(" with label %s\n",label.c_str()); } +FILE* Action::fopen(const char *path, const char *mode){ + FILE*fp=std::fopen(const_cast<char*>(path),const_cast<char*>(mode)); + files.insert(fp); + return fp; +} + +int Action::fclose(FILE*fp){ + files.erase(fp); + return std::fclose(fp); +} + +void Action::fflush(){ + for(files_iterator p=files.begin();p!=files.end();++p){ + std::fflush((*p)); + } +} + void Action::parseFlag(const std::string&key,bool & t){ if(!Tools::parseFlag(line,key,t)){ log.printf("ERROR parsing keyword %s\n",key.c_str()); diff --git a/src/Action.h b/src/Action.h index 61c111bbe..51d6f3d80 100644 --- a/src/Action.h +++ b/src/Action.h @@ -94,6 +94,10 @@ protected: /// Exit with error code c void exit(int c=0); +/// + std::set<FILE*> files; + typedef std::set<FILE*>::iterator files_iterator; + public: Action(const ActionOptions&); virtual ~Action(){}; @@ -111,7 +115,7 @@ public: virtual void apply()=0; /// Tell to the Action to flush open files - virtual void flush(){}; + void fflush(); virtual std::string getDocumentation()const; @@ -138,6 +142,9 @@ public: /// Perform calculation using numerical derivatives virtual void calculateNumericalDerivatives(); + + FILE *fopen(const char *path, const char *mode); + int fclose(FILE*fp); }; ///////////////////// diff --git a/src/DumpDerivatives.cpp b/src/DumpDerivatives.cpp index 4f785ea92..c37e354b5 100644 --- a/src/DumpDerivatives.cpp +++ b/src/DumpDerivatives.cpp @@ -46,7 +46,6 @@ public: void calculate(); DumpDerivatives(const ActionOptions&); void apply(){}; - void flush(); ~DumpDerivatives(); }; @@ -98,10 +97,6 @@ DumpDerivatives::~DumpDerivatives(){ if(fp) fclose(fp); } -void DumpDerivatives::flush(){ - if(fp) fflush(fp); -} - } diff --git a/src/Flush.cpp b/src/Flush.cpp index 60c31c09f..99c6ab024 100644 --- a/src/Flush.cpp +++ b/src/Flush.cpp @@ -34,7 +34,7 @@ public: void apply(){ const ActionSet & actionSet(plumed.getActionSet()); for(ActionSet::const_iterator p=actionSet.begin();p!=actionSet.end();++p) - (*p)->flush(); + (*p)->fflush(); } }; diff --git a/src/Print.cpp b/src/Print.cpp index a0ad96632..5af0cf4c7 100644 --- a/src/Print.cpp +++ b/src/Print.cpp @@ -45,7 +45,6 @@ public: void calculate(); Print(const ActionOptions&); void apply(){}; - void flush(); ~Print(); }; @@ -101,10 +100,6 @@ Print::~Print(){ if(fp) fclose(fp); } -void Print::flush(){ - if(fp) fflush(fp); -} - } -- GitLab