Browse Source

[trac404] Make the buffer private

Michal 'vorner' Vaner 14 years ago
parent
commit
3384859c8e

+ 12 - 16
src/lib/dns/messagerenderer.cc

@@ -180,18 +180,8 @@ MessageRenderer::~MessageRenderer() {
 }
 
 void
-MessageRenderer::skip(const size_t len) {
-    buffer_.skip(len);
-}
-
-void
-MessageRenderer::trim(const size_t len) {
-    buffer_.trim(len);
-}
-
-void
 MessageRenderer::clear() {
-    buffer_.clear();
+    AbstractMessageRenderer::clear();
     impl_->nbuffer_.clear();
     impl_->nodeset_.clear();
     impl_->msglength_limit_ = 512;
@@ -254,15 +244,15 @@ MessageRenderer::writeName(const Name& name, const bool compress) {
     }
 
     // Record the current offset before extending the buffer.
-    const size_t offset = buffer_.getLength();
+    const size_t offset = getBuffer().getLength();
     // Write uncompress part...
-    buffer_.writeData(impl_->nbuffer_.getData(),
-                             compress ? i : impl_->nbuffer_.getLength());
+    writeData(impl_->nbuffer_.getData(),
+              compress ? i : impl_->nbuffer_.getLength());
     if (compress && n != notfound) {
         // ...and compression pointer if available.
         uint16_t pointer = (*n).pos_;
         pointer |= Name::COMPRESS_POINTER_MARK16;
-        buffer_.writeUint16(pointer);
+        writeUint16(pointer);
     }
 
     // Finally, add to the set the newly rendered name and its ancestors that
@@ -274,11 +264,17 @@ MessageRenderer::writeName(const Name& name, const bool compress) {
         if (offset + j > Name::MAX_COMPRESS_POINTER) {
             break;
         }
-        impl_->nodeset_.insert(NameCompressNode(*this, buffer_,
+        impl_->nodeset_.insert(NameCompressNode(*this, getBuffer(),
                                                 offset + j,
                                                 impl_->nbuffer_.getLength() -
                                                 j));
     }
 }
+
+void
+AbstractMessageRenderer::clear() {
+    buffer_.clear();
+}
+
 }
 }

+ 16 - 9
src/lib/dns/messagerenderer.h

@@ -103,6 +103,15 @@ protected:
     AbstractMessageRenderer(OutputBuffer& buffer) :
         buffer_(buffer)
     {}
+public:
+    /// \brief The destructor.
+    virtual ~AbstractMessageRenderer() {}
+    //@}
+protected:
+    /// \brief Return the output buffer we render into.
+    const OutputBuffer& getBuffer() const { return (buffer_); }
+    OutputBuffer& getBuffer() { return (buffer_); }
+private:
     /// \short Buffer to store data
     ///
     /// It was decided that there's no need to have this in every subclass,
@@ -110,10 +119,6 @@ protected:
     /// chance to optimise.
     OutputBuffer& buffer_;
 public:
-    /// \brief The destructor.
-    virtual ~AbstractMessageRenderer() {}
-    //@}
-
     ///
     /// \name Getter Methods
     ///
@@ -196,7 +201,9 @@ public:
     /// that is to be filled in later, e.g, by \ref writeUint16At().
     ///
     /// \param len The length of the gap to be inserted in bytes.
-    virtual void skip(size_t len) = 0;
+    void skip(size_t len) {
+        buffer_.skip(len);
+    }
 
     /// \brief Trim the specified length of data from the end of the internal
     /// buffer.
@@ -208,13 +215,15 @@ public:
     /// be thrown.
     ///
     /// \param len The length of data that should be trimmed.
-    virtual void trim(size_t len) = 0;
+    void trim(size_t len) {
+        buffer_.trim(len);
+    }
 
     /// \brief Clear the internal buffer and other internal resources.
     ///
     /// This method can be used to re-initialize and reuse the renderer
     /// without constructing a new one.
-    virtual void clear() = 0;
+    virtual void clear();
 
     /// \brief Write an unsigned 8-bit integer into the internal buffer.
     ///
