rrset_collection_base.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. protected:
  50. class Iter; // forward declaration
  51. typedef boost::shared_ptr<Iter> IterPtr;
  52. class Iter {
  53. public:
  54. virtual const isc::dns::AbstractRRset& getValue() = 0;
  55. virtual IterPtr getNext() = 0;
  56. virtual bool equals(Iter& other) = 0;
  57. };
  58. virtual IterPtr getBeginning() = 0;
  59. virtual IterPtr getEnd() = 0;
  60. public:
  61. class iterator : std::iterator<std::forward_iterator_tag,
  62. const isc::dns::AbstractRRset>
  63. {
  64. public:
  65. explicit iterator(IterPtr iter) :
  66. iter_(iter)
  67. {}
  68. reference operator*() {
  69. return (iter_->getValue());
  70. }
  71. iterator& operator++() {
  72. iter_ = iter_->getNext();
  73. return (*this);
  74. }
  75. iterator operator++(int) {
  76. iterator tmp(iter_);
  77. ++*this;
  78. return (tmp);
  79. }
  80. bool operator==(const iterator& other) const {
  81. return (iter_->equals(*other.iter_));
  82. }
  83. bool operator!=(const iterator& other) const {
  84. return (!iter_->equals(*other.iter_));
  85. }
  86. private:
  87. IterPtr iter_;
  88. };
  89. iterator begin() {
  90. return iterator(getBeginning());
  91. }
  92. iterator end() {
  93. return iterator(getEnd());
  94. }
  95. };
  96. } // end of namespace dns
  97. } // end of namespace isc
  98. #endif // RRSET_COLLECTION_BASE_H
  99. // Local Variables:
  100. // mode: c++
  101. // End: