string_collection.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // string_collection.cpp
  2. /*******************************************************************************
  3. * Copyright (c) 2017 Frank Pagliughi <fpagliughi@mindspring.com>
  4. *
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * and Eclipse Distribution License v1.0 which accompany this distribution.
  8. *
  9. * The Eclipse Public License is available at
  10. * http://www.eclipse.org/legal/epl-v10.html
  11. * and the Eclipse Distribution License is available at
  12. * http://www.eclipse.org/org/documents/edl-v10.php.
  13. *
  14. * Contributors:
  15. * Frank Pagliughi - initial implementation and documentation
  16. *******************************************************************************/
  17. #include "mqtt/string_collection.h"
  18. namespace mqtt {
  19. /////////////////////////////////////////////////////////////////////////////
  20. string_collection::string_collection(const string& str) : coll_{ str }
  21. {
  22. update_c_arr();
  23. }
  24. string_collection::string_collection(string&& str) : coll_{ std::move(str) }
  25. {
  26. update_c_arr();
  27. }
  28. string_collection::string_collection(const collection_type& vec) : coll_{ vec }
  29. {
  30. update_c_arr();
  31. }
  32. string_collection::string_collection(collection_type&& vec) : coll_{ std::move(vec) }
  33. {
  34. update_c_arr();
  35. }
  36. string_collection::string_collection(const string_collection& coll) : coll_ { coll.coll_ }
  37. {
  38. update_c_arr();
  39. }
  40. string_collection::string_collection(std::initializer_list<string> sl)
  41. {
  42. for (const auto& s : sl)
  43. coll_.push_back(s);
  44. update_c_arr();
  45. }
  46. string_collection::string_collection(std::initializer_list<const char*> sl)
  47. {
  48. for (const auto& s : sl)
  49. coll_.push_back(string(s));
  50. update_c_arr();
  51. }
  52. void string_collection::update_c_arr()
  53. {
  54. cArr_.clear();
  55. cArr_.reserve(coll_.size());
  56. for (const auto& s : coll_)
  57. cArr_.push_back(s.c_str());
  58. }
  59. string_collection& string_collection::operator=(const string_collection& coll)
  60. {
  61. coll_ = coll.coll_;
  62. update_c_arr();
  63. return *this;
  64. }
  65. void string_collection::push_back(const string& str)
  66. {
  67. coll_.push_back(str);
  68. update_c_arr();
  69. }
  70. void string_collection::push_back(string&& str)
  71. {
  72. coll_.push_back(str);
  73. update_c_arr();
  74. }
  75. void string_collection::clear()
  76. {
  77. coll_.clear();
  78. cArr_.clear();
  79. }
  80. /////////////////////////////////////////////////////////////////////////////
  81. // end namespace mqtt
  82. }