From 85c132943ed5574d07d346e23409a558bbb73dbd Mon Sep 17 00:00:00 2001
From: Richard Berger <richard.berger@temple.edu>
Date: Thu, 8 Sep 2016 00:12:09 -0400
Subject: [PATCH] Use factory for integrate style creation

---
 src/info.cpp   |  9 ++++---
 src/update.cpp | 70 ++++++++++++++++++++++++++------------------------
 src/update.h   |  7 +++++
 3 files changed, 48 insertions(+), 38 deletions(-)

diff --git a/src/info.cpp b/src/info.cpp
index 8978f06b73..97b0ac1cf7 100644
--- a/src/info.cpp
+++ b/src/info.cpp
@@ -582,10 +582,11 @@ void Info::integrate_styles(FILE * out)
   fprintf(out, "\nIntegrate styles:\n");
 
   vector<string> styles;
-#define INTEGRATE_CLASS
-#define IntegrateStyle(key,Class) styles.push_back(#key);
-#include "style_integrate.h"
-#undef INTEGRATE_CLASS
+
+  for(Update::IntegrateCreatorMap::iterator it = update->integrate_map->begin(); it != update->integrate_map->end(); ++it) {
+    styles.push_back(it->first);
+  }
+
   print_columns(out, styles);
   fprintf(out, "\n\n\n");
 }
diff --git a/src/update.cpp b/src/update.cpp
index faf3a8c148..2e85798941 100644
--- a/src/update.cpp
+++ b/src/update.cpp
@@ -61,6 +61,15 @@ Update::Update(LAMMPS *lmp) : Pointers(lmp)
   minimize_style = 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";
   create_integrate(1,&str,1);
 
@@ -79,6 +88,8 @@ Update::~Update()
 
   delete [] minimize_style;
   delete minimize;
+
+  delete integrate_map;
 }
 
 /* ---------------------------------------------------------------------- */
@@ -319,52 +330,43 @@ void Update::new_integrate(char *style, int narg, char **arg,
       sflag = 1;
       char estyle[256];
       sprintf(estyle,"%s/%s",style,lmp->suffix);
-      int success = 1;
-
-      if (0) 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 (integrate_map->find(estyle) != integrate_map->end()) {
+        IntegrateCreator integrate_creator = (*integrate_map)[estyle];
+        integrate = integrate_creator(lmp, narg, arg);
+        return;
+      }
     }
 
     if (lmp->suffix2) {
       sflag = 2;
       char estyle[256];
       sprintf(estyle,"%s/%s",style,lmp->suffix2);
-      int success = 1;
-
-      if (0) 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 (integrate_map->find(estyle) != integrate_map->end()) {
+        IntegrateCreator integrate_creator = (*integrate_map)[estyle];
+        integrate = integrate_creator(lmp, narg, arg);
+        return;
+      }
     }
   }
 
   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
-#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
+  error->all(FLERR,"Illegal integrate style");
+}
 
-  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);
 }
 
 /* ---------------------------------------------------------------------- */
diff --git a/src/update.h b/src/update.h
index bbb29bc7ee..3241d64d1f 100644
--- a/src/update.h
+++ b/src/update.h
@@ -15,6 +15,8 @@
 #define LMP_UPDATE_H
 
 #include "pointers.h"
+#include <map>
+#include <string>
 
 namespace LAMMPS_NS {
 
@@ -46,6 +48,10 @@ class Update : protected Pointers {
   class Min *minimize;
   char *minimize_style;
 
+  typedef Integrate *(*IntegrateCreator)(LAMMPS *,int,char**);
+  typedef std::map<std::string,IntegrateCreator> IntegrateCreatorMap;
+  IntegrateCreatorMap *integrate_map;
+
   Update(class LAMMPS *);
   ~Update();
   void init();
@@ -60,6 +66,7 @@ class Update : protected Pointers {
  private:
   void new_integrate(char *, int, char **, int, int &);
 
+  template <typename T> static Integrate *integrate_creator(LAMMPS *, int, char **);
 };
 
 }
-- 
GitLab