18#include "colvarvalue.h"
23#define COLVARSCRIPT_ERROR -1
24#define COLVARSCRIPT_OK 0
26#define COLVARSCRIPT_MAGIC 0x5349474E
34 uint32_t
const magic = COLVARSCRIPT_MAGIC;
39 if (!script_ptr)
return false;
41 return script_ptr->
magic == COLVARSCRIPT_MAGIC;
63 int run(
int objc,
unsigned char *
const objv[]);
88#define CVSCRIPT_ENUM_COMM(COMM) COMM,
90#define CVSCRIPT(COMM,HELP,N_ARGS_MIN,N_ARGS_MAX,ARGS,FN_BODY) \
91 CVSCRIPT_ENUM_COMM(COMM)
92#ifdef COLVARSCRIPT_COMMANDS_H
93#undef COLVARSCRIPT_COMMANDS_H
95#include "colvarscript_commands.h"
96#undef COLVARSCRIPT_COMMANDS_H
98#undef CVSCRIPT_ENUM_COMM
113 template<Object_type T>
114 unsigned char *
get_cmd_arg(
int iarg,
int objc,
unsigned char *
const objv[]);
118 unsigned char *
const objv[]);
122 unsigned char *
const objv[]);
126 unsigned char *
const objv[]);
129 template<Object_type T>
131 int n_args_min,
int n_args_max);
135 int n_args_min,
int n_args_max);
139 int n_args_min,
int n_args_max);
143 int n_args_min,
int n_args_max);
146 template<colvarscript::Object_type T>
207 return this->cvmodule;
240 unsigned char *obj = NULL);
246 template <
typename T>
248 unsigned char *obj = NULL);
254 template <
typename T>
256 unsigned char *obj = NULL);
263 unsigned char *obj = NULL);
272 char const *name,
char const *help,
273 int n_args_min,
int n_args_max,
char const *arghelp,
274 int (*fn)(
void *,
int,
unsigned char *
const *));
280 int argc,
unsigned char *
const argv[]);
312 std::vector<int (*)(
void *, int,
unsigned char *
const *)>
cmd_fns;
317 unsigned char *
const *)
326 template <
typename T>
330 template <
typename T>
353inline static colvar *colvar_obj(
void *pobj)
355 return reinterpret_cast<colvar *
>(pobj);
360inline static colvarbias *colvarbias_obj(
void *pobj)
367template<colvarscript::Object_type T>
370 unsigned char *
const objv[])
372 int const shift = cmd_arg_shift<T>();
373 return (shift+iarg < objc) ? objv[shift+iarg] : NULL;
378 unsigned char *
const objv[])
380 return get_cmd_arg<use_module>(iarg, objc, objv);
385 unsigned char *
const objv[])
387 return get_cmd_arg<use_colvar>(iarg, objc, objv);
392 unsigned char *
const objv[])
394 return get_cmd_arg<use_bias>(iarg, objc, objv);
398template<colvarscript::Object_type T>
404 int const shift = cmd_arg_shift<T>();
405 if (objc < shift+n_args_min) {
407 ") for script function \""+std::string(cmd)+
409 return COLVARSCRIPT_ERROR;
411 if (objc > shift+n_args_max) {
413 ") for script function \""+std::string(cmd)+
415 return COLVARSCRIPT_ERROR;
417 return COLVARSCRIPT_OK;
426 return check_cmd_nargs<use_module>(cmd, objc, n_args_min, n_args_max);
435 return check_cmd_nargs<use_colvar>(cmd, objc, n_args_min, n_args_max);
444 return check_cmd_nargs<use_bias>(cmd, objc, n_args_min, n_args_max);
448template<colvarscript::Object_type T>
452 if (T == use_module) {
455 }
else if (T == use_colvar) {
458 }
else if (T == use_bias) {
469 int run_colvarscript_command(
colvarscript *
script,
int objc,
unsigned char *
const objv[]);
472 const char * get_colvarscript_result();
A collective variable (main class); to be defined, it needs at least one object of a derived class of...
Definition: colvar.h:53
Collective variable bias, base class.
Definition: colvarbias.h:23
Parent class for a member object of a bias, cv or cvc etc. containing features and their dependencies...
Definition: colvardeps.h:34
vector of real numbers with three components
Definition: colvartypes.h:728
Collective variables module (main class)
Definition: colvarmodule.h:72
double real
Defining an abstract real number allows to switch precision.
Definition: colvarmodule.h:99
static colvarmodule * main()
Access the main instance of the Colvars module.
Definition: colvarmodule.cpp:205
static std::string to_str(char const *s)
Convert to string for output purposes.
Definition: colvarmodule.cpp:2541
colvarproxy * proxy
Pointer to the proxy object, used to retrieve atomic data from the hosting program.
Definition: colvarmodule.h:957
colvarscript * script
Definition: colvarproxy.h:532
Definition: colvarproxy.h:564
Definition: colvarscript.h:30
int clear_str_result()
Clear the string result.
Definition: colvarscript.cpp:611
int set_result_long_int(long int const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:921
int set_result_real(cvm::real const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:931
char const * get_command_arghelp(char const *cmd, int i)
Definition: colvarscript.cpp:212
int proc_features(colvardeps *obj, int argc, unsigned char *const argv[])
Run subcommands on base colvardeps object (colvar, bias, ...)
Definition: colvarscript.cpp:511
void add_error_msg(std::string const &s)
Add the given string to the error message of the script interface.
Definition: colvarscript.cpp:601
int run(int objc, unsigned char *const objv[])
Run a script command with space-separated positional arguments (objects)
Definition: colvarscript.cpp:348
int set_result_str(std::string const &s)
Set the return value to the given string.
Definition: colvarscript.cpp:589
std::string & modify_str_result()
Modify the string result of the current scripting call.
Definition: colvarscript.h:72
char const * get_command_rethelp(char const *cmd)
Definition: colvarscript.cpp:200
std::string const & str_result() const
Get the string result of the current scripting call.
Definition: colvarscript.h:66
int set_result_long_int_vec(std::vector< long int > const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:925
void set_cmdline_main_cmd(std::string const &cmd)
Set the main command for the CLI, when it is not "cv" (e.g. LAMMPS)
Definition: colvarscript.h:179
int set_result_text(T const &x, unsigned char *obj)
Set obj equal to x, using its string representation.
Definition: colvarscript.cpp:797
command
Commands available.
Definition: colvarscript.h:87
std::vector< std::string > obj_to_str_vector(unsigned char *obj)
Get a list of strings from an object (does not work with a simple cast)
Definition: colvarscript.cpp:468
uint32_t const magic
This magic number is used to validate colvarscript pointers.
Definition: colvarscript.h:34
int set_result_colvarvalue(colvarvalue const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:965
int unsupported_op()
Set error code for unsupported script operation.
Definition: colvarscript.cpp:582
std::map< std::string, command > cmd_str_map
Internal identifiers of command strings.
Definition: colvarscript.h:285
int set_result_text_from_str(std::string const &x_str, unsigned char *obj)
Code reused by all instances of set_result_text()
Definition: colvarscript.cpp:784
int set_result_real_vec(T const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
int set_result_int(int const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:911
int check_module_cmd_nargs(char const *cmd, int objc, int n_args_min, int n_args_max)
Instantiation of check_cmd_nargs<> for module-level commands.
Definition: colvarscript.h:421
char * obj_to_str(unsigned char *obj)
Get the string representation of an object (by default, a simple cast)
Definition: colvarscript.cpp:457
int set_result_rvector_vec(T const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
static bool is_valid(const colvarscript *script_ptr)
Definition: colvarscript.h:38
char const * get_command_help(char const *cmd)
Definition: colvarscript.cpp:188
int init_command(colvarscript::command const &comm, char const *name, char const *help, int n_args_min, int n_args_max, char const *arghelp, int(*fn)(void *, int, unsigned char *const *))
Set up a single script API function.
Definition: colvarscript.cpp:105
int check_colvar_cmd_nargs(char const *cmd, int objc, int n_args_min, int n_args_max)
Instantiation of check_cmd_nargs<> for colvar-level commands.
Definition: colvarscript.h:430
int cmd_arg_shift()
Number of positional arguments to shift for each object type.
Definition: colvarscript.h:449
std::vector< size_t > cmd_n_args_max
Maximum number of arguments for each command.
Definition: colvarscript.h:303
char const * get_command_full_help(char const *cmd)
Definition: colvarscript.cpp:248
int get_command_n_args_min(char const *cmd)
Definition: colvarscript.cpp:224
unsigned char * get_colvar_cmd_arg(int iarg, int objc, unsigned char *const objv[])
Instantiation of get_cmd_arg<> for colvar-level commands.
Definition: colvarscript.h:384
int pack_vector_elements_text(T const &x, std::string &x_str)
Code reused by instances of set_result_text()
Definition: colvarscript.cpp:804
colvarmodule * module()
Pointer to the Colvars main object.
Definition: colvarscript.h:205
std::vector< std::string > cmd_rethelp
Description of the return values of each command (may be empty)
Definition: colvarscript.h:297
int check_bias_cmd_nargs(char const *cmd, int objc, int n_args_min, int n_args_max)
Instantiation of get_cmd_arg<> for bias-level commands.
Definition: colvarscript.h:439
char const ** get_command_names() const
Get names of all commands.
Definition: colvarscript.h:150
std::vector< size_t > cmd_n_args_min
Minimum number of arguments for each command.
Definition: colvarscript.h:300
unsigned char * get_cmd_arg(int iarg, int objc, unsigned char *const objv[])
Get a pointer to the i-th argument of the command (NULL if not given)
Definition: colvarscript.h:368
char const ** cmd_names
Inverse of cmd_str_map (to be exported outside this class)
Definition: colvarscript.h:291
int set_result_colvarvalue_vec(std::vector< colvarvalue > const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:970
int set_result_rvector(cvm::rvector const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:941
unsigned char * get_bias_cmd_arg(int iarg, int objc, unsigned char *const objv[])
Instantiation of get_cmd_arg<> for bias-level commands.
Definition: colvarscript.h:391
Object_type
Type of object handling a script command.
Definition: colvarscript.h:103
int get_command_n_args_max(char const *cmd)
Definition: colvarscript.cpp:236
colvarproxy * proxy()
Pointer to the colvarproxy object (interface with host engine)
Definition: colvarscript.h:211
unsigned char * get_module_cmd_arg(int iarg, int objc, unsigned char *const objv[])
Instantiation of get_cmd_arg<> for module-level commands.
Definition: colvarscript.h:377
int set_result_int_vec(std::vector< int > const &x, unsigned char *obj=NULL)
Copy x into obj if not NULL, or into the script object's result otherwise.
Definition: colvarscript.cpp:915
int init_commands()
Set up all script API functions.
Definition: colvarscript.cpp:66
std::string get_command_cmdline_syntax(Object_type t, command c)
Definition: colvarscript.cpp:260
std::vector< std::vector< std::string > > cmd_arghelp
Help strings for each command argument.
Definition: colvarscript.h:306
int(*)(void *, int, unsigned char *const *) get_cmd_fn(std::string const &cmd_key)
Get a pointer to the implementation of the given command.
Definition: colvarscript.h:315
std::string cmdline_main_cmd_
Main command used in command line ("cv" by default)
Definition: colvarscript.h:288
std::string get_cmdline_help_summary(Object_type t)
Definition: colvarscript.cpp:302
int check_cmd_nargs(char const *cmd, int objc, int n_args_min, int n_args_max)
Check the argument count of the command.
Definition: colvarscript.h:399
std::vector< std::string > cmd_help
Help strings for each command.
Definition: colvarscript.h:294
std::string get_cmd_prefix(Object_type t)
Return the prefix of the individual command for each object function.
Definition: colvarscript.cpp:171
std::string get_command_cmdline_help(Object_type t, std::string const &cmd)
Definition: colvarscript.cpp:334
std::vector< std::string > cmd_full_help
Full help strings for each command.
Definition: colvarscript.h:309
std::string str_result_
String representation of the result of a script call.
Definition: colvarscript.h:60
std::vector< int(*)(void *, int, unsigned char *const *)> cmd_fns
Implementations of each command.
Definition: colvarscript.h:312
Value of a collective variable: this is a metatype which can be set at runtime. By default it is set ...
Definition: colvarvalue.h:43
Collective variables main module.