[Liblas-commits] hg-main-tree: StreamOwner class ready

liblas-commits at liblas.org liblas-commits at liblas.org
Tue Jul 26 10:55:37 EDT 2011


details:   http://hg.libpc.orghg-main-tree/rev/9ef5cbcc90bc
changeset: 933:9ef5cbcc90bc
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Tue Jul 26 07:55:22 2011 -0700
description:
StreamOwner class ready
Subject: hg-main-tree: merge

details:   http://hg.libpc.orghg-main-tree/rev/c1aa0d010dbf
changeset: 934:c1aa0d010dbf
user:      Michael P. Gerlek <mpg at flaxen.com>
date:      Tue Jul 26 07:55:34 2011 -0700
description:
merge

diffstat:

 include/pdal/StreamOwner.hpp  |   54 +++++++++++++------
 src/StreamOwner.cpp           |  113 +++++++++++++++++++++--------------------
 src/drivers/oci/Writer.cpp    |    2 +-
 test/unit/StreamOwnerTest.cpp |   62 ++++++++++++++++++++++-
 4 files changed, 158 insertions(+), 73 deletions(-)

diffs (truncated from 315 to 300 lines):

diff -r 3b44435ac590 -r c1aa0d010dbf include/pdal/StreamOwner.hpp
--- a/include/pdal/StreamOwner.hpp	Mon Jul 25 17:30:59 2011 -0700
+++ b/include/pdal/StreamOwner.hpp	Tue Jul 26 07:55:34 2011 -0700
@@ -54,32 +54,52 @@
 // This could, I suppose, evolve into something that even takes in things
 // other than std streams or filenames.
 
-class PDAL_DLL StreamOwner
+class PDAL_DLL StreamOwnerBase
 {
 public:
-    enum Mode { ReadMode, WriteMode };
+    StreamOwnerBase(const std::string& filename);
+    virtual ~StreamOwnerBase() {}
 
-public:
-    StreamOwner(const std::string filename, Mode mode);
-    StreamOwner(std::istream*);
-    StreamOwner(std::ostream*);
-    ~StreamOwner();
-
-    std::istream* istream();
-    std::ostream* ostream();
+    // returns "" if stream-based ctor was used
+    virtual const std::string& getFileName() const;
 
 private:
-    Mode m_mode; // read or write?
-    bool m_owned; // if true, we have to delete the stream
+    std::string m_filename;
 
-    std::istream* m_istream;
-    std::ostream* m_ostream;
-
-    StreamOwner& operator=(const StreamOwner&); // not implemented
-    StreamOwner(const StreamOwner&); // not implemented
+    StreamOwnerBase(const StreamOwnerBase&); // nope
+    StreamOwnerBase operator=(const StreamOwnerBase&); // nope
 };
 
 
+class PDAL_DLL IStreamOwner : public StreamOwnerBase
+{
+public:
+    IStreamOwner(const std::string& filename);
+    IStreamOwner(std::istream&);
+    ~IStreamOwner();
+
+    std::istream& istream();
+
+private:
+    std::istream* m_pistream; // not NULL iff we own the stream
+    std::istream& m_istream;
+};
+
+
+class PDAL_DLL OStreamOwner : public StreamOwnerBase
+{
+public:
+    OStreamOwner(const std::string& filename);
+    OStreamOwner(std::ostream&);
+    ~OStreamOwner();
+
+    std::ostream& ostream();
+
+private:
+    std::ostream* m_postream;
+    std::ostream& m_ostream;
+};
+
 } // namespace pdal
 
 #endif
