Skip to content
Snippets Groups Projects
Commit 85c13294 authored by Richard Berger's avatar Richard Berger
Browse files

Use factory for integrate style creation

parent 55260ad5
No related branches found
No related tags found
No related merge requests found
...@@ -582,10 +582,11 @@ void Info::integrate_styles(FILE * out) ...@@ -582,10 +582,11 @@ void Info::integrate_styles(FILE * out)
fprintf(out, "\nIntegrate styles:\n"); fprintf(out, "\nIntegrate styles:\n");
vector<string> styles; vector<string> styles;
#define INTEGRATE_CLASS
#define IntegrateStyle(key,Class) styles.push_back(#key); for(Update::IntegrateCreatorMap::iterator it = update->integrate_map->begin(); it != update->integrate_map->end(); ++it) {
#include "style_integrate.h" styles.push_back(it->first);
#undef INTEGRATE_CLASS }
print_columns(out, styles); print_columns(out, styles);
fprintf(out, "\n\n\n"); fprintf(out, "\n\n\n");
} }
......
...@@ -61,6 +61,15 @@ Update::Update(LAMMPS *lmp) : Pointers(lmp) ...@@ -61,6 +61,15 @@ Update::Update(LAMMPS *lmp) : Pointers(lmp)
minimize_style = NULL; minimize_style = NULL;
minimize = NULL; minimize = NULL;
integrate_map = new IntegrateCreatorMap();
#define INTEGRATE_CLASS
#define IntegrateStyle(key,Class) \
(*integrate_map)[#key] = &integrate_creator<Class>;
#include "style_integrate.h"
#undef IntegrateStyle
#undef INTEGRATE_CLASS
str = (char *) "verlet"; str = (char *) "verlet";
create_integrate(1,&str,1); create_integrate(1,&str,1);
...@@ -79,6 +88,8 @@ Update::~Update() ...@@ -79,6 +88,8 @@ Update::~Update()
delete [] minimize_style; delete [] minimize_style;
delete minimize; delete minimize;
delete integrate_map;
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
...@@ -319,52 +330,43 @@ void Update::new_integrate(char *style, int narg, char **arg, ...@@ -319,52 +330,43 @@ void Update::new_integrate(char *style, int narg, char **arg,
sflag = 1; sflag = 1;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix); sprintf(estyle,"%s/%s",style,lmp->suffix);
int success = 1; if (integrate_map->find(estyle) != integrate_map->end()) {
IntegrateCreator integrate_creator = (*integrate_map)[estyle];
if (0) return; integrate = integrate_creator(lmp, narg, arg);
return;
#define INTEGRATE_CLASS }
#define IntegrateStyle(key,Class) \
else if (strcmp(estyle,#key) == 0) integrate = new Class(lmp,narg,arg);
#include "style_integrate.h"
#undef IntegrateStyle
#undef INTEGRATE_CLASS
else success = 0;
if (success) return;
} }
if (lmp->suffix2) { if (lmp->suffix2) {
sflag = 2; sflag = 2;
char estyle[256]; char estyle[256];
sprintf(estyle,"%s/%s",style,lmp->suffix2); sprintf(estyle,"%s/%s",style,lmp->suffix2);
int success = 1; if (integrate_map->find(estyle) != integrate_map->end()) {
IntegrateCreator integrate_creator = (*integrate_map)[estyle];
if (0) return; integrate = integrate_creator(lmp, narg, arg);
return;
#define INTEGRATE_CLASS }
#define IntegrateStyle(key,Class) \
else if (strcmp(estyle,#key) == 0) integrate = new Class(lmp,narg,arg);
#include "style_integrate.h"
#undef IntegrateStyle
#undef INTEGRATE_CLASS
else success = 0;
if (success) return;
} }
} }
sflag = 0; sflag = 0;
if (0) return; if (integrate_map->find(style) != integrate_map->end()) {
IntegrateCreator integrate_creator = (*integrate_map)[style];
integrate = integrate_creator(lmp, narg, arg);
return;
}
#define INTEGRATE_CLASS error->all(FLERR,"Illegal integrate style");
#define IntegrateStyle(key,Class) \ }
else if (strcmp(style,#key) == 0) integrate = new Class(lmp,narg,arg);
#include "style_integrate.h"
#undef IntegrateStyle
#undef INTEGRATE_CLASS
else error->all(FLERR,"Illegal integrate style"); /* ----------------------------------------------------------------------
one instance per integrate style in style_integrate.h
------------------------------------------------------------------------- */
template <typename T>
Integrate *Update::integrate_creator(LAMMPS *lmp, int narg, char ** arg)
{
return new T(lmp, narg, arg);
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
......
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
#define LMP_UPDATE_H #define LMP_UPDATE_H
#include "pointers.h" #include "pointers.h"
#include <map>
#include <string>
namespace LAMMPS_NS { namespace LAMMPS_NS {
...@@ -46,6 +48,10 @@ class Update : protected Pointers { ...@@ -46,6 +48,10 @@ class Update : protected Pointers {
class Min *minimize; class Min *minimize;
char *minimize_style; char *minimize_style;
typedef Integrate *(*IntegrateCreator)(LAMMPS *,int,char**);
typedef std::map<std::string,IntegrateCreator> IntegrateCreatorMap;
IntegrateCreatorMap *integrate_map;
Update(class LAMMPS *); Update(class LAMMPS *);
~Update(); ~Update();
void init(); void init();
...@@ -60,6 +66,7 @@ class Update : protected Pointers { ...@@ -60,6 +66,7 @@ class Update : protected Pointers {
private: private:
void new_integrate(char *, int, char **, int, int &); void new_integrate(char *, int, char **, int, int &);
template <typename T> static Integrate *integrate_creator(LAMMPS *, int, char **);
}; };
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment