Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2011-2023 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 : #ifndef __PLUMED_core_WithCmd_h 23 : #define __PLUMED_core_WithCmd_h 24 : 25 : #include "tools/TypesafePtr.h" 26 : #include <string> 27 : #include <string_view> 28 : 29 : namespace PLMD { 30 : 31 : /// Base for classes with cmd() method. 32 : /// This is an abstract base class for classes with 33 : /// cmd() method. 34 : class WithCmd { 35 : /// Small structure used to pass elements of a shape initializer_list 36 : struct SizeLike { 37 : std::size_t size; 38 : SizeLike(short unsigned size): size(size) {} 39 : SizeLike(unsigned size): size(size) {} 40 170 : SizeLike(long unsigned size): size(size) {} 41 : SizeLike(long long unsigned size): size(size) {} 42 : SizeLike(short size): size(std::size_t(size)) {} 43 400400 : SizeLike(int size): size(std::size_t(size)) {} 44 : SizeLike(long int size): size(std::size_t(size)) {} 45 : SizeLike(long long int size): size(std::size_t(size)) {} 46 : }; 47 : public: 48 : /// This is the preferred method as it avoid allocations of temporaries. 49 : /// If this is not overridded, it will call the legacy method. 50 0 : virtual void cmd(std::string_view key,const TypesafePtr & val=nullptr) { 51 0 : cmd(std::string(key),val); 52 0 : } 53 : /// This is the legacy method we used in older plumed versions, so it is still possible. 54 : /// If this is not overridden, it will call the preferred method 55 315 : virtual void cmd(const std::string& key,const TypesafePtr & val=nullptr) { 56 315 : cmd(std::string_view(key),val); 57 315 : } 58 400570 : void cmd(std::string_view key,const TypesafePtr & val,const std::size_t* shape) { 59 400570 : cmd(key,TypesafePtr::setNelemAndShape(val,0,shape)); 60 400570 : } 61 400570 : void cmd(std::string_view key,const TypesafePtr & val,std::initializer_list<SizeLike> shape) { 62 400570 : if(shape.size()>4) { 63 0 : plumed_error() << "Maximum shape size is 4"; 64 : } 65 : std::array<std::size_t,5> shape_; 66 : unsigned j=0; 67 1067510 : for(auto i : shape) { 68 666940 : shape_[j]=i.size; 69 666940 : j++; 70 : } 71 400570 : shape_[j]=0; 72 400570 : cmd(key,val,shape_.data()); 73 400570 : } 74 : template<typename I, typename std::enable_if<std::is_integral<I>::value, int>::type = 0> 75 74 : void cmd(std::string_view key,const TypesafePtr & val,I nelem, const std::size_t* shape=nullptr) { 76 74 : cmd(key,TypesafePtr::setNelemAndShape(val,nelem,shape)); 77 74 : } 78 : /// This is needed to avoid ambiguities 79 967672 : void cmd(const char* key,const TypesafePtr & val=nullptr) { 80 967672 : cmd(std::string_view(key),val); 81 967506 : } 82 : virtual ~WithCmd(); 83 : }; 84 : 85 : inline 86 0 : WithCmd::~WithCmd() { 87 : // do nothing 88 : // here just to allow inheriting from this class properly 89 0 : } 90 : 91 : } 92 : 93 : #endif