HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Stream.h
Go to the documentation of this file.
1 ///////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2012-2018 DreamWorks Animation LLC
4 //
5 // All rights reserved. This software is distributed under the
6 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
7 //
8 // Redistributions of source code must retain the above copyright
9 // and license notice and the following restrictions and disclaimer.
10 //
11 // * Neither the name of DreamWorks Animation nor the names of
12 // its contributors may be used to endorse or promote products derived
13 // from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 // IN NO EVENT SHALL THE COPYRIGHT HOLDERS' AND CONTRIBUTORS' AGGREGATE
27 // LIABILITY FOR ALL CLAIMS REGARDLESS OF THEIR BASIS EXCEED US$250.00.
28 //
29 ///////////////////////////////////////////////////////////////////////////
30 
31 #ifndef OPENVDB_IO_STREAM_HAS_BEEN_INCLUDED
32 #define OPENVDB_IO_STREAM_HAS_BEEN_INCLUDED
33 
34 #include "Archive.h"
35 #include <iosfwd>
36 #include <memory>
37 
38 
39 namespace openvdb {
41 namespace OPENVDB_VERSION_NAME {
42 namespace io {
43 
44 class GridDescriptor;
45 
46 
47 /// Grid archive associated with arbitrary input and output streams (not necessarily files)
48 class OPENVDB_API Stream: public Archive
49 {
50 public:
51  /// @brief Read grids from an input stream.
52  /// @details If @a delayLoad is true, map the contents of the input stream
53  /// into memory and enable delayed loading of grids.
54  /// @note Define the environment variable @c OPENVDB_DISABLE_DELAYED_LOAD
55  /// to disable delayed loading unconditionally.
56  explicit Stream(std::istream&, bool delayLoad = true);
57 
58  /// Construct an archive for stream output.
59  Stream();
60  /// Construct an archive for output to the given stream.
61  explicit Stream(std::ostream&);
62 
63  Stream(const Stream&);
64  Stream& operator=(const Stream&);
65 
66  ~Stream() override;
67 
68  /// @brief Return a copy of this archive.
69  Archive::Ptr copy() const override;
70 
71  /// Return the file-level metadata in a newly created MetaMap.
72  MetaMap::Ptr getMetadata() const;
73 
74  /// Return pointers to the grids that were read from the input stream.
75  GridPtrVecPtr getGrids();
76 
77  /// @brief Write the grids in the given container to this archive's output stream.
78  /// @throw ValueError if this archive was constructed without specifying an output stream.
79  void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const override;
80 
81  /// @brief Write the grids in the given container to this archive's output stream.
82  /// @throw ValueError if this archive was constructed without specifying an output stream.
83  template<typename GridPtrContainerT>
84  void write(const GridPtrContainerT&, const MetaMap& = MetaMap()) const;
85 
86 private:
87  /// Create a new grid of the type specified by the given descriptor,
88  /// then populate the grid from the given input stream.
89  /// @return the newly created grid.
90  GridBase::Ptr readGrid(const GridDescriptor&, std::istream&) const;
91 
92  void writeGrids(std::ostream&, const GridCPtrVec&, const MetaMap&) const;
93 
94 
95  struct Impl;
96  std::unique_ptr<Impl> mImpl;
97 };
98 
99 
100 ////////////////////////////////////////
101 
102 
103 template<typename GridPtrContainerT>
104 inline void
105 Stream::write(const GridPtrContainerT& container, const MetaMap& metadata) const
106 {
107  GridCPtrVec grids;
108  std::copy(container.begin(), container.end(), std::back_inserter(grids));
109  this->write(grids, metadata);
110 }
111 
112 } // namespace io
113 } // namespace OPENVDB_VERSION_NAME
114 } // namespace openvdb
115 
116 #endif // OPENVDB_IO_STREAM_HAS_BEEN_INCLUDED
117 
118 // Copyright (c) 2012-2018 DreamWorks Animation LLC
119 // All rights reserved. This software is distributed under the
120 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
Grid serializer/unserializer.
Definition: Archive.h:59
SharedPtr< GridBase > Ptr
Definition: Grid.h:107
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:189
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:46
void write(const GridCPtrVec &, const MetaMap &=MetaMap()) const override
Write the grids in the given container to this archive's output stream.
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:194
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:440
Grid archive associated with arbitrary input and output streams (not necessarily files) ...
Definition: Stream.h:48
void write(T &out, bool v)
Definition: ImfXdr.h:332
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:442
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h:135