1 // -*- C++ -*- 2 /** 3 * @brief ResourceMap defines a resource catalog 4 * 5 * Copyright 2005-2021 Airbus-EDF-IMACS-ONERA-Phimeca 6 * 7 * This library is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as published by 9 * the Free Software Foundation, either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * This library is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with this library. If not, see <http://www.gnu.org/licenses/>. 19 * 20 */ 21 22 #ifndef OPENTURNS_RESOURCEMAP_HXX 23 #define OPENTURNS_RESOURCEMAP_HXX 24 25 #include <map> 26 27 #include "openturns/Log.hxx" // ensures correct static initialization order 28 #include "openturns/OStream.hxx" 29 #include "openturns/Pointer.hxx" 30 #include "openturns/Path.hxx" 31 #include "openturns/MutexLock.hxx" 32 33 BEGIN_NAMESPACE_OPENTURNS 34 35 /** 36 * @class ResourceMap 37 * @brief Defines a catalog containing all default values used by OpenTURNS 38 * 39 * As OpenTURNS uses many default values for its computations, their actual values are 40 * accessible and editable through this class. Some default values are hardcoded in this class, 41 * some other are read in an configuration file. 42 */ 43 44 class OT_API ResourceMap 45 { 46 public: 47 #ifndef SWIG 48 /** GetInstance gives a locked access to the singleton */ 49 static MutexLockSingleton<ResourceMap> GetInstance(); 50 #endif 51 52 /** Get a value in the maps */ 53 static String GetType(const String & key); 54 static String Get(const String & key); 55 static String GetAsString(const String & key); 56 static Bool GetAsBool(const String & key); 57 static UnsignedInteger GetAsUnsignedInteger(const String & key); 58 static Scalar GetAsScalar(const String & key); 59 60 /** Set a value in the maps */ 61 static void Set(const String & key, const String & value); 62 static void SetAsString(const String & key, const String & value); 63 static void SetAsBool(const String & key, const Bool value); 64 static void SetAsUnsignedInteger(const String & key, const UnsignedInteger value); 65 static void SetAsScalar(const String & key, const Scalar value); 66 67 /** Add a value in the maps */ 68 static void AddAsString(const String & key, const String & value); 69 static void AddAsBool(const String & key, const Bool value); 70 static void AddAsUnsignedInteger(const String & key, const UnsignedInteger value); 71 static void AddAsScalar(const String & key, const Scalar value); 72 73 /** Get the total size of the maps */ 74 static UnsignedInteger GetSize(); 75 static UnsignedInteger GetStringSize(); 76 static UnsignedInteger GetScalarSize(); 77 static UnsignedInteger GetUnsignedIntegerSize(); 78 static UnsignedInteger GetBoolSize(); 79 80 /** Get the list of keys in the maps */ 81 static std::vector<String> GetKeys(); 82 static std::vector<String> GetStringKeys(); 83 static std::vector<String> GetScalarKeys(); 84 static std::vector<String> GetUnsignedIntegerKeys(); 85 static std::vector<String> GetBoolKeys(); 86 87 /** Is the specific key present ? */ 88 static Bool HasKey(const String & key); 89 90 /** Remove a key */ 91 static void RemoveKey(const String & key); 92 93 /** Reload configuration */ 94 static void Reload(); 95 96 /** Get the list of keys associated to a class */ 97 static std::vector<String> FindKeys(const String & substr); 98 99 /** String representation */ 100 String __repr__() const; 101 102 protected: 103 /** Method for retrieving information from the resource map 104 * @return The list of keys in the maps 105 */ 106 std::vector<String> getKeys() const; 107 108 /** Method for retrieving information from the resource map 109 * @return The list of keys in the string map 110 */ 111 std::vector<String> getStringKeys() const; 112 113 /** Method for retrieving information from the resource map 114 * @return The list of keys in the scalar map 115 */ 116 std::vector<String> getScalarKeys() const; 117 118 /** Method for retrieving information from the resource map 119 * @return The list of keys in the unsigned integer map 120 */ 121 std::vector<String> getUnsignedIntegerKeys() const; 122 123 /** Method for retrieving information from the resource map 124 * @return The list of keys in the bool map 125 */ 126 std::vector<String> getBoolKeys() const; 127 128 /** Method for retrieving information from the resource map 129 * @param key The name under which the value is stored in the ResourceMap 130 * @return the type of the key 131 */ 132 String getType(const String & key) const; 133 134 /** Method for retrieving information from the resource map 135 * @param key The name under which the value is stored in the ResourceMap 136 * @return The value written into a string if it is not already present in a dedicated map 137 */ 138 String get(const String & key) const; 139 140 /** Method for retrieving information from the resource map 141 * @param key The name under which the value is stored in the ResourceMap 142 * @return The value written into a string 143 */ 144 String getAsString(const String & key) const; 145 146 /** Method for retrieving information from the resource map 147 * @param key The name under which the value is stored in the ResourceMap 148 * @return The value if the value is boolean castable, false otherwise 149 */ 150 Bool getAsBool(const String & key) const; 151 152 /** Method for retrieving information from the resource map 153 * @param key The name under which the value is stored in the ResourceMap 154 * @return The value if the value is integer castable, zero otherwise 155 */ 156 UnsignedInteger getAsUnsignedInteger(const String & key) const; 157 158 /** Method for retrieving information from the resource map 159 * @param key The name under which the value is stored in the ResourceMap 160 * @return The value if the value is double castable, zero otherwise 161 */ 162 Scalar getAsScalar(const String & key) const; 163 164 /** Method for retrieving information from the resource map 165 * @return The number of constants defined in one of the maps 166 */ 167 UnsignedInteger getSize() const; 168 169 /** Method for retrieving information from the resource map 170 * @return The number of constants defined in the string map 171 */ 172 UnsignedInteger getStringSize() const; 173 174 /** Method for retrieving information from the resource map 175 * @return The number of constants defined in the scalar map 176 */ 177 UnsignedInteger getScalarSize() const; 178 179 /** Method for retrieving information from the resource map 180 * @return The number of constants defined in the unsigned integer map 181 */ 182 UnsignedInteger getUnsignedIntegerSize() const; 183 184 /** Method for retrieving information from the resource map 185 * @return The number of constants defined in the bool map 186 */ 187 UnsignedInteger getBoolSize() const; 188 189 /** Method for setting information into the resource map 190 * @param key The name under which the value is stored in the ResourceMap 191 * @param value The value written to a string 192 */ 193 void set(const String & key, const String & value); 194 195 /** Method for setting information into the resource map 196 * @param key The name under which the value is stored in the ResourceMap 197 * @param value The value written to a string 198 */ 199 void setAsString(const String & key, const String & value); 200 201 /** Method for setting information into the resource map 202 * @param key The name under which the value is stored in the ResourceMap 203 * @param value The value as a boolean 204 */ 205 void setAsBool(const String & key, const Bool value); 206 207 /** Method for setting information into the resource map 208 * @param key The name under which the value is stored in the ResourceMap 209 * @param value The value as an integer 210 */ 211 void setAsUnsignedInteger(const String & key, const UnsignedInteger value); 212 213 /** Method for setting information into the resource map 214 * @param key The name under which the value is stored in the ResourceMap 215 * @param value The value as a double 216 */ 217 void setAsScalar(const String & key, const Scalar value); 218 219 /** Method for adding information into the resource map 220 * @param key The name under which the value is stored in the ResourceMap 221 * @param value The value written to a string 222 */ 223 void addAsString(const String & key, const String & value); 224 225 /** Method for adding information into the resource map 226 * @param key The name under which the value is stored in the ResourceMap 227 * @param value The value as a boolean 228 */ 229 void addAsBool(const String & key, const Bool value); 230 231 /** Method for adding information into the resource map 232 * @param key The name under which the value is stored in the ResourceMap 233 * @param value The value as an integer 234 */ 235 void addAsUnsignedInteger(const String & key, const UnsignedInteger value); 236 237 /** Method for adding information into the resource map 238 * @param key The name under which the value is stored in the ResourceMap 239 * @param value The value as a double 240 */ 241 void addAsScalar(const String & key, const Scalar value); 242 243 /** Is the specific key present ? */ 244 Bool hasKey(const String & key) const; 245 246 /** Remove a key from the resource map */ 247 void removeKey(const String & key); 248 249 /** Update the ResourceMap with information from the configuration file */ 250 void readConfigurationFile(const FileName & configurationFile); 251 252 /** Find the configuration file in specific path (see Path class for algorithm) */ 253 FileName findConfigurationFile() const; 254 255 /** Load the configuration file */ 256 void loadConfigurationFile(); 257 258 /** Load the configuration defined at installation time */ 259 void loadDefaultConfiguration(); 260 261 /** Reload configuration */ 262 void reload(); 263 264 /** Get the list of keys associated to a class */ 265 std::vector<String> findKeys(const String & substr); 266 private: 267 268 /** Default constructor */ 269 ResourceMap(); 270 271 /** Default constructor */ ResourceMap(const ResourceMap & other)272 ResourceMap(const ResourceMap & other) 273 : mapString_(other.mapString_) 274 , mapScalar_(other.mapScalar_) 275 , mapUnsignedInteger_(other.mapUnsignedInteger_) 276 , mapBool_(other.mapBool_) 277 {} 278 279 /** The actual map that stores the key/value pairs */ 280 typedef std::map< String, String > MapStringType; 281 typedef std::map< String, Scalar > MapScalarType; 282 typedef std::map< String, UnsignedInteger > MapUnsignedIntegerType; 283 typedef std::map< String, Bool > MapBoolType; 284 285 MapStringType mapString_; 286 MapScalarType mapScalar_; 287 MapUnsignedIntegerType mapUnsignedInteger_; 288 MapBoolType mapBool_; 289 290 friend struct ResourceMap_init; 291 }; /* class ResourceMap */ 292 293 /** This struct initializes all static members of ResourceMap */ 294 struct OT_API ResourceMap_init 295 { 296 ResourceMap_init(); 297 ~ResourceMap_init(); 298 }; 299 300 /** 301 * @fn std::ostream & operator <<(std::ostream & os, const ResourceMap & obj) 302 * @brief Output stream converter 303 * @param os A STL output stream resourceMap 304 * @param obj The resourceMap read by \em os 305 * @return A reference to \em os 306 * 307 * Operator << converts the ResourceMap object to an output stream 308 * so it is easy to show the content of the resourceMap. 309 */ 310 #ifndef SWIG 311 OT_API std::ostream & operator <<(std::ostream & os, const MutexLockSingleton<ResourceMap> & obj); 312 OT_API OStream & operator <<(OStream & OS, const MutexLockSingleton<ResourceMap> & obj); 313 #endif 314 315 316 END_NAMESPACE_OPENTURNS 317 318 #endif /* OPENTURNS_RESOURCEMAP_HXX */ 319