OpenVDB  2.1.0
Stream.h
Go to the documentation of this file.
1 //
3 // Copyright (c) 2012-2013 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 //
30 
31 #ifndef OPENVDB_IO_STREAM_HAS_BEEN_INCLUDED
32 #define OPENVDB_IO_STREAM_HAS_BEEN_INCLUDED
33 
34 #include <iosfwd>
35 #include "Archive.h"
36 
37 
38 namespace openvdb {
40 namespace OPENVDB_VERSION_NAME {
41 namespace io {
42 
43 class GridDescriptor;
44 
45 
47 class OPENVDB_API Stream: public Archive
48 {
49 public:
51  explicit Stream(std::istream&);
52 
54  Stream();
56  explicit Stream(std::ostream&);
57 
58  virtual ~Stream();
59 
61  virtual boost::shared_ptr<Archive> copy() const;
62 
64  MetaMap::Ptr getMetadata() const;
65 
67  GridPtrVecPtr getGrids() { return mGrids; }
68 
71  virtual void write(const GridCPtrVec&, const MetaMap& = MetaMap()) const;
72 
75  template<typename GridPtrContainerT>
76  void write(const GridPtrContainerT&, const MetaMap& = MetaMap()) const;
77 
80  template<typename GridPtrContainerT>
81  OPENVDB_DEPRECATED void write(std::ostream&,
82  const GridPtrContainerT&, const MetaMap& = MetaMap()) const;
83 
84 private:
88  GridBase::Ptr readGrid(const GridDescriptor&, std::istream&) const;
89 
90  void writeGrids(std::ostream&, const GridCPtrVec&, const MetaMap&) const;
91 
92 
93  MetaMap::Ptr mMeta;
94  GridPtrVecPtr mGrids;
95  std::ostream* mOutputStream;
96 };
97 
98 
100 
101 
102 inline void
103 Stream::write(const GridCPtrVec& grids, const MetaMap& metadata) const
104 {
105  if (mOutputStream == NULL) {
106  OPENVDB_THROW(ValueError, "no output stream was specified");
107  }
108  this->writeGrids(*mOutputStream, grids, metadata);
109 }
110 
111 
112 template<typename GridPtrContainerT>
113 inline void
114 Stream::write(const GridPtrContainerT& container, const MetaMap& metadata) const
115 {
116  if (mOutputStream == NULL) {
117  OPENVDB_THROW(ValueError, "no output stream was specified");
118  }
119  GridCPtrVec grids;
120  std::copy(container.begin(), container.end(), std::back_inserter(grids));
121  this->writeGrids(*mOutputStream, grids, metadata);
122 }
123 
124 
125 template<typename GridPtrContainerT>
126 inline void
127 Stream::write(std::ostream& os, const GridPtrContainerT& container,
128  const MetaMap& metadata) const
129 {
130  GridCPtrVec grids;
131  std::copy(container.begin(), container.end(), std::back_inserter(grids));
132  this->writeGrids(os, grids, metadata);
133 }
134 
135 template<>
136 inline void
137 Stream::write<GridCPtrVec>(std::ostream& os, const GridCPtrVec& grids,
138  const MetaMap& metadata) const
139 {
140  this->writeGrids(os, grids, metadata);
141 }
142 
143 } // namespace io
144 } // namespace OPENVDB_VERSION_NAME
145 } // namespace openvdb
146 
147 #endif // OPENVDB_IO_STREAM_HAS_BEEN_INCLUDED
148 
149 // Copyright (c) 2012-2013 DreamWorks Animation LLC
150 // All rights reserved. This software is distributed under the
151 // Mozilla Public License 2.0 ( http://www.mozilla.org/MPL/2.0/ )
#define OPENVDB_API
Helper macros for defining library symbol visibility.
Definition: Platform.h:162
#define OPENVDB_DEPRECATED
Definition: Platform.h:47
#define OPENVDB_THROW(exception, message)
Definition: Exceptions.h:97
Definition: Exceptions.h:88
boost::shared_ptr< GridPtrVec > GridPtrVecPtr
Definition: Grid.h:399
boost::shared_ptr< GridBase > Ptr
Definition: Grid.h:106
#define OPENVDB_VERSION_NAME
Definition: version.h:45
GridPtrVecPtr getGrids()
Return pointers to the grids that were read from the input stream.
Definition: Stream.h:67
std::vector< GridBase::ConstPtr > GridCPtrVec
Definition: Grid.h:401
Provides functionality storing type agnostic metadata information. Grids and other structures can inh...
Definition: MetaMap.h:48
boost::shared_ptr< MetaMap > Ptr
Definition: MetaMap.h:51
Grid archive associated with arbitrary input and output streams (not necessarily files) ...
Definition: Stream.h:47
Grid serializer/unserializer.
Definition: Archive.h:113
#define OPENVDB_USE_VERSION_NAMESPACE
Definition: version.h:67
Definition: GridDescriptor.h:46