Commit 85c13294 authored by Richard Berger's avatar Richard Berger
Browse files

Use factory for integrate style creation

parent 55260ad5
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -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");
}
+36 −34
Original line number Diff line number Diff line
@@ -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);
}

/* ---------------------------------------------------------------------- */
+7 −0
Original line number Diff line number Diff line
@@ -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 **);
};

}