From 4ede322f9d590deb5c630658e3bdb5a77670b84b Mon Sep 17 00:00:00 2001 From: Mike Naquin Date: Mon, 18 Nov 2013 14:04:11 -0600 Subject: [PATCH] Add has, get, and set methods to Poco::Message for better control over accessing logging parameters. --- Foundation/include/Poco/Message.h | 19 ++++++++++++- Foundation/src/Message.cpp | 46 +++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/Foundation/include/Poco/Message.h b/Foundation/include/Poco/Message.h index b2352eeb1..fafb25efe 100644 --- a/Foundation/include/Poco/Message.h +++ b/Foundation/include/Poco/Message.h @@ -177,7 +177,24 @@ public: /// Returns the source file line of the statement /// generating the log message. May be 0 /// if not set. - + + bool has(const std::string& param) const; + /// Returns true if a parameter with the given name exists. + + const std::string& get(const std::string& param) const; + /// Returns a const reference to the value of the parameter + /// with the given name. Throws a NotFoundException if the + /// parameter does not exist. + + const std::string& get(const std::string& param, const std::string& defaultValue) const; + /// Returns a const reference to the value of the parameter + /// with the given name. If the parameter with the given name + /// does not exist, then defaultValue is returned. + + void set(const std::string& param, const std::string& value); + /// Sets the value for a parameter. If the parameter does + /// not exist, then it is created. + const std::string& operator [] (const std::string& param) const; /// Returns a const reference to the value of the parameter /// with the given name. Throws a NotFoundException if the diff --git a/Foundation/src/Message.cpp b/Foundation/src/Message.cpp index 5f941b7d0..0ef4b9b71 100644 --- a/Foundation/src/Message.cpp +++ b/Foundation/src/Message.cpp @@ -223,6 +223,52 @@ void Message::setSourceLine(int line) } +bool Message::has(const std::string& param) const +{ + return _pMap && (_pMap->find(param) != _pMap->end()); +} + + +const std::string& Message::get(const std::string& param) const +{ + if (_pMap) + { + StringMap::const_iterator it = _pMap->find(param); + if (it != _pMap->end()) + return it->second; + } + + throw NotFoundException(); +} + + +const std::string& Message::get(const std::string& param, const std::string& defaultValue) const +{ + if (_pMap) + { + StringMap::const_iterator it = _pMap->find(param); + if (it != _pMap->end()) + return it->second; + } + + return defaultValue; +} + + +void Message::set(const std::string& param, const std::string& value) +{ + if (!_pMap) + _pMap = new StringMap; + + std::pair result = + _pMap->insert(std::make_pair(param, value)); + if (!result.second) + { + result.first->second = value; + } +} + + const std::string& Message::operator [] (const std::string& param) const { if (_pMap)