[Liblas-commits] hg-main-tree: add writeBufferBegin/End functions, like on the re...

liblas-commits at liblas.org liblas-commits at liblas.org
Tue Aug 2 19:18:53 EDT 2011


details:   http://hg.libpc.orghg-main-tree/rev/a7909cd12dca
changeset: 997:a7909cd12dca
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Tue Aug 02 16:18:44 2011 -0700
description:
add writeBufferBegin/End functions, like on the read side; added a bunch of error checking to make sure the begin/end sequences are obeyed

diffstat:

 include/pdal/StageIterator.hpp |   3 +
 include/pdal/Writer.hpp        |   4 +
 src/StageIterator.cpp          |  96 +++++++++++++++++++++++++++++++++++++++--
 src/Writer.cpp                 |  23 ++++++++-
 4 files changed, 118 insertions(+), 8 deletions(-)

diffs (243 lines):

diff -r 0d5423c96f98 -r a7909cd12dca include/pdal/StageIterator.hpp
--- a/include/pdal/StageIterator.hpp	Tue Aug 02 15:43:51 2011 -0700
+++ b/include/pdal/StageIterator.hpp	Tue Aug 02 16:18:44 2011 -0700
@@ -116,6 +116,9 @@
     const Stage& m_stage;
     boost::uint32_t m_chunkSize;
 
+    bool m_readBeginPerformed;
+    bool m_readBufferBeginPerformed;
+
     StageIterator& operator=(const StageIterator&); // not implemented
     StageIterator(const StageIterator&); // not implemented
 };
diff -r 0d5423c96f98 -r a7909cd12dca include/pdal/Writer.hpp
--- a/include/pdal/Writer.hpp	Tue Aug 02 15:43:51 2011 -0700
+++ b/include/pdal/Writer.hpp	Tue Aug 02 16:18:44 2011 -0700
@@ -73,9 +73,13 @@
     // this is called once before the loop with the writeBuffer calls
     virtual void writeBegin() = 0;
 
+    virtual void writeBufferBegin(const PointBuffer&) {}
+
     // called repeatedly, until out of data
     virtual boost::uint32_t writeBuffer(const PointBuffer&) = 0;
 
+    virtual void writeBufferEnd(const PointBuffer&) {}
+
     // called once, after the writeBuffer calls
     virtual void writeEnd() = 0;
 
diff -r 0d5423c96f98 -r a7909cd12dca src/StageIterator.cpp
--- a/src/StageIterator.cpp	Tue Aug 02 15:43:51 2011 -0700
+++ b/src/StageIterator.cpp	Tue Aug 02 16:18:44 2011 -0700
@@ -55,6 +55,8 @@
     : m_index(0)
     , m_stage(stage)
     , m_chunkSize(s_defaultChunkSize)
+    , m_readBeginPerformed(false)
+    , m_readBufferBeginPerformed(false)
 {
     return;
 }
@@ -62,6 +64,10 @@
 
 StageIterator::~StageIterator()
 {
+    // sanity checks (these are not exceptions, because dtors should not throw)
+    //assert(!m_readBufferBeginPerformed);
+    //assert(!m_readBeginPerformed);
+
     return;
 }
 
@@ -109,7 +115,21 @@
         throw pdal_error("stage not initialized: " + m_stage.getName());
     }
 
-    readBeginImpl();
+    if (m_readBeginPerformed)
+    {
+        throw pdal_error("readBegin called without corresponding readEnd");
+    }
+    m_readBeginPerformed = true;
+
+    try
+    {
+        readBeginImpl();
+    }
+    catch (...)
+    {
+        m_readBeginPerformed = false;
+        throw;
+    }
 
     return;
 }
@@ -117,7 +137,26 @@
 
 void StageIterator::readBufferBegin(PointBuffer& buffer)
 {
-    readBufferBeginImpl(buffer);
+    if (!m_readBeginPerformed)
+    {
+        throw pdal_error("readBufferBegin called without corresponding readBegin");
+    }
+    if (m_readBufferBeginPerformed)
+    {
+        throw pdal_error("readBufferBegin called without corresponding readBufferEnd");
+    }
+    m_readBufferBeginPerformed = true;
+
+    try
+    {
+        readBufferBeginImpl(buffer);
+    }
+    catch (...)
+    {
+        m_readBeginPerformed = false;
+        m_readBufferBeginPerformed = false;
+        throw;
+    }
 
     return;
 }
