Line data Source code
1 : //# Copyright (C) 2019 2 : //# Associated Universities, Inc. Washington DC, USA. 3 : //# 4 : //# This library is free software; you can redistribute it and/or modify it 5 : //# under the terms of the GNU Library General Public License as published by 6 : //# the Free Software Foundation; either version 2 of the License, or (at your 7 : //# option) any later version. 8 : //# 9 : //# This library is distributed in the hope that it will be useful, but WITHOUT 10 : //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 : //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public 12 : //# License for more details. 13 : //# 14 : //# You should have received a copy of the GNU Library General Public License 15 : //# along with this library; if not, write to the Free Software Foundation, 16 : //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA. 17 : //# 18 : //# Correspondence concerning AIPS++ should be addressed as follows: 19 : //# Internet email: casa-feedback@nrao.edu. 20 : //# Postal address: AIPS++ Project Office 21 : //# National Radio Astronomy Observatory 22 : //# 520 Edgemont Road 23 : //# Charlottesville, VA 22903-2475 USA 24 : //# 25 : 26 : #ifndef MS2ASDM_MAPWITHHDEFAULT_H 27 : #define MS2ASDM_MAPWITHHDEFAULT_H 28 : 29 : #include <map> 30 : 31 : #include <casacore/casa/aips.h> 32 : 33 : namespace casa { //# NAMESPACE CASA - BEGIN 34 : 35 : // <summary> 36 : // MapWithDefault adds a default value to the std::map class. This default value 37 : // is used by the [] operator when the key is not already known. 38 : // <summary> 39 : 40 : // <visibility=export> 41 : 42 : // <reviewed reviewer="" date="yyyy/mm/dd" tests="" demos=""> 43 : // </reviewed> 44 : 45 : // <prerequisite> 46 : // <li> std::map 47 : // </prerequisite> 48 : // <etymology> 49 : // </etymology> 50 : // 51 : // <synopsis> 52 : // </synopsis> 53 : 54 : template<class K, class V> class MapWithDefault : public std::map<K,V> 55 : { 56 : public: 57 : // creates a std::map with the associated specified defaultt value 58 0 : MapWithDefault (const V& defaultValue) : std::map<K,V>(), defaultVal(defaultValue) {;} 59 : 60 : // creates a map with default from another one; use copy semantics. 61 : MapWithDefault (const MapWithDefault<K,V>& other) : std::map<K,V>(other), defaultVal(other.defaultVal) {;} 62 : 63 : // Removes a map with default. 64 0 : ~MapWithDefault () {;} 65 : 66 : // Assigns this map with default to another one; copy semantics 67 : MapWithDefault<K,V>& operator= (const MapWithDefault<K,V>& other); 68 : 69 : // this is the specialization which uses the default value as necessary. 70 : // If the map from the key to a value is not defined a mapping will 71 : // be defined from the key to the default value. 72 : V &operator[](const K &key); 73 : 74 : private: 75 : V defaultVal; 76 : }; 77 : 78 : template<class K, class V> inline MapWithDefault<K,V> &MapWithDefault<K,V>::operator=(const MapWithDefault<K,V> &other) \ 79 : { 80 : if (this != other) { 81 : std::map<K,V>::operator=(*other); 82 : this->defaultVal = other->defaultVal; 83 : } 84 : return this; 85 : } 86 : 87 0 : template<class K, class V> inline V &MapWithDefault<K,V>::operator[](const K &key) 88 : { 89 0 : if (this->count(key) == 0) { 90 0 : std::map<K,V>::operator[](key) = this->defaultVal; 91 : } 92 0 : return this->at(key); 93 : } 94 : 95 : } // # NAMESPACE CASA - END 96 : 97 : #endif