diff -r 3b44435ac590 -r c1aa0d010dbf src/StreamOwner.cpp
--- a/src/StreamOwner.cpp	Mon Jul 25 17:30:59 2011 -0700
+++ b/src/StreamOwner.cpp	Tue Jul 26 07:55:34 2011 -0700
@@ -40,88 +40,93 @@
 namespace pdal
 {
 
-StreamOwner::StreamOwner(const std::string filename, Mode mode)
-    : m_mode(mode)
-    , m_owned(true)
-    , m_istream(NULL)
-    , m_ostream(NULL)
+
+StreamOwnerBase::StreamOwnerBase(const std::string& filename)
+    : m_filename(filename)
 {
-    if (m_mode == ReadMode)
+    return;
+}
+
+
+const std::string& StreamOwnerBase::getFileName() const
+{
+    return m_filename;
+}
+
+
+// -------------------------------------------------------------------------------
+
+IStreamOwner::IStreamOwner(const std::string& filename)
+    : StreamOwnerBase(filename)
+    , m_pistream(Utils::openFile(filename, true))
+    , m_istream(*m_pistream)
+{
+    return;
+}
+
+
+IStreamOwner::IStreamOwner(std::istream& istream)
+    : StreamOwnerBase("")
+    , m_pistream(NULL)
+    , m_istream(istream)
+{
+    return;
+}
+
+
+IStreamOwner::~IStreamOwner()
+{
+    if (m_pistream)
     {
-        m_istream = Utils::openFile(filename);
-    }
-    else if (m_mode == WriteMode)
-    {
-        m_ostream = Utils::createFile(filename);
-    }
-    else
-    {
-        throw pdal_error("invalid file mode");
+        Utils::closeFile(m_pistream);
+        m_pistream = NULL;
     }
 
     return;
 }
 
 
-StreamOwner::StreamOwner(std::istream* istream)
-    : m_mode(ReadMode)
-    , m_owned(false)
-    , m_istream(istream)
-    , m_ostream(NULL)
+std::istream& IStreamOwner::istream()
+{
+    return m_istream;
+}
+
+
+// -------------------------------------------------------------------------------
+
+
+OStreamOwner::OStreamOwner(const std::string& filename)
+    : StreamOwnerBase(filename)
+    , m_postream(Utils::createFile(filename, true))
+    , m_ostream(*m_postream)
 {
     return;
 }
 
 
-StreamOwner::StreamOwner(std::ostream* ostream)
-    : m_mode(WriteMode)
-    , m_owned(false)
-    , m_istream(NULL)
+OStreamOwner::OStreamOwner(std::ostream& ostream)
+    : StreamOwnerBase("")
+    , m_postream(NULL)
     , m_ostream(ostream)
 {
     return;
 }
 
 
-StreamOwner::~StreamOwner()
+OStreamOwner::~OStreamOwner()
 {
-    if (m_istream)
+    if (m_postream)
     {
-        if (m_owned)
-        {
-            Utils::closeFile(m_istream);
-        }
-        m_istream = NULL;
-    }
-    if (m_ostream)
-    {
-        if (m_owned)
-        {
-            Utils::closeFile(m_ostream);
-        }
-        m_ostream = NULL;
+        Utils::closeFile(m_postream);
+        m_postream = NULL;
     }
 
     return;
 }
 
 
-std::istream* StreamOwner::istream()
+std::ostream& OStreamOwner::ostream()
 {
-    if (m_mode != ReadMode)
-    {
-        throw pdal_error("invalid file mode");
-    }
-    return m_istream;
-}
-
-
-std::ostream* StreamOwner::ostream()
-{
-    if (m_mode != WriteMode)
-    {
-        throw pdal_error("invalid file mode");
-    }
     return m_ostream;
 }
 
diff -r 3b44435ac590 -r c1aa0d010dbf src/drivers/oci/Writer.cpp
--- a/src/drivers/oci/Writer.cpp	Mon Jul 25 17:30:59 2011 -0700
+++ b/src/drivers/oci/Writer.cpp	Tue Jul 26 07:55:34 2011 -0700
@@ -872,7 +872,7 @@
     {
         statement->WriteCLob( &boundary_locator, wkt ); 
         statement->Bind(&boundary_locator);
-        statement->Bind((long int*)&srid);
+        statement->Bind((int*)&srid);
 
     }
 
diff -r 3b44435ac590 -r c1aa0d010dbf test/unit/StreamOwnerTest.cpp
--- a/test/unit/StreamOwnerTest.cpp	Mon Jul 25 17:30:59 2011 -0700
+++ b/test/unit/StreamOwnerTest.cpp	Tue Jul 26 07:55:34 2011 -0700
@@ -37,7 +37,11 @@
 #include <boost/test/unit_test.hpp>
 #include <boost/cstdint.hpp>
 
-#include <pdal/Range.hpp>
+#include <pdal/StreamOwner.hpp>
+#include <pdal/Utils.hpp>
+#include <pdal/exceptions.hpp>
+
+#include "Support.hpp"
 
 using namespace pdal;
 
@@ -45,6 +49,62 @@
 
 BOOST_AUTO_TEST_CASE(StreamOwnerTest_test1)
 {
+    const std::string rfile = Support::datapath("1.2-with-color.las");
+    const std::string wfile = "temp.txt";
+
+    // remove file from earlier run, if needed
+    Utils::deleteFile(wfile);
+    BOOST_CHECK(!Utils::fileExists(wfile));
+
+    {
+        // filename, reading
+        pdal::IStreamOwner file1(rfile);
+        BOOST_CHECK(file1.istream() != NULL);
+        BOOST_CHECK(file1.getFileName() == rfile);
+    }
+
+    {
+        // filename, reading -- should throw
+        bool ok = false;
+        try
+        {
+            pdal::IStreamOwner file1x("sillyfilenamethatdoesnotexist.xml");
+            ok = false;
+        }
+        catch (pdal::pdal_error ex)
+        {
+            ok = true;
+        }
+        BOOST_CHECK(ok);
+    }
+
+    {
+        // filename, writing
+        pdal::OStreamOwner file2(wfile);
+        BOOST_CHECK(file2.ostream() != NULL);
+        BOOST_CHECK(file2.getFileName() == wfile);
+
+        BOOST_CHECK(Utils::fileExists(wfile));
+    }
+
+    {
+        // stream, reading
+        pdal::IStreamOwner file3(std::cin);
+        BOOST_CHECK(file3.istream() == std::cin);
+        BOOST_CHECK(file3.getFileName() == "");
+    }


More information about the Liblas-commits mailing list