Skip to content
Snippets Groups Projects
Commit 5b30b04d authored by Giovanni Bussi's avatar Giovanni Bussi
Browse files

Fixed cmd map

Using c++11 to initialize the map instead of calling a function everytime
parent d87aba9a
No related branches found
No related tags found
No related merge requests found
......@@ -37,20 +37,8 @@
using namespace std;
#include "CLToolMainEnum.inc"
namespace PLMD {
const std::unordered_map<std::string, int> & clToolMainWordMap() {
static std::unordered_map<std::string, int> word_map;
static bool init=false;
if(!init) {
#include "CLToolMainMap.inc"
}
init=true;
return word_map;
}
CLToolMain::CLToolMain():
argc(0),
in(stdin),
......@@ -66,6 +54,16 @@ CLToolMain::~CLToolMain() {
void CLToolMain::cmd(const std::string& word,void*val) {
// Enumerate all possible commands:
enum {
#include "CLToolMainEnum.inc"
};
// Static object (initialized once) containing the map of commands:
const static std::unordered_map<std::string, int> word_map = {
#include "CLToolMainMap.inc"
};
std::vector<std::string> words=Tools::getWords(word);
unsigned nw=words.size();
if(nw==0) {
......@@ -74,8 +72,8 @@ void CLToolMain::cmd(const std::string& word,void*val) {
int iword=-1;
char**v;
char*vv;
const auto it=clToolMainWordMap().find(words[0]);
if(it!=clToolMainWordMap().end()) iword=it->second;
const auto it=word_map.find(words[0]);
if(it!=word_map.end()) iword=it->second;
switch(iword) {
case cmd_setArgc:
CHECK_NULL(val,word);
......
......@@ -27,21 +27,9 @@
#include <sstream>
#include <unordered_map>
#include "GREXEnum.inc"
using namespace std;
namespace PLMD {
const std::unordered_map<std::string, int> & GREXWordMap() {
static std::unordered_map<std::string, int> word_map;
static bool init=false;
if(!init) {
#include "GREXMap.inc"
}
init=true;
return word_map;
}
GREX::GREX(PlumedMain&p):
initialized(false),
plumedMain(p),
......@@ -65,14 +53,25 @@ GREX::~GREX() {
#define CHECK_NOTNULL(val,word) plumed_massert(val,"NULL pointer received in cmd(\"GREX " + word + "\")");
void GREX::cmd(const string&key,void*val) {
// Enumerate all possible commands:
enum {
#include "GREXEnum.inc"
};
// Static object (initialized once) containing the map of commands:
const static std::unordered_map<std::string, int> word_map = {
#include "GREXMap.inc"
};
std::vector<std::string> words=Tools::getWords(key);
unsigned nw=words.size();
if(nw==0) {
// do nothing
} else {
int iword=-1;
const auto it=GREXWordMap().find(words[0]);
if(it!=GREXWordMap().end()) iword=it->second;
const auto it=word_map.find(words[0]);
if(it!=word_map.end()) iword=it->second;
switch(iword) {
case cmd_initialized:
CHECK_NOTNULL(val,key);
......
......@@ -48,20 +48,8 @@
using namespace std;
#include "PlumedMainEnum.inc"
namespace PLMD {
const std::unordered_map<std::string, int> & plumedMainWordMap() {
static std::unordered_map<std::string, int> word_map;
static bool init=false;
if(!init) {
#include "PlumedMainMap.inc"
}
init=true;
return word_map;
}
PlumedMain::PlumedMain():
initialized(false),
// automatically write on log in destructor
......@@ -100,6 +88,16 @@ PlumedMain::~PlumedMain() {
void PlumedMain::cmd(const std::string & word,void*val) {
// Enumerate all possible commands:
enum {
#include "PlumedMainEnum.inc"
};
// Static object (initialized once) containing the map of commands:
const static std::unordered_map<std::string, int> word_map = {
#include "PlumedMainMap.inc"
};
try {
auto ss=stopwatch.startPause();
......@@ -111,8 +109,8 @@ void PlumedMain::cmd(const std::string & word,void*val) {
} else {
int iword=-1;
double d;
const auto it=plumedMainWordMap().find(words[0]);
if(it!=plumedMainWordMap().end()) iword=it->second;
const auto it=word_map.find(words[0]);
if(it!=word_map.end()) iword=it->second;
switch(iword) {
case cmd_setBox:
CHECK_INIT(initialized,word);
......
......@@ -13,15 +13,17 @@ awk -v opt=$1 '
}END{
n=i;
if(opt=="enum"){
print "enum {"
for(i=1;i<=n;i++){
comma=","
if(i==n) comma=""
print enum[i],comma;
comma=",";
if(i==n) comma="";
printf("%s%s\n",enum[i],comma);
}
print "};"
} else if(opt=="map"){
for(i=1;i<=n;i++) printf(" word_map[\"%s\"]=%s;\n",string[i],enum[i]);
for(i=1;i<=n;i++) {
comma=",";
if(i==n) comma="";
printf(" {\"%s\",%s}%s\n",string[i],enum[i],comma);
}
}
}'
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment