OpenVDB  9.0.1
Stream.h
Go to the documentation of this file.
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 {
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
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
#define OPENVDB_API
Definition: Platform.h:254
SharedPtr< GridBase > Ptr
Definition: Grid.h:80
static fileSize_t write(std::ostream &os, const GridHandle< BufferT > &handle, Codec codec)
SharedPtr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:517
Grid serializer/unserializer.
Definition: Archive.h:32
SharedPtr< Archive > Ptr
Definition: Archive.h:35
Container that maps names (strings) to values of arbitrary types.
Definition: MetaMap.h:19
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:519
SharedPtr< MetaMap > Ptr
Definition: MetaMap.h:22
Definition: Exceptions.h:13
Grid archive associated with arbitrary input and output streams (not necessarily files) ...
Definition: Stream.h:21
GridHandle< BufferT > readGrid(const std::string &fileName, uint64_t n=0, int verbose=0, const BufferT &buffer=BufferT())
Read the n&#39;th grid from file (defaults to first grid)
Definition: IO.h:576
#define OPENVDB_VERSION_NAME
The version namespace name for this library version.
Definition: version.h.in:116
void writeGrids(const std::string &fileName, const VecT< GridHandle< BufferT >> &handles, Codec codec=Codec::NONE, int verbose=0)
Write multiple grids to file (over-writing existing content of the file)
Definition: IO.h:543
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h.in:202
Definition: GridDescriptor.h:19