take advantage of having the common Command base class to unify code paths

This commit is contained in:
Axel Kohlmeyer
2021-04-14 07:05:00 -04:00
parent 81578d9934
commit 75579fc100
47 changed files with 83 additions and 91 deletions

View File

@ -59,31 +59,25 @@ Members of ``lammpsplugin_t``
* - author
- String with the name and email of the author
* - creator.v1
- Pointer to factory function for pair, bond, angle, dihedral, or improper styles
- Pointer to factory function for pair, bond, angle, dihedral, improper or command styles
* - creator.v2
- Pointer to factory function for compute, fix, or region styles
* - creator.v3
- Pointer to factory function for command styles
* - handle
- Pointer to the open DSO file handle
Only one of the three alternate creator entries can be used at a time
and which of those is determined by the style of plugin. The "creator.v1"
element is for factory functions of supported styles computing forces (i.e.
pair, bond, angle, dihedral, or improper styles) and the function takes
as single argument the pointer to the LAMMPS instance. The factory function
is cast to the ``lammpsplugin_factory1`` type before assignment. The
"creator.v2" element is for factory functions creating an instance of
a fix, compute, or region style and takes three arguments: a pointer to
the LAMMPS instance, an integer with the length of the argument list and
a ``char **`` pointer to the list of arguments. The factory function pointer
needs to be cast to the ``lammpsplugin_factory2`` type before assignment.
The "creator.v3" element takes the same arguments as "creator.v3" but is
specific to creating command styles: the factory function has to instantiate
the command style locally passing the LAMMPS pointer as argument and then
call its "command" member function with the number and list of arguments.
The factory function pointer needs to be cast to the
``lammpsplugin_factory3`` type before assignment.
and which of those is determined by the style of plugin. The
"creator.v1" element is for factory functions of supported styles
computing forces (i.e. command, pair, bond, angle, dihedral, or
improper styles) and the function takes as single argument the pointer
to the LAMMPS instance. The factory function is cast to the
``lammpsplugin_factory1`` type before assignment. The "creator.v2"
element is for factory functions creating an instance of a fix, compute,
or region style and takes three arguments: a pointer to the LAMMPS
instance, an integer with the length of the argument list and a ``char
**`` pointer to the list of arguments. The factory function pointer
needs to be cast to the ``lammpsplugin_factory2`` type before
assignment.
Pair style example
^^^^^^^^^^^^^^^^^^
@ -123,12 +117,12 @@ function would look like this:
The factory function in this example is called ``morse2creator()``. It
receives a pointer to the LAMMPS class as only argument and thus has to
be assigned to the *creator.v1* member of the plugin struct and cast to the
``lammpsplugin_factory1`` pointer type. It returns a
be assigned to the *creator.v1* member of the plugin struct and cast to
the ``lammpsplugin_factory1`` function pointer type. It returns a
pointer to the allocated class instance derived from the ``Pair`` class.
This function may be declared static to avoid clashes with other plugins.
The name of the derived class, ``PairMorse2``, must be unique inside
the entire LAMMPS executable.
This function may be declared static to avoid clashes with other
plugins. The name of the derived class, ``PairMorse2``, however must be
unique inside the entire LAMMPS executable.
Fix style example
^^^^^^^^^^^^^^^^^
@ -169,9 +163,9 @@ Below is an example for that:
Command style example
^^^^^^^^^^^^^^^^^^^^^
For command styles there is a third variant of factory function as
Command styles also use the first variant of factory function as
demonstrated in the following example, which also shows that the
implementation of the plugin class may also be within the same
implementation of the plugin class may be within the same source
file as the plugin interface code:
.. code-block:: C++
@ -180,15 +174,15 @@ file as the plugin interface code:
#include "comm.h"
#include "error.h"
#include "pointers.h"
#include "command.h"
#include "version.h"
#include <cstring>
namespace LAMMPS_NS {
class Hello : protected Pointers {
class Hello : public Command {
public:
Hello(class LAMMPS *lmp) : Pointers(lmp) {};
Hello(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);
};
}
@ -202,10 +196,9 @@ file as the plugin interface code:
utils::logmesg(lmp,fmt::format("Hello, {}!\n",argv[0]));
}
static void hellocreator(LAMMPS *lmp, int argc, char **argv)
static void hellocreator(LAMMPS *lmp)
{
Hello hello(lmp);
hello.command(argc,argv);
return new Hello(lmp);
}
extern "C" void lammpsplugin_init(void *lmp, void *handle, void *regfunc)
@ -216,9 +209,9 @@ file as the plugin interface code:
plugin.version = LAMMPS_VERSION;
plugin.style = "command";
plugin.name = "hello";
plugin.info = "Hello world command v1.0";
plugin.info = "Hello world command v1.1";
plugin.author = "Axel Kohlmeyer (akohlmey@gmail.com)";
plugin.creator.v3 = (lammpsplugin_factory3 *) &hellocreator;
plugin.creator.v1 = (lammpsplugin_factory1 *) &hellocreator;
plugin.handle = handle;
(*register_plugin)(&plugin,lmp);
}

View File

@ -9,7 +9,7 @@
#include <cstring>
namespace LAMMPS_NS {
class Hello : protected Command {
class Hello : public Command {
public:
Hello(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);
@ -25,10 +25,9 @@ void Hello::command(int argc, char **argv)
utils::logmesg(lmp,fmt::format("Hello, {}!\n",argv[0]));
}
static void hellocreator(LAMMPS *lmp, int argc, char **argv)
static Command *hellocreator(LAMMPS *lmp)
{
Hello hello(lmp);
hello.command(argc,argv);
return new Hello(lmp);
}
extern "C" void lammpsplugin_init(void *lmp, void *handle, void *regfunc)
@ -39,9 +38,9 @@ extern "C" void lammpsplugin_init(void *lmp, void *handle, void *regfunc)
plugin.version = LAMMPS_VERSION;
plugin.style = "command";
plugin.name = "hello";
plugin.info = "Hello world command v1.0";
plugin.info = "Hello world command v1.1";
plugin.author = "Axel Kohlmeyer (akohlmey@gmail.com)";
plugin.creator.v3 = (lammpsplugin_factory3 *) &hellocreator;
plugin.creator.v1 = (lammpsplugin_factory1 *) &hellocreator;
plugin.handle = handle;
(*register_plugin)(&plugin,lmp);
}

View File

@ -66,7 +66,7 @@ CommandStyle(kim,KimCommand)
namespace LAMMPS_NS {
class KimCommand : protected Command {
class KimCommand : public Command {
public:
KimCommand(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(message,Message)
namespace LAMMPS_NS {
class Message : protected Command {
class Message : public Command {
public:
Message(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(server,Server)
namespace LAMMPS_NS {
class Server : protected Command {
class Server : public Command {
public:
Server(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);

View File

@ -257,7 +257,7 @@ namespace LAMMPS_NS
"style {} from plugin",
plugin->name));
}
(*command_map)[plugin->name] = (Input::CommandCreator)plugin->creator.v3;
(*command_map)[plugin->name] = (Input::CommandCreator)plugin->creator.v1;
} else {
utils::logmesg(lmp,fmt::format("Loading plugin for {} styles not "

View File

@ -26,7 +26,7 @@ CommandStyle(plugin,Plugin)
namespace LAMMPS_NS
{
class Plugin : protected Command {
class Plugin : public Command {
public:
Plugin(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(hyper,Hyper)
namespace LAMMPS_NS {
class Hyper : protected Command {
class Hyper : public Command {
public:
Hyper(class LAMMPS *);
~Hyper() {}

View File

@ -24,7 +24,7 @@ CommandStyle(neb,NEB)
namespace LAMMPS_NS {
class NEB : protected Command {
class NEB : public Command {
public:
NEB(class LAMMPS *);
NEB(class LAMMPS *, double, double, int, int, int, double *, double *);

View File

@ -24,7 +24,7 @@ CommandStyle(prd,PRD)
namespace LAMMPS_NS {
class PRD : protected Command {
class PRD : public Command {
public:
PRD(class LAMMPS *);
~PRD() {}

View File

@ -24,7 +24,7 @@ CommandStyle(tad,TAD)
namespace LAMMPS_NS {
class TAD : protected Command {
class TAD : public Command {
public:
TAD(class LAMMPS *);
~TAD();

View File

@ -24,7 +24,7 @@ CommandStyle(temper,Temper)
namespace LAMMPS_NS {
class Temper : protected Command {
class Temper : public Command {
public:
Temper(class LAMMPS *);
~Temper();

View File

@ -24,7 +24,7 @@ CommandStyle(neb/spin,NEBSpin)
namespace LAMMPS_NS {
class NEBSpin : protected Command {
class NEBSpin : public Command {
public:
NEBSpin(class LAMMPS *);
~NEBSpin();

View File

@ -26,7 +26,7 @@ CommandStyle(group2ndx,Group2Ndx)
namespace LAMMPS_NS {
class Group2Ndx : protected Command {
class Group2Ndx : public Command {
public:
Group2Ndx(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);

View File

@ -27,7 +27,7 @@ CommandStyle(ndx2group,Ndx2Group)
namespace LAMMPS_NS {
class Ndx2Group : protected Command {
class Ndx2Group : public Command {
public:
Ndx2Group(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(temper/grem,TemperGrem)
namespace LAMMPS_NS {
class TemperGrem : protected Command {
class TemperGrem : public Command {
public:
TemperGrem(class LAMMPS *);
~TemperGrem();

View File

@ -25,7 +25,7 @@ CommandStyle(temper/npt,TemperNPT)
namespace LAMMPS_NS {
class TemperNPT : protected Command {
class TemperNPT : public Command {
public:
TemperNPT(class LAMMPS *);
~TemperNPT();

View File

@ -15,7 +15,7 @@ CommandStyle(dynamical_matrix,DynamicalMatrix)
namespace LAMMPS_NS {
class DynamicalMatrix : protected Command {
class DynamicalMatrix : public Command {
public:
DynamicalMatrix(class LAMMPS *);
virtual ~DynamicalMatrix();

View File

@ -16,7 +16,7 @@ CommandStyle(third_order,ThirdOrder)
namespace LAMMPS_NS {
class ThirdOrder : protected Command {
class ThirdOrder : public Command {
public:
ThirdOrder(class LAMMPS *);
virtual ~ThirdOrder();

View File

@ -24,7 +24,7 @@ CommandStyle(balance,Balance)
namespace LAMMPS_NS {
class Balance : protected Command {
class Balance : public Command {
public:
class RCB *rcb;
class FixStore *fixstore; // per-atom weights stored in FixStore

View File

@ -24,7 +24,7 @@ CommandStyle(change_box,ChangeBox)
namespace LAMMPS_NS {
class ChangeBox : protected Command {
class ChangeBox : public Command {
public:
ChangeBox(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(create_atoms,CreateAtoms)
namespace LAMMPS_NS {
class CreateAtoms : protected Command {
class CreateAtoms : public Command {
public:
CreateAtoms(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(create_bonds,CreateBonds)
namespace LAMMPS_NS {
class CreateBonds : protected Command {
class CreateBonds : public Command {
public:
CreateBonds(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(create_box,CreateBox)
namespace LAMMPS_NS {
class CreateBox : protected Command {
class CreateBox : public Command {
public:
CreateBox(class LAMMPS *);
void command(int, char **);

View File

@ -25,7 +25,7 @@ CommandStyle(delete_atoms,DeleteAtoms)
namespace LAMMPS_NS {
class DeleteAtoms : protected Command {
class DeleteAtoms : public Command {
public:
DeleteAtoms(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(delete_bonds,DeleteBonds)
namespace LAMMPS_NS {
class DeleteBonds : protected Command {
class DeleteBonds : public Command {
public:
DeleteBonds(class LAMMPS *);
void command(int, char **);

View File

@ -30,7 +30,7 @@ CommandStyle(kim_query,Deprecated)
namespace LAMMPS_NS {
class Deprecated : protected Command {
class Deprecated : public Command {
public:
Deprecated(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(displace_atoms,DisplaceAtoms)
namespace LAMMPS_NS {
class DisplaceAtoms : protected Command {
class DisplaceAtoms : public Command {
public:
DisplaceAtoms(class LAMMPS *);
~DisplaceAtoms();

View File

@ -26,7 +26,7 @@ CommandStyle(info,Info)
namespace LAMMPS_NS {
class Info : protected Command {
class Info : public Command {
public:
Info(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);

View File

@ -21,6 +21,7 @@
#include "comm.h"
#include "comm_brick.h"
#include "comm_tiled.h"
#include "command.h"
#include "compute.h"
#include "dihedral.h"
#include "domain.h"
@ -82,7 +83,7 @@ command line flags, holds the factory of commands and creates and
initializes an instance of the Variable class.
To execute a command, a specific class instance, derived from
:cpp:class:`Pointers`, is created, then its ``command()`` member
:cpp:class:`Command`, is created, then its ``command()`` member
function executed, and finally the class instance is deleted.
\endverbatim
@ -789,7 +790,8 @@ int Input::execute_command()
if (command_map->find(command) != command_map->end()) {
CommandCreator &command_creator = (*command_map)[command];
command_creator(lmp,narg,arg);
Command *cmd = command_creator(lmp);
cmd->command(narg,arg);
return 0;
}
@ -803,10 +805,9 @@ int Input::execute_command()
------------------------------------------------------------------------- */
template <typename T>
void Input::command_creator(LAMMPS *lmp, int narg, char **arg)
Command *Input::command_creator(LAMMPS *lmp)
{
T cmd(lmp);
cmd.command(narg,arg);
return new T(lmp);
}
/* ---------------------------------------------------------------------- */

View File

@ -19,6 +19,7 @@
#include <map>
namespace LAMMPS_NS {
class Command;
class Input : protected Pointers {
friend class Info;
@ -59,12 +60,12 @@ class Input : protected Pointers {
FILE **infiles; // list of open input files
public:
typedef void (*CommandCreator)(LAMMPS *, int, char **);
typedef Command * (*CommandCreator)(LAMMPS *);
typedef std::map<std::string,CommandCreator> CommandCreatorMap;
CommandCreatorMap *command_map;
protected:
template <typename T> static void command_creator(LAMMPS *, int, char **);
template <typename T> static Command *command_creator(LAMMPS *);
private:
void parse(); // parse an input text line

View File

@ -20,7 +20,6 @@ extern "C" {
typedef void *(lammpsplugin_factory1)(void *);
typedef void *(lammpsplugin_factory2)(void *, int, char **);
typedef void (lammpsplugin_factory3)(void *, int, char **);
typedef struct {
const char *version;
@ -31,7 +30,6 @@ extern "C" {
union {
lammpsplugin_factory1 *v1;
lammpsplugin_factory2 *v2;
lammpsplugin_factory3 *v3;
} creator;
void *handle;
} lammpsplugin_t;

View File

@ -24,7 +24,7 @@ CommandStyle(minimize,Minimize)
namespace LAMMPS_NS {
class Minimize : protected Command {
class Minimize : public Command {
public:
Minimize(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(read_data,ReadData)
namespace LAMMPS_NS {
class ReadData : protected Command {
class ReadData : public Command {
public:
ReadData(class LAMMPS *);
~ReadData();

View File

@ -26,7 +26,7 @@ CommandStyle(read_dump,ReadDump)
namespace LAMMPS_NS {
class ReadDump : protected Command {
class ReadDump : public Command {
public:
ReadDump(class LAMMPS *);
~ReadDump();

View File

@ -24,7 +24,7 @@ CommandStyle(read_restart,ReadRestart)
namespace LAMMPS_NS {
class ReadRestart : protected Command {
class ReadRestart : public Command {
public:
ReadRestart(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(replicate,Replicate)
namespace LAMMPS_NS {
class Replicate : protected Command {
class Replicate : public Command {
public:
Replicate(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(rerun,Rerun)
namespace LAMMPS_NS {
class Rerun : protected Command {
class Rerun : public Command {
public:
Rerun(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(reset_atom_ids,ResetIDs)
namespace LAMMPS_NS {
class ResetIDs : protected Command {
class ResetIDs : public Command {
public:
struct AtomRvous {
bigint ibin;

View File

@ -24,7 +24,7 @@ CommandStyle(reset_mol_ids,ResetMolIDs)
namespace LAMMPS_NS {
class ResetMolIDs : protected Command {
class ResetMolIDs : public Command {
public:
ResetMolIDs(class LAMMPS *);
~ResetMolIDs();

View File

@ -24,7 +24,7 @@ CommandStyle(run,Run)
namespace LAMMPS_NS {
class Run : protected Command {
class Run : public Command {
public:
Run(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(set,Set)
namespace LAMMPS_NS {
class Set : protected Command {
class Set : public Command {
public:
Set(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(velocity,Velocity)
namespace LAMMPS_NS {
class Velocity : protected Command {
class Velocity : public Command {
public:
Velocity(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(write_coeff,WriteCoeff)
namespace LAMMPS_NS {
class WriteCoeff : protected Command {
class WriteCoeff : public Command {
public:
WriteCoeff(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(write_data,WriteData)
namespace LAMMPS_NS {
class WriteData : protected Command {
class WriteData : public Command {
public:
WriteData(class LAMMPS *);
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(write_dump,WriteDump)
namespace LAMMPS_NS {
class WriteDump : protected Command {
class WriteDump : public Command {
public:
WriteDump(class LAMMPS *lmp) : Command(lmp) {};
void command(int, char **);

View File

@ -24,7 +24,7 @@ CommandStyle(write_restart,WriteRestart)
namespace LAMMPS_NS {
class WriteRestart : protected Command {
class WriteRestart : public Command {
public:
WriteRestart(class LAMMPS *);
void command(int, char **);