Use factory for dump style creation

This commit is contained in:
Richard Berger
2016-09-08 00:45:03 -04:00
parent 9952d8a210
commit fd2b886422
3 changed files with 40 additions and 12 deletions

View File

@ -735,10 +735,11 @@ void Info::dump_styles(FILE * out)
fprintf(out, "\nDump styles:\n");
vector<string> styles;
#define DUMP_CLASS
#define DumpStyle(key,Class) styles.push_back(#key);
#include "style_dump.h"
#undef DUMP_CLASS
for(Output::DumpCreatorMap::iterator it = output->dump_map->begin(); it != output->dump_map->end(); ++it) {
styles.push_back(it->first);
}
print_columns(out, styles);
fprintf(out, "\n\n\n");
}

View File

@ -89,6 +89,15 @@ Output::Output(LAMMPS *lmp) : Pointers(lmp)
restart1 = restart2a = restart2b = NULL;
var_restart_single = var_restart_double = NULL;
restart = NULL;
dump_map = new DumpCreatorMap();
#define DUMP_CLASS
#define DumpStyle(key,Class) \
(*dump_map)[#key] = &dump_creator<Class>;
#include "style_dump.h"
#undef DumpStyle
#undef DUMP_CLASS
}
/* ----------------------------------------------------------------------
@ -115,6 +124,8 @@ Output::~Output()
delete [] var_restart_single;
delete [] var_restart_double;
delete restart;
delete dump_map;
}
/* ---------------------------------------------------------------------- */
@ -571,14 +582,10 @@ void Output::add_dump(int narg, char **arg)
// create the Dump
if (0) return; // dummy line to enable else-if macro expansion
#define DUMP_CLASS
#define DumpStyle(key,Class) \
else if (strcmp(arg[2],#key) == 0) dump[ndump] = new Class(lmp,narg,arg);
#include "style_dump.h"
#undef DUMP_CLASS
if (dump_map->find(arg[2]) != dump_map->end()) {
DumpCreator dump_creator = (*dump_map)[arg[2]];
dump[ndump] = dump_creator(lmp, narg, arg);
}
else error->all(FLERR,"Unknown dump style");
every_dump[ndump] = force->inumeric(FLERR,arg[3]);
@ -588,6 +595,16 @@ void Output::add_dump(int narg, char **arg)
ndump++;
}
/* ----------------------------------------------------------------------
one instance per dump style in style_dump.h
------------------------------------------------------------------------- */
template <typename T>
Dump *Output::dump_creator(LAMMPS *lmp, int narg, char ** arg)
{
return new T(lmp, narg, arg);
}
/* ----------------------------------------------------------------------
modify parameters of a Dump
------------------------------------------------------------------------- */

View File

@ -15,6 +15,8 @@
#define LMP_OUTPUT_H
#include "pointers.h"
#include <map>
#include <string>
namespace LAMMPS_NS {
@ -57,6 +59,11 @@ class Output : protected Pointers {
char *restart2a,*restart2b; // names of double restart files
class WriteRestart *restart; // class for writing restart files
typedef Dump *(*DumpCreator)(LAMMPS *,int,char**);
typedef std::map<std::string,DumpCreator> DumpCreatorMap;
DumpCreatorMap *dump_map;
Output(class LAMMPS *);
~Output();
void init();
@ -75,6 +82,9 @@ class Output : protected Pointers {
void create_restart(int, char **); // create Restart and restart files
void memory_usage(); // print out memory usage
private:
template <typename T> static Dump *dump_creator(LAMMPS *, int, char **);
};
}