20 #if !defined(__MITSUBA_CORE_STREAM_H_)
21 #define __MITSUBA_CORE_STREAM_H_
23 #include <mitsuba/mitsuba.h>
32 : std::runtime_error(str), m_completed(completed) { }
64 ENetworkByteOrder = EBigEndian
77 virtual std::string
toString()
const;
84 void setByteOrder(EByteOrder byteOrder);
104 virtual void read(
void *ptr,
size_t size) = 0;
111 virtual void write(
const void *ptr,
size_t size) = 0;
114 virtual void seek(
size_t pos) = 0;
117 virtual void truncate(
size_t size) = 0;
120 virtual size_t getPos()
const = 0;
123 virtual size_t getSize()
const = 0;
126 virtual void flush() = 0;
129 virtual bool canWrite()
const = 0;
132 virtual bool canRead()
const = 0;
142 void skip(
size_t amount);
145 void writeString(
const std::string &value);
148 void writeLine(
const std::string &value);
151 void writeShort(
short value);
154 void writeShortArray(
const short *values,
size_t size);
159 writeShortArray(&values[0], N);
163 void writeUShort(
unsigned short value);
166 void writeUShortArray(
const unsigned short *values,
size_t size);
171 writeUShortArray(&values[0], N);
175 void writeInt(
int value);
178 void writeIntArray(
const int *values,
size_t size);
183 writeIntArray(&values[0], N);
187 void writeUInt(
unsigned int value);
190 void writeUIntArray(
const unsigned int *values,
size_t size);
195 writeUIntArray(&values[0], N);
199 void writeLong(int64_t value);
202 void writeLongArray(
const int64_t *values,
size_t size);
207 writeLongArray(&values[0], N);
211 void writeULong(uint64_t value);
214 void writeSize(
size_t value) { writeULong((uint64_t) value); }
217 void writeULongArray(
const uint64_t *values,
size_t size);
222 writeULongArray(&values[0], N);
226 void writeChar(
char value);
229 void writeUChar(
unsigned char value);
232 inline void writeBool(
bool value) { writeUChar(value); }
235 void writeHalf(
half value);
238 void writeHalfArray(
const half *data,
size_t size);
243 writeHalfArray(&values[0], N);
247 void writeSingle(
float value);
250 void writeSingleArray(
const float *data,
size_t size);
255 writeSingleArray(&values[0], N);
259 void writeDouble(
double value);
262 void writeDoubleArray(
const double *data,
size_t size);
267 writeDoubleArray(&values[0], N);
272 #ifdef SINGLE_PRECISION
281 #ifdef SINGLE_PRECISION
282 writeSingleArray(data, size);
284 writeDoubleArray(data, size);
291 writeFloatArray(&values[0], N);
298 std::string readLine();
301 std::string readString();
307 void readShortArray(
short *dest,
size_t size);
312 readShortArray(&values[0], N);
316 unsigned short readUShort();
319 void readUShortArray(
unsigned short *dest,
size_t size);
324 readUShortArray(&values[0], N);
331 void readIntArray(
int *dst,
size_t size);
336 readIntArray(&values[0], N);
340 unsigned int readUInt();
343 void readUIntArray(
unsigned int *dest,
size_t size);
348 readUIntArray(&values[0], N);
355 void readLongArray(int64_t *dst,
size_t size);
360 readLongArray(&values[0], N);
364 uint64_t readULong();
370 void readULongArray(uint64_t *dst,
size_t size);
375 readULongArray(&values[0], N);
382 unsigned char readUChar();
385 inline bool readBool() {
return static_cast<bool> (readUChar()); }
391 void readHalfArray(
half *data,
size_t size);
396 readHalfArray(&values[0], N);
403 void readSingleArray(
float *data,
size_t size);
408 readSingleArray(&values[0], N);
415 void readDoubleArray(
double *data,
size_t size);
420 readDoubleArray(&values[0], N);
425 #ifdef SINGLE_PRECISION
434 #ifdef SINGLE_PRECISION
435 readSingleArray(data, size);
437 readDoubleArray(data, size);
444 readFloatArray(&values[0], N);
454 void copyTo(
Stream *stream, int64_t numBytes = -1);
460 template <
typename T> T readElement();
466 template <
typename T>
void writeElement(T value);
472 template <
typename T>
void readArray(T *array,
size_t count);
478 template <
typename T,
size_t N>
inline void readArray(T (&arr)[N]) {
479 readArray(&arr[0], N);
486 template <
typename T>
void writeArray(
const T *array,
size_t count);
492 template <
typename T,
size_t N>
inline void writeArray(
const T (&arr)[N]) {
493 writeArray(&arr[0], N);
503 static EByteOrder m_hostByteOrder;
504 EByteOrder m_byteOrder;
508 template <
typename T>
inline T Stream::readElement() {
509 Log(
EError,
"Stream::readElement<T>: not implemented!");
512 template <
typename T>
inline void Stream::writeElement(T value) {
513 Log(
EError,
"Stream::writeElement<T>: not implemented!");
516 template <
typename T>
inline void Stream::readArray(T *array,
size_t count) {
517 Log(
EError,
"Stream::readArray<T>: not implemented!");
520 template <
typename T>
inline void Stream::writeArray(
const T *array,
size_t count) {
521 Log(
EError,
"Stream::writeArray<T>: not implemented!");
525 template <>
inline half Stream::readElement() {
return readHalf(); }
526 template <>
inline float Stream::readElement() {
return readSingle(); }
527 template <>
inline double Stream::readElement() {
return readDouble(); }
528 template <>
inline char Stream::readElement() {
return readChar(); }
529 template <>
inline unsigned char Stream::readElement() {
return readUChar(); }
530 template <>
inline bool Stream::readElement() {
return readBool(); }
531 template <>
inline int16_t Stream::readElement() {
return readShort(); }
532 template <>
inline uint16_t Stream::readElement() {
return readUShort(); }
533 template <>
inline int32_t Stream::readElement() {
return readInt(); }
534 template <>
inline uint32_t Stream::readElement() {
return readUInt(); }
535 template <>
inline int64_t Stream::readElement() {
return readLong(); }
536 template <>
inline uint64_t Stream::readElement() {
return readULong(); }
537 template <>
inline std::string Stream::readElement() {
return readString(); }
539 template <>
inline void Stream::writeElement(
half val) {
return writeHalf(val); }
540 template <>
inline void Stream::writeElement(
float val) {
return writeSingle(val); }
541 template <>
inline void Stream::writeElement(
double val) {
return writeDouble(val); }
542 template <>
inline void Stream::writeElement(
char val) {
return writeChar(val); }
543 template <>
inline void Stream::writeElement(
unsigned char val) {
return writeUChar(val); }
544 template <>
inline void Stream::writeElement(
bool val) {
return writeBool(val); }
545 template <>
inline void Stream::writeElement(int16_t val) {
return writeShort(val); }
546 template <>
inline void Stream::writeElement(uint16_t val) {
return writeUShort(val); }
547 template <>
inline void Stream::writeElement(int32_t val) {
return writeInt(val); }
548 template <>
inline void Stream::writeElement(
uint32_t val) {
return writeUInt(val); }
549 template <>
inline void Stream::writeElement(int64_t val) {
return writeLong(val); }
550 template <>
inline void Stream::writeElement(uint64_t val) {
return writeULong(val); }
551 template <>
inline void Stream::writeElement(
const std::string &val) {
return writeString(val); }
553 template <>
inline void Stream::readArray(
half *array,
size_t count) {
return readHalfArray(array, count); }
554 template <>
inline void Stream::readArray(
float *array,
size_t count) {
return readSingleArray(array, count); }
555 template <>
inline void Stream::readArray(
double *array,
size_t count) {
return readDoubleArray(array, count); }
556 template <>
inline void Stream::readArray(
char *array,
size_t count) {
return read((
uint8_t *) array, count); }
557 template <>
inline void Stream::readArray(
unsigned char *array,
size_t count) {
return read((
uint8_t *) array, count); }
558 template <>
inline void Stream::readArray(
bool *array,
size_t count) {
return read((
uint8_t *) array, count); }
559 template <>
inline void Stream::readArray(int16_t *array,
size_t count) {
return readShortArray(array, count); }
560 template <>
inline void Stream::readArray(uint16_t *array,
size_t count) {
return readUShortArray(array, count); }
561 template <>
inline void Stream::readArray(int32_t *array,
size_t count) {
return readIntArray(array, count); }
562 template <>
inline void Stream::readArray(
uint32_t *array,
size_t count) {
return readUIntArray(array, count); }
563 template <>
inline void Stream::readArray(int64_t *array,
size_t count) {
return readLongArray(array, count); }
564 template <>
inline void Stream::readArray(uint64_t *array,
size_t count) {
return readULongArray(array, count); }
566 template <>
inline void Stream::writeArray(
const half *array,
size_t count) {
return writeHalfArray(array, count); }
567 template <>
inline void Stream::writeArray(
const float *array,
size_t count) {
return writeSingleArray(array, count); }
568 template <>
inline void Stream::writeArray(
const double *array,
size_t count) {
return writeDoubleArray(array, count); }
569 template <>
inline void Stream::writeArray(
const char *array,
size_t count) {
return write((
uint8_t *) array, count); }
570 template <>
inline void Stream::writeArray(
const unsigned char *array,
size_t count) {
return write((
uint8_t *) array, count); }
571 template <>
inline void Stream::writeArray(
const bool *array,
size_t count) {
return write((
uint8_t *) array, count); }
572 template <>
inline void Stream::writeArray(
const int16_t *array,
size_t count) {
return writeShortArray(array, count); }
573 template <>
inline void Stream::writeArray(
const uint16_t *array,
size_t count) {
return writeUShortArray(array, count); }
574 template <>
inline void Stream::writeArray(
const int32_t *array,
size_t count) {
return writeIntArray(array, count); }
575 template <>
inline void Stream::writeArray(
const uint32_t *array,
size_t count) {
return writeUIntArray(array, count); }
576 template <>
inline void Stream::writeArray(
const int64_t *array,
size_t count) {
return writeLongArray(array, count); }
577 template <>
inline void Stream::writeArray(
const uint64_t *array,
size_t count) {
return writeULongArray(array, count); }
bool readBool()
Read a boolean (8 bit) from the stream.
Definition: stream.h:385
void writeArray(const T(&arr)[N])
Write a known-size array to the stream (uses partial template specialization to select a method appro...
Definition: stream.h:492
void readUShortArray(short(&values)[N])
Read an array of known size of unsigned shorts (16 bit) from the stream.
Definition: stream.h:323
EByteOrder getByteOrder() const
Return the byte order of this stream.
Definition: stream.h:87
void readFloatArray(Float(&values)[N])
Read a known-size array of floating point values (configured precision) to the stream.
Definition: stream.h:443
void readLongArray(int64_t(&values)[N])
Read an array of known size of signed ints (64 bit) from the stream.
Definition: stream.h:359
void writeDoubleArray(const double(&values)[N])
Write a known size double-precision floating point array (64 bit) to the stream.
Definition: stream.h:266
void readShortArray(short(&values)[N])
Read an array of known size of signed shorts (16 bit) from the stream.
Definition: stream.h:311
void writeShortArray(const short(&values)[N])
Write an array of known size of signed shorts (16 bit) to the stream.
Definition: stream.h:158
void writeHalfArray(const half(&values)[N])
Write a known size half-precision floating point array (16 bit) to the stream.
Definition: stream.h:242
void writeIntArray(const int(&values)[N])
Write an array of known size of signed ints (32 bit) to the stream.
Definition: stream.h:182
void readSingleArray(float(&values)[N])
Read a known-size single-precision floating point array (32 bit) from the stream. ...
Definition: stream.h:407
#define MTS_EXPORT_CORE
Definition: getopt.h:29
Float readFloat()
Write a floating point number (configured precision) to the stream.
Definition: stream.h:424
EOFException(const std::string &str, size_t completed)
Definition: stream.h:31
std::ostream & operator<<(std::ostream &out, const DScalar1< Scalar, VecType > &s)
Definition: autodiff.h:392
void readDoubleArray(double(&values)[N])
Read a known-size double-precision floating point array (64 bit) from the stream. ...
Definition: stream.h:419
void readArray(T(&arr)[N])
Read a known-size array from the stream (uses partial template specialization to select a method appr...
Definition: stream.h:478
void writeSingleArray(const float(&values)[N])
Write a known size single-precision floating point array (32 bit) to the stream.
Definition: stream.h:254
void writeLongArray(const int64_t(&values)[N])
Write an array of known size of signed ints (64 bit) to the stream.
Definition: stream.h:206
void writeFloatArray(const Float *data, size_t size)
Write an array of floating point values (configured precision) to the stream.
Definition: stream.h:280
void readULongArray(uint64_t(&values)[N])
Read an array of known size of unsigned ints (64 bit) from the stream.
Definition: stream.h:374
void readHalfArray(half(&values)[N])
Read a known-size half-precision floating point array (16 bit) from the stream.
Definition: stream.h:395
size_t getCompleted() const
Return the number of bytes that successfully completed.
Definition: stream.h:35
#define Log(level, fmt,...)
Write a Log message to the console (to be used within subclasses of Object)
Definition: logger.h:35
void writeFloat(Float value)
Write a floating point number (configured precision) to the stream.
Definition: stream.h:271
Abstract seekable stream class.
Definition: stream.h:58
#define MTS_DECLARE_CLASS()
This macro must be used in the initial definition in classes that derive from Object.
Definition: class.h:158
void writeUIntArray(const unsigned int(&values)[N])
Write an array of known size of unsigned ints (32 bit) to the stream.
Definition: stream.h:194
void readFloatArray(Float *data, size_t size)
Write an array of floating point values (configured precision) to the stream.
Definition: stream.h:433
Error message, causes an exception to be thrown.
Definition: formatter.h:33
void readUIntArray(int(&values)[N])
Read an array of known size of unsigned ints (32 bit) from the stream.
Definition: stream.h:347
void writeUShortArray(const unsigned short(&values)[N])
Write an array of known size of unsigned shorts (16 bit) to the stream.
Definition: stream.h:170
static EByteOrder getHostByteOrder()
Return the byte order of the underlying machine.
Definition: stream.h:90
void writeFloatArray(const Float(&values)[N])
Write a known size array of floating point values (configured precision) to the stream.
Definition: stream.h:290
void writeULongArray(const uint64_t(&values)[N])
Write an array of known size of unsigned ints (64 bit) to the stream.
Definition: stream.h:221
void readIntArray(int(&values)[N])
Read an array of known size of signed ints (32 bit) from the stream.
Definition: stream.h:335
Parent of all Mitsuba classes.
Definition: object.h:38
EByteOrder
Defines the byte order to use in this Stream.
Definition: stream.h:61
virtual std::string toString() const
Return a human-readable string representation of the object's contents.
void writeBool(bool value)
Write a boolean (8 bit) to the stream.
Definition: stream.h:232
void writeSize(size_t value)
Write a size value to the stream.
Definition: stream.h:214
size_t readSize()
Read a size value from the stream.
Definition: stream.h:367
This exception is thrown after an incomplete IO read/write operation.
Definition: stream.h:29