rrset_collection_base.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // Copyright (C) 2012 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 RRSET_COLLECTION_BASE_H
  15. #define RRSET_COLLECTION_BASE_H 1
  16. #include <dns/rrset.h>
  17. #include <dns/name.h>
  18. #include <boost/shared_ptr.hpp>
  19. #include <iterator>
  20. namespace isc {
  21. namespace dns {
  22. /// \brief Generic class to represent a set of RRsets.
  23. ///
  24. /// This is a generic container and the stored set of RRsets does not
  25. /// necessarily form a valid zone (e.g. there doesn't necessarily have
  26. /// to be an SOA at the "origin"). Instead, it will be used to represent
  27. /// a single zone for the purpose of zone loading/checking. It provides
  28. /// a simple find() method to find an RRset for the given name and type
  29. /// (and maybe class) and a way to iterate over all RRsets.
  30. ///
  31. /// See \c RRsetCollection for a simple libdns++ implementation using an
  32. /// STL container. libdatasrc will have another implementation.
  33. class RRsetCollectionBase {
  34. public:
  35. /// \brief Find a matching RRset in the collection.
  36. ///
  37. /// Returns the RRset in the collection that exactly matches the
  38. /// given \c name and \c rrtype. If no matching RRset is found,
  39. /// \c NULL is returned.
  40. ///
  41. /// \param name The name of the RRset to search for.
  42. /// \param rrtype The type of the RRset to search for.
  43. /// \param rrclass The class of the RRset to search for.
  44. /// \returns A pointer to the RRset if found, \c NULL otherwise.
  45. virtual const isc::dns::AbstractRRset* find
  46. (const isc::dns::Name& name, const isc::dns::RRType& rrtype,
  47. const isc::dns::RRClass& rrclass)
  48. const = 0;
  49. /// \brief Destructor
  50. virtual ~RRsetCollectionBase() {}
  51. protected:
  52. class Iter; // forward declaration
  53. typedef boost::shared_ptr<Iter> IterPtr;
  54. class Iter {
  55. public:
  56. virtual const isc::dns::AbstractRRset& getValue() = 0;
  57. virtual IterPtr getNext() = 0;
  58. /// \brief Check if another iterator is equal to this one.
  59. ///
  60. /// Returns \c true if this iterator is equal to \c other,
  61. /// \c false otherwise. Note that if \c other is not the same
  62. /// type as \c this, or cannot be compared meaningfully, the
  63. /// method must return \c false.
  64. ///
  65. /// \param other The other iterator to compare against.
  66. /// \returns \c true if equal, \c false otherwise.
  67. virtual bool equals(Iter& other) = 0;
  68. };
  69. virtual IterPtr getBeginning() = 0;
  70. virtual IterPtr getEnd() = 0;
  71. public:
  72. class iterator : std::iterator<std::forward_iterator_tag,
  73. const isc::dns::AbstractRRset>
  74. {
  75. public:
  76. explicit iterator(IterPtr iter) :
  77. iter_(iter)
  78. {}
  79. reference operator*() {
  80. return (iter_->getValue());
  81. }
  82. iterator& operator++() {
  83. iter_ = iter_->getNext();
  84. return (*this);
  85. }
  86. iterator operator++(int) {
  87. iterator tmp(iter_);
  88. ++*this;
  89. return (tmp);
  90. }
  91. bool operator==(const iterator& other) const {
  92. return (iter_->equals(*other.iter_));
  93. }
  94. bool operator!=(const iterator& other) const {
  95. return (!iter_->equals(*other.iter_));
  96. }
  97. private:
  98. IterPtr iter_;
  99. };
  100. iterator begin() {
  101. return iterator(getBeginning());
  102. }
  103. iterator end() {
  104. return iterator(getEnd());
  105. }
  106. };
  107. } // end of namespace dns
  108. } // end of namespace isc
  109. #endif // RRSET_COLLECTION_BASE_H
  110. // Local Variables:
  111. // mode: c++
  112. // End: