rrsetlist.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef __RRSETLIST_H
  15. #define __RRSETLIST_H 1
  16. #include <iostream>
  17. #include <iterator>
  18. #include <vector>
  19. #include <boost/shared_ptr.hpp>
  20. #include <dns/rrset.h>
  21. #include <dns/rrclass.h>
  22. #include <dns/rrtype.h>
  23. namespace isc {
  24. namespace dns {
  25. class DuplicateRRset : public Exception {
  26. public:
  27. DuplicateRRset(const char* file, size_t line, const char* what) :
  28. isc::Exception(file, line, what) {}
  29. };
  30. template <typename T, typename P, typename R>
  31. class RRsetListIterator :
  32. public std::iterator<std::input_iterator_tag, RRsetPtr> {
  33. public:
  34. RRsetListIterator() {}
  35. explicit RRsetListIterator(const T& it) :
  36. it_(it) {}
  37. RRsetListIterator& operator++()
  38. {
  39. ++it_;
  40. return (*this);
  41. }
  42. RRsetListIterator operator++(int)
  43. {
  44. RRsetListIterator tmp(*this);
  45. ++it_;
  46. return (tmp);
  47. }
  48. R operator*() const
  49. {
  50. return (*it_);
  51. }
  52. P operator->() const
  53. {
  54. return (&(operator*()));
  55. }
  56. bool operator==(const RRsetListIterator& other)
  57. {
  58. return (it_ == other.it_);
  59. }
  60. bool operator!=(const RRsetListIterator& other)
  61. {
  62. return (it_ != other.it_);
  63. }
  64. private:
  65. T it_;
  66. };
  67. /// A set of RRsets.
  68. ///
  69. /// \note Do not use this class unless you really understand what
  70. /// you're doing and you're 100% sure that this class is the best choice
  71. /// for your purpose.
  72. ///
  73. /// Counter intuitively, this class is not a "list" of RRsets but a
  74. /// "set" of them; it doesn't allow multiple RRsets of the same RR
  75. /// type and RR class to be added at the same time. And, for that
  76. /// reason, adding an RRset is more expensive than you'd expect. The
  77. /// class name is confusing, but was named so as a result of
  78. /// compromise: "RRsetset" would look awkward; RRsets would be
  79. /// confusing (with RRset).
  80. ///
  81. /// In any case, if you want a list like container of RRsets, your best choice
  82. /// would be \c std::vector<RRset> or \c std::list<RRset>, not this class.
  83. /// In fact, in many cases \c RRsetList will be a suboptimal choice.
  84. /// This class is defined publicly as part of libdns++ for a historical
  85. /// reason and is actually quite specific to a particular need for libdatasrc.
  86. /// If you are tempted to use it, think twice to assess if this class
  87. /// is really what you want. Again, in many cases the answer will be no.
  88. class RRsetList {
  89. private:
  90. RRsetList(const RRsetList& source);
  91. RRsetList& operator=(const RRsetList& source);
  92. public:
  93. RRsetList() {}
  94. void addRRset(RRsetPtr new_rrsetptr);
  95. void append(RRsetList& source);
  96. RRsetPtr findRRset(const RRType& rrtype, const RRClass& rrclass);
  97. typedef RRsetListIterator<std::vector<RRsetPtr>::iterator,
  98. RRsetPtr*,
  99. RRsetPtr&> iterator;
  100. typedef RRsetListIterator<std::vector<RRsetPtr>::const_iterator,
  101. const RRsetPtr*,
  102. const RRsetPtr&> const_iterator;
  103. const_iterator begin() const { return (const_iterator(rrsets_.begin())); }
  104. const_iterator end() const { return (const_iterator(rrsets_.end())); }
  105. iterator begin() { return (iterator(rrsets_.begin())); }
  106. iterator end() { return (iterator(rrsets_.end())); }
  107. size_t size() const { return (rrsets_.size()); }
  108. private:
  109. std::vector<RRsetPtr> rrsets_;
  110. };
  111. } // end of namespace dns
  112. } // end of namespace isc
  113. #endif // __RRSETLIST_H
  114. // Local Variables:
  115. // mode: c++
  116. // End: