Merge pull request #330 from syvex/feature/MessageParams

Add has, get, and set methods to Poco::Message
This commit is contained in:
Günter Obiltschnig 2013-11-18 12:20:47 -08:00
commit ae10cf754b
2 changed files with 64 additions and 1 deletions

View File

@ -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

View File

@ -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<StringMap::iterator, bool> 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)