Commit a1e5fc0f authored by Richard Berger's avatar Richard Berger
Browse files

Use factory for AtomVec style creation

parent 88e10b40
Loading
Loading
Loading
Loading
+33 −26
Original line number Diff line number Diff line
@@ -210,6 +210,15 @@ Atom::Atom(LAMMPS *lmp) : Pointers(lmp)

  datamask = ALL_MASK;
  datamask_ext = ALL_MASK;

  avec_map = new AtomVecCreatorMap();

#define ATOM_CLASS
#define AtomStyle(key,Class) \
  (*avec_map)[#key] = &avec_creator<Class>;
#include "style_atom.h"
#undef AtomStyle
#undef ATOM_CLASS
}

/* ---------------------------------------------------------------------- */
@@ -218,6 +227,7 @@ Atom::~Atom()
{
  delete [] atom_style;
  delete avec;
  delete avec_map;

  delete [] firstgroupname;
  memory->destroy(binhead);
@@ -445,46 +455,43 @@ AtomVec *Atom::new_avec(const char *style, int trysuffix, int &sflag)
      sflag = 1;
      char estyle[256];
      sprintf(estyle,"%s/%s",style,lmp->suffix);

      if (0) return NULL;

#define ATOM_CLASS
#define AtomStyle(key,Class) \
      else if (strcmp(estyle,#key) == 0) return new Class(lmp);
#include "style_atom.h"
#undef AtomStyle
#undef ATOM_CLASS
      if (avec_map->find(estyle) != avec_map->end()) {
        AtomVecCreator avec_creator = (*avec_map)[estyle];
        return avec_creator(lmp);
      }
    }

    if (lmp->suffix2) {
      sflag = 2;
      char estyle[256];
      sprintf(estyle,"%s/%s",style,lmp->suffix2);

      if (0) return NULL;

#define ATOM_CLASS
#define AtomStyle(key,Class) \
      else if (strcmp(estyle,#key) == 0) return new Class(lmp);
#include "style_atom.h"
#undef AtomStyle
#undef ATOM_CLASS
      if (avec_map->find(estyle) != avec_map->end()) {
        AtomVecCreator avec_creator = (*avec_map)[estyle];
        return avec_creator(lmp);
      }
    }
  }

  sflag = 0;
  if (0) return NULL;

#define ATOM_CLASS
#define AtomStyle(key,Class) \
  else if (strcmp(style,#key) == 0) return new Class(lmp);
#include "style_atom.h"
#undef ATOM_CLASS
  if (avec_map->find(style) != avec_map->end()) {
    AtomVecCreator avec_creator = (*avec_map)[style];
    return avec_creator(lmp);
  }

  else error->all(FLERR,"Unknown atom style");
  error->all(FLERR,"Unknown atom style");
  return NULL;
}

/* ----------------------------------------------------------------------
   one instance per AtomVec style in style_atom.h
------------------------------------------------------------------------- */

template <typename T>
AtomVec *Atom::avec_creator(LAMMPS *lmp)
{
  return new T(lmp);
}

/* ---------------------------------------------------------------------- */

void Atom::init()
+11 −0
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
#define LMP_ATOM_H

#include "pointers.h"
#include <map>
#include <string>

namespace LAMMPS_NS {

@@ -197,6 +199,12 @@ class Atom : protected Pointers {

  int *sametag;      // sametag[I] = next atom with same ID, -1 if no more

  // AtomVec factory types and map

  typedef AtomVec *(*AtomVecCreator)(LAMMPS *);
  typedef std::map<std::string,AtomVecCreator> AtomVecCreatorMap;
  AtomVecCreatorMap *avec_map;

  // functions

  Atom(class LAMMPS *);
@@ -322,6 +330,9 @@ class Atom : protected Pointers {

  void setup_sort_bins();
  int next_prime(int);

 private:
  template <typename T> static AtomVec *avec_creator(LAMMPS *);
};

}
+5 −4
Original line number Diff line number Diff line
@@ -568,10 +568,11 @@ void Info::atom_styles(FILE * out)
  fprintf(out, "\nAtom styles:\n");

  vector<string> styles;
#define ATOM_CLASS
#define AtomStyle(key,Class) styles.push_back(#key);
#include "style_atom.h"
#undef ATOM_CLASS

  for(Atom::AtomVecCreatorMap::iterator it = atom->avec_map->begin(); it != atom->avec_map->end(); ++it) {
    styles.push_back(it->first);
  }

  print_columns(out, styles);
  fprintf(out, "\n\n\n");
}