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 protected:
99  IMF_EXPORT IStream (const char fileName[]);
100 
101 private:
102  IStream (const IStream&) = delete;
103  IStream& operator= (const IStream&) = delete;
104  IStream (IStream&&) = delete;
105  IStream& operator= (IStream&&) = delete;
106 
107  std::string _fileName;
108 };
109 
110 //-----------------------------------------------------------
111 // class OStream -- an abstract base class for output streams
112 //-----------------------------------------------------------
113 
115 {
116 public:
117  //-----------
118  // Destructor
119  //-----------
120 
121  IMF_EXPORT virtual ~OStream ();
122 
123  //----------------------------------------------------------
124  // Write to the stream:
125  //
126  // write(c,n) takes n bytes from array c, and stores them
127  // in the stream. If an I/O error occurs, write(c,n) throws
128  // an exception.
129  //----------------------------------------------------------
130 
131  virtual void write (const char c[/*n*/], int n) = 0;
132 
133  //---------------------------------------------------------
134  // Get the current writing position, in bytes from the
135  // beginning of the file. If the next call to write() will
136  // start writing at the beginning of the file, tellp()
137  // returns 0.
138  //---------------------------------------------------------
139 
140  virtual uint64_t tellp () = 0;
141 
142  //-------------------------------------------
143  // Set the current writing position.
144  // After calling seekp(i), tellp() returns i.
145  //-------------------------------------------
146 
147  virtual void seekp (uint64_t pos) = 0;
148 
149  //------------------------------------------------------
150  // Get the name of the file associated with this stream.
151  //------------------------------------------------------
152 
153  IMF_EXPORT const char* fileName () const;
154 
155 protected:
156  IMF_EXPORT OStream (const char fileName[]);
157 
158 private:
159  OStream (const OStream&) = delete;
160  OStream& operator= (const OStream&) = delete;
161  OStream (OStream&&) = delete;
162  OStream& operator= (OStream&&) = delete;
163 
164  std::string _fileName;
165 };
166 
167 //-----------------------
168 // Helper classes for Xdr
169 //-----------------------
170 
171 struct StreamIO
172 {
173  static inline void writeChars (OStream& os, const char c[/*n*/], int n)
174  {
175  os.write (c, n);
176  }
177 
178  static inline bool readChars (IStream& is, char c[/*n*/], int n)
179  {
180  return is.read (c, n);
181  }
182 };
183 
184 struct CharPtrIO
185 {
186  static inline void writeChars (char*& op, const char c[/*n*/], int n)
187  {
188  while (n--)
189  *op++ = *c++;
190  }
191 
192  static inline bool readChars (const char*& ip, char c[/*n*/], int n)
193  {
194  while (n--)
195  *c++ = *ip++;
196 
197  return true;
198  }
199 };
200 
202 
203 #endif
static void writeChars(OStream &os, const char c[], int n)
Definition: ImfIO.h:173
Definition: ImfIO.h:26
#define OPENEXR_IMF_INTERNAL_NAMESPACE_HEADER_EXIT
Definition: ImfNamespace.h:83
GLsizei const GLchar *const * string
Definition: glcorearb.h:814
static bool readChars(IStream &is, char c[], int n)
Definition: ImfIO.h:178
Definition: ImfIO.h:114
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:192
GLdouble n
Definition: glcorearb.h:2008
#define IMF_EXPORT
Definition: ImfExport.h:54
static void writeChars(char *&op, const char c[], int n)
Definition: ImfIO.h:186
class IMF_EXPORT_TYPE OStream
Definition: ImfForward.h:86
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