rrset_collection_base.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, \c rrclass and \c rrtype. If no matching RRset
  39. /// is found, \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. /// \return The RRset if found, \c NULL otherwise.
  45. virtual isc::dns::ConstRRsetPtr find
  46. (const isc::dns::Name& name, const isc::dns::RRClass& rrclass,
  47. const isc::dns::RRType& rrtype)
  48. const = 0;
  49. /// \brief Destructor
  50. virtual ~RRsetCollectionBase() {}
  51. protected:
  52. class Iter; // forward declaration
  53. /// \brief Wraps Iter with a reference count.
  54. typedef boost::shared_ptr<Iter> IterPtr;
  55. /// \brief A helper iterator interface for \c RRsetCollectionBase.
  56. ///
  57. /// This is a protected iterator class that is a helper interface
  58. /// used by the public iterator. Derived classes of
  59. /// \c RRsetCollectionBase are supposed to implement this class and
  60. /// the \c getBeginning() and \c getEnd() methods, so that the
  61. /// public interator interface can be provided. This is a forward
  62. /// iterator only.
  63. class Iter {
  64. public:
  65. virtual ~Iter() {};
  66. /// \brief Returns the \c AbstractRRset currently pointed to by
  67. /// the iterator.
  68. virtual const isc::dns::AbstractRRset& getValue() = 0;
  69. /// \brief Returns an \c IterPtr wrapping an Iter pointing to
  70. /// the next \c AbstractRRset in sequence in the collection.
  71. virtual IterPtr getNext() = 0;
  72. /// \brief Check if another iterator is equal to this one.
  73. ///
  74. /// Returns \c true if this iterator is equal to \c other,
  75. /// \c false otherwise. Note that if \c other is not the same
  76. /// type as \c this, or cannot be compared meaningfully, the
  77. /// method must return \c false.
  78. ///
  79. /// \param other The other iterator to compare against.
  80. /// \return \c true if equal, \c false otherwise.
  81. virtual bool equals(Iter& other) = 0;
  82. };
  83. /// \brief Returns an \c IterPtr wrapping an Iter pointing to the
  84. /// beginning of the collection.
  85. virtual IterPtr getBeginning() = 0;
  86. /// \brief Returns an \c IterPtr wrapping an Iter pointing past the
  87. /// end of the collection.
  88. virtual IterPtr getEnd() = 0;
  89. public:
  90. /// \brief A forward \c std::iterator for \c RRsetCollectionBase.
  91. ///
  92. /// It behaves like a \c std::iterator forward iterator, so please
  93. /// see its documentation for usage.
  94. class Iterator : std::iterator<std::forward_iterator_tag,
  95. const isc::dns::AbstractRRset>
  96. {
  97. public:
  98. explicit Iterator(IterPtr iter) :
  99. iter_(iter)
  100. {}
  101. reference operator*() {
  102. return (iter_->getValue());
  103. }
  104. Iterator& operator++() {
  105. iter_ = iter_->getNext();
  106. return (*this);
  107. }
  108. Iterator operator++(int) {
  109. Iterator tmp(iter_);
  110. ++*this;
  111. return (tmp);
  112. }
  113. bool operator==(const Iterator& other) const {
  114. return (iter_->equals(*other.iter_));
  115. }
  116. bool operator!=(const Iterator& other) const {
  117. return (!iter_->equals(*other.iter_));
  118. }
  119. private:
  120. IterPtr iter_;
  121. };
  122. /// \brief Returns an iterator pointing to the beginning of the
  123. /// collection.
  124. Iterator begin() {
  125. return Iterator(getBeginning());
  126. }
  127. /// \brief Returns an iterator pointing past the end of the
  128. /// collection.
  129. Iterator end() {
  130. return Iterator(getEnd());
  131. }
  132. };
  133. } // end of namespace dns
  134. } // end of namespace isc
  135. #endif // RRSET_COLLECTION_BASE_H
  136. // Local Variables:
  137. // mode: c++
  138. // End: