Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-2019 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : plumed is distributed in the hope that it will be useful,
15 : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #include "CLToolRegister.h"
23 : #include "tools/Tools.h"
24 : #include "CLTool.h"
25 : #include <algorithm>
26 : #include <iostream>
27 :
28 :
29 : using namespace std;
30 : namespace PLMD {
31 :
32 3214 : CLToolRegister::~CLToolRegister() {
33 1607 : if(m.size()>0) {
34 0 : string names="";
35 0 : for(const auto & p : m) names+=p.first+" ";
36 0 : std::cerr<<"WARNING: CLTools "+ names +" has not been properly unregistered. This might lead to memory leak!!\n";
37 : }
38 1607 : }
39 :
40 41609 : CLToolRegister& cltoolRegister() {
41 41609 : static CLToolRegister ans;
42 41609 : return ans;
43 : }
44 :
45 19284 : void CLToolRegister::remove(creator_pointer f) {
46 109276 : for(auto p=m.begin(); p!=m.end(); ++p) {
47 109276 : if((*p).second==f) {
48 19284 : m.erase(p); break;
49 : }
50 : }
51 19284 : }
52 :
53 19284 : void CLToolRegister::add(string key,creator_pointer f,keywords_pointer kf) {
54 19284 : if(m.count(key)) {
55 0 : m.erase(key);
56 0 : disabled.insert(key);
57 : } else {
58 19284 : m.insert(pair<string,creator_pointer>(key,f));
59 19284 : Keywords keys; kf(keys);
60 19284 : mk.insert(pair<string,Keywords>(key,keys));
61 : };
62 19284 : }
63 :
64 1240 : bool CLToolRegister::check(string key) {
65 1240 : if(m.count(key)>0) return true;
66 0 : return false;
67 : }
68 :
69 1240 : CLTool* CLToolRegister::create(const CLToolOptions&ao) {
70 1240 : if(ao.line.size()<1)return NULL;
71 : CLTool* cltool;
72 1240 : if(check(ao.line[0])) {
73 1240 : CLToolOptions nao( ao,mk[ao.line[0]] );
74 1240 : cltool=m[ao.line[0]](nao);
75 0 : } else cltool=NULL;
76 1240 : return cltool;
77 : }
78 :
79 :
80 223 : std::ostream & operator<<(std::ostream &log,const CLToolRegister&ar) {
81 223 : vector<string> s(ar.list());
82 223 : for(unsigned i=0; i<s.size(); i++) log<<" "<<s[i]<<"\n";
83 223 : if(!ar.disabled.empty()) {
84 0 : s.assign(ar.disabled.size(),"");
85 0 : copy(ar.disabled.begin(),ar.disabled.end(),s.begin());
86 0 : sort(s.begin(),s.end());
87 0 : log<<"+++++++ WARNING +++++++\n";
88 0 : log<<"The following keywords have been registered more than once and will be disabled:\n";
89 0 : for(unsigned i=0; i<s.size(); i++) log<<" - "<<s[i]<<"\n";
90 0 : log<<"+++++++ END WARNING +++++++\n";
91 : };
92 223 : return log;
93 : }
94 :
95 0 : bool CLToolRegister::printManual( const std::string& cltool ) {
96 0 : if ( check(cltool) ) {
97 0 : mk[cltool].print_html();
98 0 : return true;
99 : } else {
100 0 : return false;
101 : }
102 : }
103 :
104 1801 : vector<string> CLToolRegister::list()const {
105 1801 : vector<string> s;
106 1801 : for(const auto & it : m) s.push_back(it.first);
107 1801 : sort(s.begin(),s.end());
108 1801 : return s;
109 : }
110 :
111 :
112 :
113 4821 : }
|