[Liblas-commits] hg: use boost::scoped_ptr for LASzip object lifetime management ...

liblas-commits at liblas.org liblas-commits at liblas.org
Wed Jun 22 22:29:06 EDT 2011


details:   http://hg.liblas.orghg/rev/40a924f1ee9a
changeset: 2995:40a924f1ee9a
user:      Howard Butler <hobu.inc at gmail.com>
date:      Wed Jun 22 21:28:56 2011 -0500
description:
use boost::scoped_ptr for LASzip object lifetime management and use the new LASzip 2.0 error messages when they are available #232

diffstat:

 include/liblas/detail/reader/zipreader.hpp |    7 +-
 include/liblas/detail/writer/zipwriter.hpp |    9 +-
 src/detail/reader/zipreader.cpp            |  111 +++++++++------------------
 src/detail/writer/zipwriter.cpp            |  114 +++++++++++-----------------
 4 files changed, 91 insertions(+), 150 deletions(-)

diffs (truncated from 357 to 300 lines):

diff -r 6fb8cbd436c4 -r 40a924f1ee9a include/liblas/detail/reader/zipreader.hpp
--- a/include/liblas/detail/reader/zipreader.hpp	Wed Jun 22 11:53:38 2011 -0500
+++ b/include/liblas/detail/reader/zipreader.hpp	Wed Jun 22 21:28:56 2011 -0500
@@ -112,9 +112,10 @@
 private:
     void ReadIdiom();
 
-    LASzip* m_zip;
-    LASunzipper* m_unzipper;
-    ZipPoint* m_zipPoint;
+    boost::scoped_ptr<LASzip> m_zip;
+    boost::scoped_ptr<ZipPoint> m_zipPoint;
+    boost::scoped_ptr<LASunzipper> m_unzipper;
+
     bool bNeedHeaderCheck;
     
     // Blocked copying operations, declared but not defined.
diff -r 6fb8cbd436c4 -r 40a924f1ee9a include/liblas/detail/writer/zipwriter.hpp
--- a/include/liblas/detail/writer/zipwriter.hpp	Wed Jun 22 11:53:38 2011 -0500
+++ b/include/liblas/detail/writer/zipwriter.hpp	Wed Jun 22 21:28:56 2011 -0500
@@ -50,6 +50,7 @@
 // boost
 #include <boost/cstdint.hpp>
 #include <boost/shared_ptr.hpp>
+#include <boost/scoped_ptr.hpp>
 
 // liblaszip
 class LASzip;
@@ -98,10 +99,10 @@
 private:
     boost::uint32_t m_pointCount;
 
-    LASzip* m_zip;
-    LASzipper* m_zipper;
-    ZipPoint* m_zipPoint;
-
+    boost::scoped_ptr<LASzip> m_zip;
+    boost::scoped_ptr<LASzipper> m_zipper;
+    boost::scoped_ptr<ZipPoint> m_zipPoint;
+    
     // block copying operations
     ZipWriterImpl(ZipWriterImpl const& other);
     ZipWriterImpl& operator=(ZipWriterImpl const& other);
diff -r 6fb8cbd436c4 -r 40a924f1ee9a src/detail/reader/zipreader.cpp
--- a/src/detail/reader/zipreader.cpp	Wed Jun 22 11:53:38 2011 -0500
+++ b/src/detail/reader/zipreader.cpp	Wed Jun 22 21:28:56 2011 -0500
@@ -72,9 +72,6 @@
     , m_point(PointPtr(new liblas::Point()))
     , m_filters(0)
     , m_transforms(0)
-    , m_zip(0)
-    , m_unzipper(0)
-    , m_zipPoint(0)
     , bNeedHeaderCheck(false)
 {
     return;
@@ -85,17 +82,11 @@
     if (m_unzipper)
     {
         m_unzipper->close();
-        delete m_unzipper;
-        m_unzipper = 0;
     }
 
-    if (m_zip)
-    {
-        delete m_zip;
-        m_zip = 0;
-    }
-
-    delete m_zipPoint;
+    m_zipPoint.reset();
+    m_zip.reset();
+    m_unzipper.reset();
 
     return;
 }
@@ -114,72 +105,49 @@
     // we'll create a point reader at this point.
     if (!m_zip)
     {
-        try
+
+        // Initialize a scoped_ptr and swap it with our member variable 
+        // that will contain it.
+        boost::scoped_ptr<LASzip> s(new LASzip());
+        m_zip.swap(s);
+
+        PointFormatName format = m_header->GetDataFormatId();
+        boost::scoped_ptr<ZipPoint> z(new ZipPoint(format, m_header->GetVLRs()));
+        m_zipPoint.swap(z);
+
+        bool ok = false;
+        ok = m_zip->setup((unsigned char)format, m_header->GetDataRecordLength());
+
+        if (!ok)
         {
-            m_zip = new LASzip();
-        }
-        catch(...)
-        {
-            throw liblas_error("Failed to open laszip compression core (1)"); 
+            std::ostringstream oss;
+            oss << "Error setting up compression engine: " << std::string(m_zip->get_error());
+            throw liblas_error(oss.str());
         }
 
-        PointFormatName format = m_header->GetDataFormatId();
-        delete m_zipPoint;
-        m_zipPoint = new ZipPoint(format, m_header->GetVLRs());
-
-        bool ok = false;
-        try
-        {
-            ok = m_zip->setup((unsigned char)format, m_header->GetDataRecordLength());
-        }
-        catch(...)
-        {
-            throw liblas_error("Error opening compression core (3)");
-        }
+        ok = m_zip->unpack(m_zipPoint->our_vlr_data, m_zipPoint->our_vlr_num);
         if (!ok)
         {
-            throw liblas_error("Error opening compression core (2)");
-        }
-
-        try
-        {
-
-            ok = m_zip->unpack(m_zipPoint->our_vlr_data, m_zipPoint->our_vlr_num);
-        }
-        catch(...)
-        {
-            throw liblas_error("Failed to open laszip compression core (2)"); 
-        }
-        if (!ok)
-        {
-            throw liblas_error("Failed to open laszip compression core (3)"); 
+            std::ostringstream oss;
+            oss << "Error unpacking zip VLR data: " << std::string(m_zip->get_error());
+            throw liblas_error(oss.str());
         }
     }
 
     if (!m_unzipper)
     {
-        try
-        {
-            m_unzipper = new LASunzipper();
-        }
-        catch(...)
-        {
-            throw liblas_error("Failed to open laszip decompression engine (1)"); 
-        }
+        boost::scoped_ptr<LASunzipper> z(new LASunzipper());
+        m_unzipper.swap(z);
 
         bool stat(false);
-        try
-        {
-            m_ifs.seekg(m_header->GetDataOffset(), std::ios::beg);
-            stat = m_unzipper->open(m_ifs, m_zip);
-        }
-        catch(...)
-        {
-            throw liblas_error("Failed to open laszip decompression engine (2)"); 
-        }
+        m_ifs.seekg(m_header->GetDataOffset(), std::ios::beg);
+        stat = m_unzipper->open(m_ifs, m_zip.get());
+
         if (!stat)
         {
-            throw liblas_error("Failed to open laszip decompression engine (3)"); 
+            std::ostringstream oss;
+            oss << "Failed to open LASzip stream: " << std::string(m_zip->get_error());
+            throw liblas_error(oss.str());
         }
     }
 
@@ -247,17 +215,14 @@
 void ZipReaderImpl::ReadIdiom()
 {
     bool ok = false;
-    try
-    {
-        ok = m_unzipper->read(m_zipPoint->m_lz_point);
-    }
-    catch(...)
-    {
-        throw liblas_error("Error reading compressed point data (1)");
-    }
+
+    ok = m_unzipper->read(m_zipPoint->m_lz_point);
+
     if (!ok)
     {
-        throw liblas_error("Error reading compressed point data (2)");
+        std::ostringstream oss;
+        oss << "Error reading compressed point data: " << std::string(m_zip->get_error());
+        throw liblas_error(oss.str());
     }
 
     {
diff -r 6fb8cbd436c4 -r 40a924f1ee9a src/detail/writer/zipwriter.cpp
--- a/src/detail/writer/zipwriter.cpp	Wed Jun 22 11:53:38 2011 -0500
+++ b/src/detail/writer/zipwriter.cpp	Wed Jun 22 21:28:56 2011 -0500
@@ -101,96 +101,69 @@
 
 void ZipWriterImpl::WritePoint(liblas::Point const& point)
 {
-    if (m_zip==NULL)
+    if (!m_zip)
     {
-        try
+        // Initialize a scoped_ptr and swap it with our member variable 
+        // that will contain it.
+        boost::scoped_ptr<LASzip> s(new LASzip());
+        m_zip.swap(s);
+            
+        PointFormatName format = m_header->GetDataFormatId();
+
+        boost::scoped_ptr<ZipPoint> z(new ZipPoint(format, m_header->GetVLRs()));
+        m_zipPoint.swap(z);
+        
+        bool ok = false;
+        ok = m_zip->setup((unsigned char)format, m_header->GetDataRecordLength());
+        if (!ok)
         {
-            m_zip = new LASzip();
-        }
-        catch(...)
-        {
-            throw liblas_error("Error opening compression core (1)");
+            std::ostringstream oss;
+            oss << "Error opening compression core: " << std::string(m_zip->get_error());
+            throw liblas_error(oss.str());
         }
 
-        PointFormatName format = m_header->GetDataFormatId();
-        delete m_zipPoint;
-        m_zipPoint = new ZipPoint(format, m_header->GetVLRs());
-
-        bool ok = false;
-        try
-        {
-            ok = m_zip->setup((unsigned char)format, m_header->GetDataRecordLength());
-        }
-        catch(...)
-        {
-            throw liblas_error("Error opening compression core (3)");
-        }
+        ok = m_zip->pack(m_zipPoint->his_vlr_data, m_zipPoint->his_vlr_num);
         if (!ok)
         {
-            throw liblas_error("Error opening compression core (2)");
-        }
-
-        try
-        {
-            ok = m_zip->pack(m_zipPoint->his_vlr_data, m_zipPoint->his_vlr_num);
-        }
-        catch(...)
-        {
-            throw liblas_error("Error opening compression core (3)");
-        }
-        if (!ok)
-        {
-            throw liblas_error("Error opening compression core (2)");
+            std::ostringstream oss;
+            oss << "Error packing VLR data for compression: " << std::string(m_zip->get_error());
+            throw liblas_error(oss.str());
         }
     }
 
-    if (m_zipper==NULL)
+    if (!m_zipper)
     {
-        try
-        {
-            m_zipper = new LASzipper();
-        }
-        catch(...)
-        {
-            throw liblas_error("Error opening compression engine (1)");
-        }
+        boost::scoped_ptr<LASzipper> z(new LASzipper());
+        m_zipper.swap(z);
 
         bool stat(false);
-        try
-        {
-            stat = m_zipper->open(m_ofs, m_zip);
-        }
-        catch(...)
-        {
-            throw liblas_error("Error opening compression engine (3)");
-        }
+
+        stat = m_zipper->open(m_ofs, m_zip.get());
         if (!stat)


More information about the Liblas-commits mailing list