NameValueCollection.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //
  2. // NameValueCollection.cpp
  3. //
  4. // Library: Net
  5. // Package: Messages
  6. // Module: NameValueCollection
  7. //
  8. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  9. // and Contributors.
  10. //
  11. // SPDX-License-Identifier: BSL-1.0
  12. //
  13. #include "Poco/Net/NameValueCollection.h"
  14. #include "Poco/Exception.h"
  15. #include <algorithm>
  16. using Poco::NotFoundException;
  17. namespace Poco {
  18. namespace Net {
  19. NameValueCollection::NameValueCollection()
  20. {
  21. }
  22. NameValueCollection::NameValueCollection(const NameValueCollection& nvc):
  23. _map(nvc._map)
  24. {
  25. }
  26. NameValueCollection::~NameValueCollection()
  27. {
  28. }
  29. NameValueCollection& NameValueCollection::operator = (const NameValueCollection& nvc)
  30. {
  31. if (&nvc != this)
  32. {
  33. _map = nvc._map;
  34. }
  35. return *this;
  36. }
  37. void NameValueCollection::swap(NameValueCollection& nvc)
  38. {
  39. std::swap(_map, nvc._map);
  40. }
  41. const std::string& NameValueCollection::operator [] (const std::string& name) const
  42. {
  43. ConstIterator it = _map.find(name);
  44. if (it != _map.end())
  45. return it->second;
  46. else
  47. throw NotFoundException(name);
  48. }
  49. void NameValueCollection::set(const std::string& name, const std::string& value)
  50. {
  51. Iterator it = _map.find(name);
  52. if (it != _map.end())
  53. it->second = value;
  54. else
  55. _map.insert(HeaderMap::ValueType(name, value));
  56. }
  57. void NameValueCollection::add(const std::string& name, const std::string& value)
  58. {
  59. _map.insert(HeaderMap::ValueType(name, value));
  60. }
  61. const std::string& NameValueCollection::get(const std::string& name) const
  62. {
  63. ConstIterator it = _map.find(name);
  64. if (it != _map.end())
  65. return it->second;
  66. else
  67. throw NotFoundException(name);
  68. }
  69. const std::string& NameValueCollection::get(const std::string& name, const std::string& defaultValue) const
  70. {
  71. ConstIterator it = _map.find(name);
  72. if (it != _map.end())
  73. return it->second;
  74. else
  75. return defaultValue;
  76. }
  77. bool NameValueCollection::has(const std::string& name) const
  78. {
  79. return _map.find(name) != _map.end();
  80. }
  81. NameValueCollection::ConstIterator NameValueCollection::find(const std::string& name) const
  82. {
  83. return _map.find(name);
  84. }
  85. NameValueCollection::ConstIterator NameValueCollection::begin() const
  86. {
  87. return _map.begin();
  88. }
  89. NameValueCollection::ConstIterator NameValueCollection::end() const
  90. {
  91. return _map.end();
  92. }
  93. bool NameValueCollection::empty() const
  94. {
  95. return _map.empty();
  96. }
  97. std::size_t NameValueCollection::size() const
  98. {
  99. return _map.size();
  100. }
  101. void NameValueCollection::erase(const std::string& name)
  102. {
  103. _map.erase(name);
  104. }
  105. void NameValueCollection::clear()
  106. {
  107. _map.clear();
  108. }
  109. } } // namespace Poco::Net