1 // Copyright Contributors to the OpenVDB Project
2 // SPDX-License-Identifier: MPL-2.0
3 
4 #ifndef OPENVDB_IO_STREAM_HAS_BEEN_INCLUDED
5 #define OPENVDB_IO_STREAM_HAS_BEEN_INCLUDED
6 
7 #include "Archive.h"
8 #include <iosfwd>
9 #include <memory>
10 
11 
12 namespace openvdb {
13 OPENVDB_USE_VERSION_NAMESPACE
14 namespace OPENVDB_VERSION_NAME {
15 namespace io {
16 
17 class GridDescriptor;
18 
19 
20 /// Grid archive associated with arbitrary input and output streams (not necessarily files)
21 class OPENVDB_API Stream: public Archive
22 {
23 public:
24     /// @brief Read grids from an input stream.
25     /// @details If @a delayLoad is true, map the contents of the input stream
26     /// into memory and enable delayed loading of grids.
27     /// @note Define the environment variable @c OPENVDB_DISABLE_DELAYED_LOAD
28     /// to disable delayed loading unconditionally.
29     explicit Stream(std::istream&, bool delayLoad = true);
30 
31     /// Construct an archive for stream output.
32     Stream();
33     /// Construct an archive for output to the given stream.
34     explicit Stream(std::ostream&);
35 
36     Stream(const Stream&);
37     Stream& operator=(const Stream&);
38 
39     ~Stream() override;
40 
41     /// @brief Return a copy of this archive.
42     Archive::Ptr copy() const override;
43 
44     /// Return the file-level metadata in a newly created MetaMap.
45     MetaMap::Ptr getMetadata() const;
46 
47     /// Return pointers to the grids that were read from the input stream.
48     GridPtrVecPtr getGrids();
49 
50     /// @brief Write the grids in the given container to this archive's output stream.
51     /// @throw ValueError if this archive was constructed without specifying an output stream.
52     void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const override;
53 
54     /// @brief Write the grids in the given container to this archive's output stream.
55     /// @throw ValueError if this archive was constructed without specifying an output stream.
56     template<typename GridPtrContainerT>
57     void write(const GridPtrContainerT&, const MetaMap& = MetaMap()) const;
58 
59 private:
60     /// Create a new grid of the type specified by the given descriptor,
61     /// then populate the grid from the given input stream.
62     /// @return the newly created grid.
63     GridBase::Ptr readGrid(const GridDescriptor&, std::istream&) const;
64 
65     void writeGrids(std::ostream&, const GridCPtrVec&, const MetaMap&) const;
66 
67 
68     struct Impl;
69     std::unique_ptr<Impl> mImpl;
70 };
71 
72 
73 ////////////////////////////////////////
74 
75 
76 template<typename GridPtrContainerT>
77 inline void
write(const GridPtrContainerT & container,const MetaMap & metadata)78 Stream::write(const GridPtrContainerT& container, const MetaMap& metadata) const
79 {
80     GridCPtrVec grids;
81     std::copy(container.begin(), container.end(), std::back_inserter(grids));
82     this->write(grids, metadata);
83 }
84 
85 } // namespace io
86 } // namespace OPENVDB_VERSION_NAME
87 } // namespace openvdb
88 
89 #endif // OPENVDB_IO_STREAM_HAS_BEEN_INCLUDED
90