HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ImfIO.h
Go to the documentation of this file.
1 //
2 // SPDX-License-Identifier: BSD-3-Clause
3 // Copyright (c) Contributors to the OpenEXR Project.
4 //
5 
6 #ifndef INCLUDED_IMF_IO_H
7 #define INCLUDED_IMF_IO_H
8 
9 //-----------------------------------------------------------------------------
10 //
11 // Low-level file input and output for OpenEXR.
12 //
13 //-----------------------------------------------------------------------------
14 
15 #include "ImfForward.h"
16 
17 #include <cstdint>
18 #include <string>
19 
21 
22 //-----------------------------------------------------------
23 // class IStream -- an abstract base class for input streams.
24 //-----------------------------------------------------------
25 
27 {
28 public:
29  //-----------
30  // Destructor
31  //-----------
32 
33  IMF_EXPORT virtual ~IStream ();
34 
35  //-------------------------------------------------
36  // Does this input stream support memory-mapped IO?
37  //
38  // Memory-mapped streams can avoid an extra copy;
39  // memory-mapped read operations return a pointer
40  // to an internal buffer instead of copying data
41  // into a buffer supplied by the caller.
42  //-------------------------------------------------
43 
44  IMF_EXPORT virtual bool isMemoryMapped () const;
45 
46  //------------------------------------------------------
47  // Read from the stream:
48  //
49  // read(c,n) reads n bytes from the stream, and stores
50  // them in array c. If the stream contains less than n
51  // bytes, or if an I/O error occurs, read(c,n) throws
52  // an exception. If read(c,n) reads the last byte from
53  // the file it returns false, otherwise it returns true.
54  //------------------------------------------------------
55 
56  virtual bool read (char c[/*n*/], int n) = 0;
57 
58  //---------------------------------------------------
59  // Read from a memory-mapped stream:
60  //
61  // readMemoryMapped(n) reads n bytes from the stream
62  // and returns a pointer to the first byte. The
63  // returned pointer remains valid until the stream
64  // is closed. If there are less than n byte left to
65  // read in the stream or if the stream is not memory-
66  // mapped, readMemoryMapped(n) throws an exception.
67  //---------------------------------------------------
68 
69  IMF_EXPORT virtual char* readMemoryMapped (int n);
70 
71  //--------------------------------------------------------
72  // Get the current reading position, in bytes from the
73  // beginning of the file. If the next call to read() will
74  // read the first byte in the file, tellg() returns 0.
75  //--------------------------------------------------------
76 
77  virtual uint64_t tellg () = 0;
78 
79  //-------------------------------------------
80  // Set the current reading position.
81  // After calling seekg(i), tellg() returns i.
82  //-------------------------------------------
83 
84  virtual void seekg (uint64_t pos) = 0;
85 
86  //------------------------------------------------------
87  // Clear error conditions after an operation has failed.
88  //------------------------------------------------------
89 
90  IMF_EXPORT virtual void clear ();
91 
92  //------------------------------------------------------
93  // Get the name of the file associated with this stream.
94  //------------------------------------------------------
95 
96  IMF_EXPORT const char* fileName () const;
97 
98  //------------------------------------------------------
99  // Get the size of the file (or buffer) associated with
100  // this stream.
101  //
102  // by default, this will return -1. That value will skip a few
103  // safety checks. However, if you provide the size, it will apply
104  // a number of file consistency checks as the file is read.
105  // ------------------------------------------------------
106 
107  IMF_EXPORT virtual int64_t size ();
108 
109  //-------------------------------------------------
110  // Does this input stream support stateless reading?
111  //
112  // Stateless reading allows multiple threads to
113  // read from the stream concurrently from different
114  // locations in the file
115  //-------------------------------------------------
116 
117  IMF_EXPORT virtual bool isStatelessRead () const;
118 
119  //------------------------------------------------------
120  // Read from the stream with an offset:
121  //
122  // read(b,s,o) should read up to sz bytes from the
123  // stream using something like pread or ReadFileEx with
124  // overlapped data at the provided offset in the stream.
125  //
126  // for this function, the buffer size requested may be
127  // either larger than the file or request a read past
128  // the end of the file. This should NOT be treated as
129  // an error - the library will handle whether that is
130  // an error (if the offset is past the end, it should
131  // read 0)
132  //
133  // If there is an error, it should either return -1
134  // or throw an exception (an exception could provide
135  // a message).
136  //
137  // This will only be used if isStatelessRead returns true.
138  //
139  // NB: It is expected that this is thread safe such
140  // that multiple threads can be reading from the stream
141  // at the same time
142  //------------------------------------------------------
143 
144  IMF_EXPORT virtual int64_t read (void *buf, uint64_t sz, uint64_t offset);
145 
146 protected:
147  IMF_EXPORT IStream (const char fileName[]);
148 
149 private:
150  IStream (const IStream&) = delete;
151  IStream& operator= (const IStream&) = delete;
152  IStream (IStream&&) = delete;
153  IStream& operator= (IStream&&) = delete;
154 
155  std::string _fileName;
156 };
157 
158 //-----------------------------------------------------------
159 // class OStream -- an abstract base class for output streams
160 //-----------------------------------------------------------
161 
163 {
164 public:
165  //-----------
166  // Destructor
167  //-----------
168 
169  IMF_EXPORT virtual ~OStream ();
170 
171  //----------------------------------------------------------
172  // Write to the stream:
173  //
174  // write(c,n) takes n bytes from array c, and stores them
175  // in the stream. If an I/O error occurs, write(c,n) throws
176  // an exception.
177  //----------------------------------------------------------
178 
179  virtual void write (const char c[/*n*/], int n) = 0;
180 
181  //---------------------------------------------------------
182  // Get the current writing position, in bytes from the
183  // beginning of the file. If the next call to write() will
184  // start writing at the beginning of the file, tellp()
185  // returns 0.
186  //---------------------------------------------------------
187 
188  virtual uint64_t tellp () = 0;
189 
190  //-------------------------------------------
191  // Set the current writing position.
192  // After calling seekp(i), tellp() returns i.
193  //-------------------------------------------
194 
195  virtual void seekp (uint64_t pos) = 0;
196 
197  //------------------------------------------------------
198  // Get the name of the file associated with this stream.
199  //------------------------------------------------------
200 
201  IMF_EXPORT const char* fileName () const;
202 
203 protected:
204  IMF_EXPORT OStream (const char fileName[]);
205 
206 private:
207  OStream (const OStream&) = delete;
208  OStream& operator= (const OStream&) = delete;
209  OStream (OStream&&) = delete;
210  OStream& operator= (OStream&&) = delete;
211 
212  std::string _fileName;
213 };
214 
215 //-----------------------
216 // Helper classes for Xdr
217 //-----------------------
218 
219 struct StreamIO
220 {
221  static inline void writeChars (OStream& os, const char c[/*n*/], int n)
222  {
223  os.write (c, n);
224  }
225 
226  static inline bool readChars (IStream& is, char c[/*n*/], int n)
227  {
228  return is.read (c, n);
229  }
230 };
231 
232 struct CharPtrIO
233 {
234  static inline void writeChars (char*& op, const char c[/*n*/], int n)
235  {
236  while (n--)
237  *op++ = *c++;
238  }
239 
240  static inline bool readChars (const char*& ip, char c[/*n*/], int n)
241  {
242  while (n--)
243  *c++ = *ip++;
244 
245  return true;
246  }
247 };
248 
250 
251 #endif
static void writeChars(OStream &os, const char c[], int n)
Definition: ImfIO.h:221
Definition: ImfIO.h:26
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:83
static bool readChars(IStream &is, char c[], int n)
Definition: ImfIO.h:226
Definition: ImfIO.h:162
virtual void write(const char c[], int n)=0
virtual bool read(char c[], int n)=0
static bool readChars(const char *&ip, char c[], int n)
Definition: ImfIO.h:240
GLdouble n
Definition: glcorearb.h:2008
GLintptr offset
Definition: glcorearb.h:665
#define IMF_EXPORT
Definition: ImfExport.h:54
static void writeChars(char *&op, const char c[], int n)
Definition: ImfIO.h:234
class IMF_EXPORT_TYPE OStream
Definition: ImfForward.h:86
GLsizeiptr size
Definition: glcorearb.h:664
LeafData & operator=(const LeafData &)=delete
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_ENTER
Definition: ImfNamespace.h:80
#define IMF_EXPORT_TYPE
Definition: ImfExport.h:57
class IMF_EXPORT_TYPE IStream
Definition: ImfForward.h:87