@@ -312,8 +321,6 @@ public:
     virtual void setTruncated();
     virtual void setLengthLimit(size_t len);
     virtual void setCompressMode(CompressMode mode);
-    virtual void skip(size_t len);
-    virtual void trim(size_t len);
     virtual void clear();
     virtual void writeName(const Name& name, bool compress = true);
 private:

+ 8 - 19
src/lib/dns/rdatafields.cc

@@ -62,8 +62,7 @@ namespace {
 // Technically, this use of inheritance may be considered a violation of
 // Liskov Substitution Principle in that it doesn't actually compress domain
 // names, and some of the methods are not expected to be used.
-// In fact, skip() or trim() may not be well defined for the purpose of this
-// class.
+// In fact, skip() or trim() may not be make much sense in this context.
 // Nevertheless we keep this idea at the moment.  Since the usage is limited
 // (it's only used within this file, and only used with \c Rdata variants),
 // it's hopefully an acceptable practice.
@@ -75,8 +74,8 @@ public:
         mode_(CASE_INSENSITIVE), last_data_pos_(0)
     {}
     virtual ~RdataFieldComposer() {}
-    virtual const void* getData() const { return (buffer_.getData()); }
-    virtual size_t getLength() const { return (buffer_.getLength()); }
+    virtual const void* getData() const { return (getBuffer().getData()); }
+    virtual size_t getLength() const { return (getBuffer().getLength()); }
     virtual bool isTruncated() const { return (truncated_); }
     virtual size_t getLengthLimit() const { return (length_limit_); }
     virtual CompressMode getCompressMode() const { return (mode_); }
@@ -88,25 +87,15 @@ public:
         const RdataFields::Type field_type =
             compress ? RdataFields::COMPRESSIBLE_NAME :
             RdataFields::INCOMPRESSIBLE_NAME;
-        name.toWire(buffer_);
+        name.toWire(getBuffer());
         fields_.push_back(RdataFields::FieldSpec(field_type,
                                                  name.getLength()));
-        last_data_pos_ = buffer_.getLength();
+        last_data_pos_ = getBuffer().getLength();
     }
 
     virtual void clear() {
         isc_throw(Unexpected, "unexpected clear() for RdataFieldComposer");
     }
-    virtual void skip(size_t) {
-        isc_throw(Unexpected, "unexpected skip() for RdataFieldComposer");
-    }
-    virtual void trim(size_t) {
-        isc_throw(Unexpected, "unexpected trim() for RdataFieldComposer");
-    }
-    virtual void writeUint16At(uint16_t, size_t) {
-        isc_throw(Unexpected,
-                  "unexpected writeUint16At() for RdataFieldComposer");
-    }
     bool truncated_;
     size_t length_limit_;
     CompressMode mode_;
@@ -121,7 +110,7 @@ public:
     size_t last_data_pos_;
     void extendData() {
         // No news, return to work
-        if (buffer_.getLength() == last_data_pos_) {
+        if (getBuffer().getLength() == last_data_pos_) {
             return;
         }
         // The new bytes are just ordinary uninteresting data
@@ -129,8 +118,8 @@ public:
             fields_.push_back(RdataFields::FieldSpec(RdataFields::DATA, 0));
         }
         // We added this much data from last time
-        fields_.back().len += buffer_.getLength() - last_data_pos_;
-        last_data_pos_ = buffer_.getLength();
+        fields_.back().len += getBuffer().getLength() - last_data_pos_;
+        last_data_pos_ = getBuffer().getLength();
     }
 };
 

+ 0 - 2
src/lib/dns/tests/rdatafields_unittest.cc

@@ -368,7 +368,5 @@ private:
 
 TEST(RdataFieldComposerTest, unusedMethods) {
     EXPECT_THROW(RdataFields(DummyRdata(DummyRdata::CLEAR)), isc::Unexpected);
-    EXPECT_THROW(RdataFields(DummyRdata(DummyRdata::SKIP)), isc::Unexpected);
-    EXPECT_THROW(RdataFields(DummyRdata(DummyRdata::TRIM)), isc::Unexpected);
 }
 }