propertyMap.cpp (2073B)
1 #include "propertyMap.h" 2 3 #include <cmath> 4 5 namespace baseLib 6 { 7 bool PropertyMap::add(const std::string& _key, const std::string& _value) 8 { 9 if (m_argsWithValues.find(_key) != m_argsWithValues.end()) 10 return false; 11 m_argsWithValues.insert(std::make_pair(_key, _value)); 12 return true; 13 } 14 15 bool PropertyMap::add(const std::string& _key) 16 { 17 return m_args.insert(_key).second; 18 } 19 20 bool PropertyMap::add(const PropertyMap& _other, const bool _overwrite) 21 { 22 bool changed = false; 23 24 for (const auto& [key, val] : _other.getArgsWithValues()) 25 { 26 if (_overwrite || m_argsWithValues.find(key) == m_argsWithValues.end()) 27 { 28 m_argsWithValues[key] = val; 29 changed = true; 30 } 31 } 32 for (const auto& a : _other.getArgs()) 33 { 34 if (_overwrite || m_args.find(a) == m_args.end()) 35 { 36 m_args.insert(a); 37 changed = true; 38 } 39 } 40 return changed; 41 } 42 43 std::string PropertyMap::tryGet(const std::string& _key, const std::string& _value) const 44 { 45 const auto it = m_argsWithValues.find(_key); 46 if (it == m_argsWithValues.end()) 47 return _value; 48 return it->second; 49 } 50 51 std::string PropertyMap::get(const std::string& _key, const std::string& _default/* = {}*/) const 52 { 53 const auto it = m_argsWithValues.find(_key); 54 if (it == m_argsWithValues.end()) 55 return _default; 56 return it->second; 57 } 58 59 float PropertyMap::getFloat(const std::string& _key, const float _default/* = 0.0f*/) const 60 { 61 const std::string stringResult = get(_key); 62 63 if (stringResult.empty()) 64 return _default; 65 66 const double result = atof(stringResult.c_str()); 67 if (std::isinf(result) || std::isnan(result)) 68 { 69 return _default; 70 } 71 return static_cast<float>(result); 72 } 73 74 int PropertyMap::getInt(const std::string& _key, const int _default/* = 0*/) const 75 { 76 const std::string stringResult = get(_key); 77 if (stringResult.empty()) 78 return _default; 79 return atoi(stringResult.c_str()); 80 } 81 82 bool PropertyMap::contains(const std::string& _key) const 83 { 84 return m_args.find(_key) != m_args.end() || m_argsWithValues.find(_key) != m_argsWithValues.end(); 85 } 86 }