@@ -125,8 +164,24 @@
 
 boost::uint32_t StageIterator::readBuffer(PointBuffer& buffer)
 {
-    const boost::uint32_t numRead = readBufferImpl(buffer);
+    if (!m_readBufferBeginPerformed)
+    {
+        throw pdal_error("readBuffer called without corresponding readBufferBegin");
+    }
+
+    boost::uint32_t numRead = 0;
     
+    try
+    {
+        numRead = readBufferImpl(buffer);
+    }
+    catch (...)
+    {
+        m_readBeginPerformed = false;
+        m_readBufferBeginPerformed = false;
+        throw;
+    }
+
     m_index += numRead;
 
     return numRead;
@@ -135,7 +190,23 @@
 
 void StageIterator::readBufferEnd(PointBuffer& buffer)
 {
-    readBufferEndImpl(buffer);
+    if (!m_readBufferBeginPerformed)
+    {
+        throw pdal_error("readBufferEnd called without corresponding readBufferBegin");
+    }
+
+    try
+    {
+        readBufferEndImpl(buffer);
+    }
+    catch (...)
+    {
+        m_readBeginPerformed = false;
+        m_readBufferBeginPerformed = false;
+        throw;
+    }
+    
+    m_readBufferBeginPerformed = false;
 
     return;
 }
@@ -143,7 +214,22 @@
 
 void StageIterator::readEnd()
 {
-    readEndImpl();
+    if (!m_readBeginPerformed)
+    {
+        throw pdal_error("readEnd called without corresponding readBegin");
+    }
+
+    try
+    {
+        readEndImpl();
+    }
+    catch (...)
+    {
+        m_readBeginPerformed = false;
+        throw;
+    }
+
+    m_readBeginPerformed = false;
 
     return;
 }
diff -r 0d5423c96f98 -r a7909cd12dca src/Writer.cpp
--- a/src/Writer.cpp	Tue Aug 02 15:43:51 2011 -0700
+++ b/src/Writer.cpp	Tue Aug 02 16:18:44 2011 -0700
@@ -109,20 +109,29 @@
 
     writeBegin();
 
+    iter->readBegin();
+
     // in case targetNumPointsToWrite is really big, we will process just one chunk at a time
     
     if (targetNumPointsToWrite == 0)
     {
         PointBuffer buffer(m_prevStage.getSchema(), m_chunkSize);
-        boost::uint32_t numPointsReadThisChunk = iter->read(buffer);
+        iter->readBufferBegin(buffer);
+        boost::uint32_t numPointsReadThisChunk = iter->readBuffer(buffer);
+        iter->readBufferEnd(buffer);
         boost::uint32_t numPointsWrittenThisChunk = 0;
         while (numPointsReadThisChunk != 0)
         {
+            writeBufferBegin(buffer);
             numPointsWrittenThisChunk = writeBuffer(buffer);
+            writeBufferEnd(buffer);
             m_actualNumPointsWritten += numPointsWrittenThisChunk;
 
             buffer.setNumPoints(0); // reset the buffer, so we can use it again
-            numPointsReadThisChunk = iter->read(buffer);
+
+            iter->readBufferBegin(buffer);
+            numPointsReadThisChunk = iter->readBuffer(buffer);
+            iter->readBufferEnd(buffer);
         }
     } else 
     {
@@ -137,12 +146,19 @@
 
             PointBuffer buffer(m_prevStage.getSchema(), numPointsToReadThisChunk);
 
-            const boost::uint32_t numPointsReadThisChunk = iter->read(buffer);
+            iter->readBufferBegin(buffer);
+            const boost::uint32_t numPointsReadThisChunk = iter->readBuffer(buffer);
+            iter->readBufferEnd(buffer);
+
             assert(numPointsReadThisChunk <= numPointsToReadThisChunk);
 
+            writeBufferBegin(buffer);
+
             const boost::uint32_t numPointsWrittenThisChunk = writeBuffer(buffer);
             assert(numPointsWrittenThisChunk == numPointsReadThisChunk);
 
+            writeBufferEnd(buffer);
+
             m_actualNumPointsWritten += numPointsWrittenThisChunk;
 
             if (iter->atEnd())
@@ -152,6 +168,7 @@
         }
     }
 
+    iter->readEnd();
 
     writeEnd();
 


More information about the Liblas-commits mailing list