[Liblas-commits] hg: delay initialization of mask and cache until we attempt to r...

liblas-commits at liblas.org liblas-commits at liblas.org
Thu Oct 28 12:45:53 EDT 2010


changeset 4c958bf3c1f1 in /Volumes/Data/www/liblas.org/hg
details: http://hg.liblas.orghg?cmd=changeset;node=4c958bf3c1f1
summary: delay initialization of mask and cache until we attempt to read the first point.  Make sure that we actually reset the reader if we don't have a mask yet

diffstat:

 include/liblas/detail/reader/cachedreader.hpp |   1 +
 src/detail/reader/cachedreader.cpp            |  36 +++++++++++++++++++++------
 2 files changed, 29 insertions(+), 8 deletions(-)

diffs (93 lines):

diff -r 12532d4c7222 -r 4c958bf3c1f1 include/liblas/detail/reader/cachedreader.hpp
--- a/include/liblas/detail/reader/cachedreader.hpp	Wed Oct 27 11:23:13 2010 -0500
+++ b/include/liblas/detail/reader/cachedreader.hpp	Thu Oct 28 11:45:47 2010 -0500
@@ -89,6 +89,7 @@
 
     typedef std::vector<liblas::Point> cache_type;
     cache_type m_cache;
+    bool m_cache_initialized;
 };
 
 
diff -r 12532d4c7222 -r 4c958bf3c1f1 src/detail/reader/cachedreader.cpp
--- a/src/detail/reader/cachedreader.cpp	Wed Oct 27 11:23:13 2010 -0500
+++ b/src/detail/reader/cachedreader.cpp	Thu Oct 28 11:45:47 2010 -0500
@@ -66,6 +66,7 @@
     , m_cache_size(size)
     , m_cache_start_position(0)
     , m_cache_read_position(0)
+    , m_cache_initialized(false)
 {
 }
 
@@ -81,13 +82,13 @@
     if (m_cache_size > hptr->GetPointRecordsCount()) {
         m_cache_size = hptr->GetPointRecordsCount();
     }
-    // FIXME: Note, vector::resize never shrinks the container and frees memory! Are we aware of this fact here? --mloskot
-    m_cache.resize(m_cache_size);
-    
-    // Mark all positions as uncached and build up the mask
-    // to the size of the number of points in the file
-    boost::uint8_t const uncached_mask = 0;
-    cache_mask_type(hptr->GetPointRecordsCount(), uncached_mask).swap(m_mask);
+    // // FIXME: Note, vector::resize never shrinks the container and frees memory! Are we aware of this fact here? --mloskot
+    // m_cache.resize(m_cache_size);
+    // 
+    // // Mark all positions as uncached and build up the mask
+    // // to the size of the number of points in the file
+    // boost::uint8_t const uncached_mask = 0;
+    // cache_mask_type(hptr->GetPointRecordsCount(), uncached_mask).swap(m_mask);
     
     return hptr;
 }
@@ -138,6 +139,21 @@
     // }
     // std::cout << std::endl;
 
+    // If our point cache and mask have not yet been initialized, we 
+    // should do so before tyring to read any points.  We don't want to do 
+    // this in ::Reset or ::ReadHeader, as these functions may be called 
+    // multiple times and screw up our assumptions.
+    if (!m_cache_initialized) 
+    {
+        m_cache = cache_type(m_cache_size);
+    
+        // Mark all positions as uncached and build up the mask
+        // to the size of the number of points in the file
+        boost::uint8_t const uncached_mask = 0;
+        cache_mask_type(header->GetPointRecordsCount(), uncached_mask).swap(m_mask);
+ 
+        m_cache_initialized = true;
+    }
     if (m_mask[position] == 1) {
         m_cache_read_position = position;
         return m_cache[cache_position];
@@ -206,7 +222,7 @@
         std::string out(output.str());
         throw std::runtime_error(out);
     }
-
+    
     liblas::Point const& p = ReadCachedPoint(n, header);
     m_cache_read_position = n;
     return p;
@@ -215,7 +231,10 @@
 void CachedReaderImpl::Reset(HeaderPtr header)
 {
     if (m_mask.empty())
+    {    
+        ReaderImpl::Reset(header);
         return;
+    }
 
     typedef cache_mask_type::size_type size_type;
     size_type old_cache_start_position = m_cache_start_position;
@@ -232,6 +251,7 @@
 
     m_cache_start_position = 0;
     m_cache_read_position = 0;
+    m_cache_initialized = false;
 
     ReaderImpl::Reset(header);
 }


More information about the Liblas-commits mailing list