[Liblas-commits] hg-main-tree: back PointData's m_data with a boost::scoped_array...

liblas-commits at liblas.org liblas-commits at liblas.org
Tue Mar 22 12:39:31 EDT 2011


details:   http://hg.libpc.orghg-main-tree/rev/05f72f01754c
changeset: 385:05f72f01754c
user:      Howard Butler <hobu.inc at gmail.com>
date:      Tue Mar 22 11:39:21 2011 -0500
description:
back PointData's m_data with a boost::scoped_array instead of a naked array of bytes so we get some exception and leak safety

diffstat:

 include/libpc/PointData.hpp |   7 ++++---
 src/PointData.cpp           |  17 ++++++-----------
 test/unit/PointDataTest.cpp |  20 +++++++++++++++++++-
 3 files changed, 29 insertions(+), 15 deletions(-)

diffs (143 lines):

diff -r 4685539ca0f9 -r 05f72f01754c include/libpc/PointData.hpp
--- a/include/libpc/PointData.hpp	Tue Mar 22 10:39:17 2011 -0500
+++ b/include/libpc/PointData.hpp	Tue Mar 22 11:39:21 2011 -0500
@@ -36,6 +36,7 @@
 #define INCLUDED_POINTDATA_HPP
 
 #include <boost/cstdint.hpp>
+#include <boost/scoped_array.hpp>
 
 #include <libpc/export.hpp>
 #include <libpc/Bounds.hpp>
@@ -114,7 +115,7 @@
 private:
 
     SchemaLayout m_schemaLayout;
-    boost::uint8_t* m_data;
+    boost::scoped_array<boost::uint8_t> m_data;
     std::size_t m_pointSize;
     boost::uint32_t m_numPoints;
     boost::uint32_t m_capacity;    
@@ -128,7 +129,7 @@
 {
     std::size_t offset = (pointIndex * m_pointSize) + m_schemaLayout.getDimensionLayout(fieldIndex).getByteOffset();
     assert(offset + sizeof(T) <= m_pointSize * m_capacity);
-    boost::uint8_t* p = m_data + offset;
+    boost::uint8_t* p = m_data.get() + offset;
 
     *(T*)p = value;
 }
@@ -139,7 +140,7 @@
 {
     std::size_t offset = (pointIndex * m_pointSize) + m_schemaLayout.getDimensionLayout(fieldIndex).getByteOffset();
     assert(offset + sizeof(T) <= m_pointSize * m_capacity);
-    boost::uint8_t* p = m_data + offset;
+    boost::uint8_t* p = m_data.get() + offset;
 
     return *(T*)p;
 }
diff -r 4685539ca0f9 -r 05f72f01754c src/PointData.cpp
--- a/src/PointData.cpp	Tue Mar 22 10:39:17 2011 -0500
+++ b/src/PointData.cpp	Tue Mar 22 11:39:21 2011 -0500
@@ -46,20 +46,19 @@
 
 PointData::PointData(const SchemaLayout& schemaLayout, boost::uint32_t capacity)
     : m_schemaLayout(schemaLayout)
-    , m_data(NULL)
+    , m_data(new boost::uint8_t[m_schemaLayout.getByteSize() * capacity])
     , m_pointSize(m_schemaLayout.getByteSize())
     , m_numPoints(0)
     , m_capacity(capacity)
     , m_bounds(Bounds<double>::getDefaultSpatialExtent())
 {
-    m_data = new boost::uint8_t[m_pointSize * m_capacity];
 
     return;
 }
 
 PointData::PointData(PointData const& other) 
     : m_schemaLayout(other.getSchemaLayout())
-    , m_data(0)
+    , m_data(new boost::uint8_t[m_schemaLayout.getByteSize() * other.m_capacity])
     , m_pointSize(m_schemaLayout.getByteSize())
     , m_numPoints(other.m_numPoints)
     , m_capacity(other.m_capacity)
@@ -67,8 +66,7 @@
 {
     if (other.m_data)
     {
-        m_data = new boost::uint8_t[m_pointSize * m_capacity];
-        memcpy(m_data, other.m_data, m_pointSize*m_capacity);
+        memcpy(m_data.get(), other.m_data.get(), m_pointSize*m_capacity);
     }
 
 }
@@ -78,13 +76,12 @@
     if (&rhs != this)
     {
         m_schemaLayout = rhs.getSchemaLayout();
-        m_data = new boost::uint8_t[m_schemaLayout.getByteSize() * rhs.getCapacity()];
-        memcpy(m_data, rhs.m_data, m_pointSize*m_capacity);
-        
         m_pointSize = m_schemaLayout.getByteSize();
         m_numPoints = rhs.getNumPoints();
         m_capacity = rhs.getCapacity();
         m_bounds = rhs.getSpatialBounds();
+        memcpy(m_data.get(), rhs.m_data.get(), m_pointSize*m_capacity);
+        
         
     }
     return *this;
@@ -92,8 +89,6 @@
 
 PointData::~PointData()
 {
-    if (m_data)
-        delete[] m_data;
 }
 
 
@@ -111,7 +106,7 @@
 
 boost::uint8_t* PointData::getData(std::size_t index) const
 {
-    return m_data + m_pointSize * index;
+    return m_data.get() + m_pointSize * index;
 }
 
 
diff -r 4685539ca0f9 -r 05f72f01754c test/unit/PointDataTest.cpp
--- a/test/unit/PointDataTest.cpp	Tue Mar 22 10:39:17 2011 -0500
+++ b/test/unit/PointDataTest.cpp	Tue Mar 22 11:39:21 2011 -0500
@@ -60,7 +60,7 @@
 }
 
 
-static PointData* makeTestBuffer()
+PointData* makeTestBuffer()
 {
     Dimension d1(Dimension::Field_X, Dimension::Uint8);
     Dimension d2(Dimension::Field_Y, Dimension::Int32);
@@ -174,4 +174,22 @@
     delete data;
 }
 
+BOOST_AUTO_TEST_CASE(test_copy_constructor)
+{
+    PointData* data = makeTestBuffer();
+    
+    PointData d2(*data);
+    verifyTestBuffer(d2);
+    delete data;
+}
+
+BOOST_AUTO_TEST_CASE(test_assignment_constructor)
+{
+    PointData* data = makeTestBuffer();
+    
+    PointData d2 = *data;
+    verifyTestBuffer(d2);
+    delete data;
+}
+
 BOOST_AUTO_TEST_SUITE_END()


More information about the Liblas-commits mailing list