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 387758 : 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 387928 : void cmd(std::string_view key,const TypesafePtr & val,const std::size_t* shape) { 59 387928 : cmd(key,TypesafePtr::setNelemAndShape(val,0,shape)); 60 387928 : } 61 387928 : void cmd(std::string_view key,const TypesafePtr & val,std::initializer_list<SizeLike> shape) { 62 387928 : if(shape.size()>4) plumed_error() << "Maximum shape size is 4"; 63 : std::array<std::size_t,5> shape_; 64 : unsigned j=0; 65 1033798 : for(auto i : shape) { 66 645870 : shape_[j]=i.size; 67 645870 : j++; 68 : } 69 387928 : shape_[j]=0; 70 387928 : cmd(key,val,shape_.data()); 71 387928 : } 72 : template<typename I, typename std::enable_if<std::is_integral<I>::value, int>::type = 0> 73 74 : void cmd(std::string_view key,const TypesafePtr & val,I nelem, const std::size_t* shape=nullptr) { 74 74 : cmd(key,TypesafePtr::setNelemAndShape(val,nelem,shape)); 75 74 : } 76 : /// This is needed to avoid ambiguities 77 960463 : void cmd(const char* key,const TypesafePtr & val=nullptr) { 78 960463 : cmd(std::string_view(key),val); 79 960297 : } 80 : virtual ~WithCmd(); 81 : }; 82 : 83 : inline 84 0 : WithCmd::~WithCmd() { 85 : // do nothing 86 : // here just to allow inheriting from this class properly 87 0 : } 88 : 89 : } 90 : 91 : #endif