Skip to content
Snippets Groups Projects
Commit aa1f4b26 authored by Giovanni Bussi's avatar Giovanni Bussi
Browse files

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.
parent 9eaf110b
No related branches found
No related tags found
No related merge requests found
......@@ -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());
......
......@@ -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);
};
/////////////////////
......
......@@ -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);
}
}
......@@ -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();
}
};
......
......@@ -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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment