Skip to content

Commit

Permalink
checked that the json did not change and that cppcheck passes
Browse files Browse the repository at this point in the history
  • Loading branch information
Iximiel committed Jan 27, 2025
1 parent 4522e86 commit a73ecd1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
57 changes: 30 additions & 27 deletions src/tools/Keywords.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ void erase_remove(std::string& vec, const char& value) {
//few definition to avoid rewriting the too many times the same docstring
#define NUMBERED_DOCSTRING(key) ". You can use multiple instances of this keyword i.e. " \
+ std::string(key) +"1, " + std::string(key) + "2, " + std::string(key) + "3..."
#define ATOM_DOCSTRING ". For more information on how to specify lists of atoms see \\ref Group"


namespace PLMD {
Expand All @@ -49,14 +48,14 @@ std::string toString(Keywords::argType at) {
case Keywords::argType::scalar:
return "scalar";

case Keywords::argType::grid:
return "grid";

case Keywords::argType::vector:
return "vector";

case Keywords::argType::matrix:
return "matrix";

case Keywords::argType::grid:
return "grid";
}
//the not simple cases
{
Expand All @@ -66,16 +65,16 @@ std::string toString(Keywords::argType at) {
ret+="scalar";
next="/";
}
if(valid(at & Keywords::argType::grid)) {
ret+=next+"grid";
next="/";
}
if(valid(at & Keywords::argType::vector)) {
ret+=next+"vector";
next="/";
}
if(valid(at & Keywords::argType::matrix)) {
ret+=next+"matrix";
next="/";
}
if(valid(at & Keywords::argType::grid)) {
ret+=next+"grid";
}
return ret;
}
Expand All @@ -93,15 +92,15 @@ Keywords::argType stoat(std::string_view str) {
if (str == "scalar") {
return Keywords::argType::scalar;
}
if (str == "grid") {
return Keywords::argType::grid;
}
if (str == "vector") {
return Keywords::argType::vector;
}
if (str == "matrix") {
return Keywords::argType::matrix;
}
if (str == "grid") {
return Keywords::argType::grid;
}
// Handle the case where the string does not match any enum value.
plumed_massert(false,"invalid argType specifier " + std::string(str));
}
Expand All @@ -111,15 +110,15 @@ std::string toString(Keywords::componentType at) {
case Keywords::componentType::scalar:
return "scalar";

case Keywords::componentType::grid:
return "grid";

case Keywords::componentType::vector:
return "vector";

case Keywords::componentType::matrix:
return "matrix";

case Keywords::componentType::grid:
return "grid";

case Keywords::componentType::atom:
return "atom";

Expand All @@ -134,10 +133,6 @@ std::string toString(Keywords::componentType at) {
ret+="scalar";
next="/";
}
if(valid(at & Keywords::componentType::grid)) {
ret+=next+"grid";
next="/";
}
if(valid(at & Keywords::componentType::vector)) {
ret+=next+"vector";
next="/";
Expand All @@ -146,9 +141,14 @@ std::string toString(Keywords::componentType at) {
ret+=next+"matrix";
next="/";
}
if(valid(at & Keywords::componentType::grid)) {
ret+=next+"grid";
next="/";
}
//I do not think these two are necessary
if(valid(at & Keywords::componentType::atom)) {
ret+=next+"atom";
next="/";
}
if(valid(at & Keywords::componentType::atoms)) {
ret+=next+"atoms";
Expand Down Expand Up @@ -252,12 +252,14 @@ bool Keywords::keyInfo::isArgument() const {
return std::holds_alternative<argType>(argument_type);
}

Keywords::component::component()=default;
Keywords::component& Keywords::component::setKey(std::string k) {
Keywords::component::component():
//the 0 ensures something that always fails unles explicitly set
type(static_cast<componentType>(0)) {};
Keywords::component& Keywords::component::setKey(std::string_view k) {
key=k;
return *this;
}
Keywords::component& Keywords::component::setDocstring(std::string d) {
Keywords::component& Keywords::component::setDocstring(std::string_view d) {
docstring=d;
return *this;
}
Expand Down Expand Up @@ -343,8 +345,8 @@ void Keywords::addOrReserve( std::string_view keytype,
if( type.isAtomList() ) {
//keytype may be "residues" or something like "atoms-3"
keywords.find(key)->second.atomtag=keytype;
if (isaction) {
fd += ATOM_DOCSTRING;
if (isaction && keytype == "atoms") { //this narrow the doctrstring ONLY to atoms
keywords.find(key)->second.docstring+= ". For more information on how to specify lists of atoms see \\ref Group";
}
}
if (reserve) {
Expand Down Expand Up @@ -809,13 +811,13 @@ std::string Keywords::getHelpString() const {
}
}
}
unsigned ncompulsory=0;
for(const auto& key : keys) {
if ( keywords.at(key).type.isCompulsory() ) {
nkeys++;
ncompulsory++;
}
}
unsigned ncompulsory=nkeys;
if( nkeys>0 ) {
if( ncompulsory>0 ) {
helpstr += "\nThe following arguments are compulsory: \n\n";
for(const auto& key : keys) {
if ( keywords.at(key).type.isCompulsory() ) {
Expand Down Expand Up @@ -1054,7 +1056,7 @@ std::vector<std::string> Keywords::getArgumentKeys() const {
bool Keywords::checkArgumentType( const std::size_t& rank, const bool& hasderiv ) const {
std::map <std::string,bool> arguments;
for(auto const& kw : getArgumentKeys() ) {
auto & at = std::get<argType>(keywords.at(kw).argument_type);
const auto & at = std::get<argType>(keywords.at(kw).argument_type);
arguments[kw] = false;
if( rank==0 && valid(at | argType::scalar)) {
arguments[kw] = true;
Expand Down Expand Up @@ -1086,6 +1088,7 @@ bool Keywords::checkArgumentType( const std::size_t& rank, const bool& hasderiv
" ("+toString(std::get<argType>(keywords.at(arg.first).argument_type))+")" +"\n";
}
}
//the merror makes the return never executed!!!
plumed_merror(errorMessage);
return false;
}
Expand Down
19 changes: 10 additions & 9 deletions src/tools/Keywords.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class Log;
class Keywords {
/// This class lets me pass keyword types easily
struct KeyType {
enum class keyStyle {hidden,compulsory,flag,optional,atoms,vessel,unknown} style;
enum class keyStyle {hidden,compulsory,flag,optional,atoms,vessel,unknown};
keyStyle style;
static keyStyle keyStyleFromString(std::string_view type );
explicit KeyType( keyStyle type );
explicit KeyType( std::string_view type );
Expand Down Expand Up @@ -84,19 +85,19 @@ class Keywords {
};

public:
enum class argType {scalar=1,grid=1<<2,vector=1<<3,matrix=1<<4};
enum class componentType {scalar=1,grid=1<<2,vector=1<<3,matrix=1<<4,atoms=1<<5,atom=1<<6};
enum class argType {scalar=1,vector=1<<2,matrix=1<<3,grid=1<<4};
enum class componentType {scalar=1,vector=1<<2,matrix=1<<3,grid=1<<4,atoms=1<<5,atom=1<<6};
private:
/// Is this an action or driver (this bool affects what style==atoms does in print)
bool isaction;
bool isaction=true;
/// This allows us to overwrite the behavior of the atoms type in analysis actions
bool isatoms;
bool isatoms=true;
/// The name of the action that has this set of keywords
std::string thisactname;

struct keyInfo {
/// Whether the keyword is compulsory, optional...
KeyType type;
KeyType type{KeyType::keyStyle::unknown};
/// The documentation for the keyword
std::string docstring;
/// The default values (if there are default values) for compulsory keywords or flags
Expand Down Expand Up @@ -135,8 +136,8 @@ class Keywords {
componentType type;
component();
//these functions are not neeeded (this is a struct), but are useful in constructing the component
component& setKey(std::string k);
component& setDocstring(std::string d);
component& setKey(std::string_view k);
component& setDocstring(std::string_view d);
component& setType(componentType t);
};
//the "exists component" is stored in the map keys
Expand All @@ -153,7 +154,7 @@ class Keywords {
void print_html_item( const std::string& ) const;
public:
/// Constructor
Keywords() : isaction(true), isatoms(true) {}
Keywords() {}
///
void isDriver() {
isaction=false;
Expand Down

0 comments on commit a73ecd1

Please sign in to comment.