Cortex  10.0.0-a4
StreamIndexedIO.h
1 //
3 // Copyright (c) 2007-2013, Image Engine Design Inc. All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 //
12 // * Redistributions in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
15 //
16 // * Neither the name of Image Engine Design nor the names of any
17 // other contributors to this software may be used to endorse or
18 // promote products derived from this software without specific prior
19 // written permission.
20 //
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 //
34 
35 #ifndef IE_CORE_STREAMINDEXEDIO_H
36 #define IE_CORE_STREAMINDEXEDIO_H
37 
38 #include <map>
39 #include <iostream>
40 #include <fstream>
41 #include "tbb/recursive_mutex.h"
42 #include "boost/optional.hpp"
43 #include "boost/iostreams/filtering_stream.hpp"
44 
45 #include "IECore/Export.h"
46 #include "IECore/IndexedIO.h"
47 #include "IECore/Exception.h"
48 #include "IECore/VectorTypedData.h"
49 
50 namespace IECore
51 {
56 class IECORE_API StreamIndexedIO : public IndexedIO
57 {
58  public:
59 
60  IE_CORE_DECLARERUNTIMETYPED( StreamIndexedIO, IndexedIO );
61 
62  ~StreamIndexedIO() override;
63 
64  IndexedIO::OpenMode openMode() const override;
65 
66  void path( IndexedIO::EntryIDList &result ) const override;
67 
68  bool hasEntry( const IndexedIO::EntryID &name ) const override;
69 
70  const IndexedIO::EntryID &currentEntryId() const override;
71 
72  void entryIds( IndexedIO::EntryIDList &names ) const override;
73 
74  void entryIds( IndexedIO::EntryIDList &names, IndexedIO::EntryType type ) const override;
75 
76  IndexedIOPtr subdirectory( const IndexedIO::EntryID &name, IndexedIO::MissingBehaviour missingBehaviour = IndexedIO::ThrowIfMissing ) override;
77 
78  ConstIndexedIOPtr subdirectory( const IndexedIO::EntryID &name, IndexedIO::MissingBehaviour missingBehaviour = IndexedIO::ThrowIfMissing ) const override;
79 
80  IndexedIO::Entry entry( const IndexedIO::EntryID &name ) const override;
81 
82  IndexedIOPtr createSubdirectory( const IndexedIO::EntryID &name ) override;
83 
84  void remove( const IndexedIO::EntryID &name ) override;
85 
86  void removeAll() override;
87 
88  IndexedIOPtr parentDirectory() override;
89 
90  ConstIndexedIOPtr parentDirectory() const override;
91 
92  IndexedIOPtr directory( const IndexedIO::EntryIDList &path, IndexedIO::MissingBehaviour missingBehaviour = IndexedIO::ThrowIfMissing ) override;
93 
94  ConstIndexedIOPtr directory( const IndexedIO::EntryIDList &path, IndexedIO::MissingBehaviour missingBehaviour = IndexedIO::ThrowIfMissing ) const override;
95 
96  void commit() override;
97 
98  void write(const IndexedIO::EntryID &name, const float *x, unsigned long arrayLength) override;
99  void write(const IndexedIO::EntryID &name, const double *x, unsigned long arrayLength) override;
100  void write(const IndexedIO::EntryID &name, const half *x, unsigned long arrayLength) override;
101  void write(const IndexedIO::EntryID &name, const int *x, unsigned long arrayLength) override;
102  void write(const IndexedIO::EntryID &name, const int64_t *x, unsigned long arrayLength) override;
103  void write(const IndexedIO::EntryID &name, const uint64_t *x, unsigned long arrayLength) override;
104  void write(const IndexedIO::EntryID &name, const unsigned int *x, unsigned long arrayLength) override;
105  void write(const IndexedIO::EntryID &name, const char *x, unsigned long arrayLength) override;
106  void write(const IndexedIO::EntryID &name, const unsigned char *x, unsigned long arrayLength) override;
107  void write(const IndexedIO::EntryID &name, const std::string *x, unsigned long arrayLength) override;
108  void write(const IndexedIO::EntryID &name, const short *x, unsigned long arrayLength) override;
109  void write(const IndexedIO::EntryID &name, const unsigned short *x, unsigned long arrayLength) override;
110  void write(const IndexedIO::EntryID &name, const InternedString *x, unsigned long arrayLength) override;
111  void write(const IndexedIO::EntryID &name, const float &x) override;
112  void write(const IndexedIO::EntryID &name, const double &x) override;
113  void write(const IndexedIO::EntryID &name, const half &x) override;
114  void write(const IndexedIO::EntryID &name, const int &x) override;
115  void write(const IndexedIO::EntryID &name, const int64_t &x) override;
116  void write(const IndexedIO::EntryID &name, const uint64_t &x) override;
117  void write(const IndexedIO::EntryID &name, const std::string &x) override;
118  void write(const IndexedIO::EntryID &name, const unsigned int &x) override;
119  void write(const IndexedIO::EntryID &name, const char &x) override;
120  void write(const IndexedIO::EntryID &name, const unsigned char &x) override;
121  void write(const IndexedIO::EntryID &name, const short &x) override;
122  void write(const IndexedIO::EntryID &name, const unsigned short &x) override;
123 
124  void read(const IndexedIO::EntryID &name, float *&x, unsigned long arrayLength) const override;
125  void read(const IndexedIO::EntryID &name, double *&x, unsigned long arrayLength) const override;
126  void read(const IndexedIO::EntryID &name, half *&x, unsigned long arrayLength) const override;
127  void read(const IndexedIO::EntryID &name, int *&x, unsigned long arrayLength) const override;
128  void read(const IndexedIO::EntryID &name, int64_t *&x, unsigned long arrayLength) const override;
129  void read(const IndexedIO::EntryID &name, uint64_t *&x, unsigned long arrayLength) const override;
130  void read(const IndexedIO::EntryID &name, unsigned int *&x, unsigned long arrayLength) const override;
131  void read(const IndexedIO::EntryID &name, char *&x, unsigned long arrayLength) const override;
132  void read(const IndexedIO::EntryID &name, unsigned char *&x, unsigned long arrayLength) const override;
133  void read(const IndexedIO::EntryID &name, std::string *&x, unsigned long arrayLength) const override;
134  void read(const IndexedIO::EntryID &name, short *&x, unsigned long arrayLength) const override;
135  void read(const IndexedIO::EntryID &name, unsigned short *&x, unsigned long arrayLength) const override;
136  void read(const IndexedIO::EntryID &name, InternedString *&x, unsigned long arrayLength) const override;
137  void read(const IndexedIO::EntryID &name, float &x) const override;
138  void read(const IndexedIO::EntryID &name, double &x) const override;
139  void read(const IndexedIO::EntryID &name, half &x) const override;
140  void read(const IndexedIO::EntryID &name, int &x) const override;
141  void read(const IndexedIO::EntryID &name, int64_t &x) const override;
142  void read(const IndexedIO::EntryID &name, uint64_t &x) const override;
143  void read(const IndexedIO::EntryID &name, std::string &x) const override;
144  void read(const IndexedIO::EntryID &name, unsigned int &x) const override;
145  void read(const IndexedIO::EntryID &name, char &x) const override;
146  void read(const IndexedIO::EntryID &name, unsigned char &x) const override;
147  void read(const IndexedIO::EntryID &name, short &x) const override;
148  void read(const IndexedIO::EntryID &name, unsigned short &x) const override;
149 
150  protected:
151 
152  class Index;
153  IE_CORE_DECLAREPTR( Index );
154 
155  class Node;
156  IE_CORE_DECLAREPTR( Node );
157 
158  class StringCache;
159 
161  class StreamFile : public RefCounted
162  {
163  public:
164  ~StreamFile() override;
165 
166  void seekg( size_t pos, std::ios_base::seekdir dir );
167  void seekp( size_t pos, std::ios_base::seekdir dir );
168  void read( char *buffer, size_t size );
169  void write( const char *buffer, size_t size );
170  Imf::Int64 tellg();
171  Imf::Int64 tellp();
172 
173  IndexedIO::OpenMode openMode() const;
174 
175  // returns a read lock, when thread-safety is required.
176  typedef tbb::recursive_mutex Mutex;
177  typedef Mutex::scoped_lock MutexLock;
178  Mutex & mutex();
179 
180  // utility function that returns a temporary buffer for io operations (not thread safe).
181  char *ioBuffer( unsigned long size );
182 
184  virtual void flush( size_t endPosition );
185 
186  static bool canRead( std::iostream &stream );
187 
188  protected:
189 
190  StreamFile( IndexedIO::OpenMode mode );
192  // This function allocates and if in read-mode also reads the Index of the file.
193  void setStream( std::iostream *stream, bool emptyFile );
194 
195  IndexedIO::OpenMode m_openmode;
196  std::iostream *m_stream;
197  Mutex m_mutex;
198 
199  unsigned long m_ioBufferLen;
200  char *m_ioBuffer;
201  };
202  IE_CORE_DECLAREPTR( StreamFile );
203 
205  StreamIndexedIO();
206 
208  StreamIndexedIO( Node &node );
209 
211  void open( StreamFilePtr file, const IndexedIO::EntryIDList &root );
212 
215  void remove( const IndexedIO::EntryID &name, bool throwIfNonExistent );
216 
217  // Write an array of POD types
218  template<typename T>
219  void write(const IndexedIO::EntryID &name, const T *x, unsigned long arrayLength);
220 
221  // Write an array of POD types (without temporary buffers - used on little endian platforms)
222  template<typename T>
223  void rawWrite(const IndexedIO::EntryID &name, const T *x, unsigned long arrayLength);
224 
225  // Read an array of POD types
226  template<typename T>
227  void read(const IndexedIO::EntryID &name, T *&x, unsigned long arrayLength) const;
228 
229  // Read an array of POD types (without temporary buffers - used on little endian platforms)
230  template<typename T>
231  void rawRead(const IndexedIO::EntryID &name, T *&x, unsigned long arrayLength) const;
232 
233  // Write an instance of a type which is able to flatten itself.
234  template<typename T>
235  void write(const IndexedIO::EntryID &name, const T &x);
236 
237  // Write an instance of a type which is able to flatten itself (without temporary buffers - used on little endian platforms).
238  template<typename T>
239  void rawWrite(const IndexedIO::EntryID &name, const T &x);
240 
241  // Read an instance of a type which is able to unflatten itself.
242  template<typename T>
243  void read(const IndexedIO::EntryID &name, T &x) const;
244 
245  // Read an instance of a type which is able to unflatten itself (without temporary buffers - used on little endian platforms).
246  template<typename T>
247  void rawRead(const IndexedIO::EntryID &name, T &x) const;
248 
249  // Duplicates this object by mapping it to a different root node. Used when the subdirectory functions are called.
250  // This function does not duplicate the file handle like the public duplicate does. It works with any openMode.
251  virtual IndexedIO *duplicate(Node &rootNode) const = 0;
252 
255  void flush();
256 
257  StreamFile &streamFile() const;
258 
259  private :
260 
261  // \todo Either rename Node to MemberData or add it's member here and save one raw pointer.
262  Node *m_node;
263 
264  void setRoot( const IndexedIO::EntryIDList &root );
265 
266 };
267 
268 IE_CORE_DECLAREPTR( StreamIndexedIO )
269 
270 }
271 
272 #endif // IE_CORE_STREAMINDEXEDIO_H
Definition: IndexedIO.h:57
Definition: StreamIndexedIO.h:56
MissingBehaviour
Enum used to specify behavior when querying child directories.
Definition: IndexedIO.h:116
Class that provides access to the stream file.
Definition: StreamIndexedIO.h:161
Definition: InternedString.h:55
A representation of a single file/directory.
Definition: IndexedIO.h:492
Definition: RefCounted.h:124
This namespace contains all components of the core library.
Definition: AddSmoothSkinningInfluencesOp.h:43