stream.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // This code is in the public domain -- Ignacio Castaño <[email protected]>
  2. #ifndef NV_CORE_STREAM_H
  3. #define NV_CORE_STREAM_H
  4. #include "nvcore.h"
  5. #include "debug.h"
  6. namespace nv
  7. {
  8. /// Base stream class.
  9. class NVCORE_CLASS Stream {
  10. public:
  11. enum ByteOrder {
  12. LittleEndian = false,
  13. BigEndian = true,
  14. };
  15. /// Get the byte order of the system.
  16. static ByteOrder getSystemByteOrder() {
  17. #if NV_LITTLE_ENDIAN
  18. return LittleEndian;
  19. #else
  20. return BigEndian;
  21. #endif
  22. }
  23. /// Ctor.
  24. Stream() : m_byteOrder(LittleEndian) { }
  25. /// Virtual destructor.
  26. virtual ~Stream() {}
  27. /// Set byte order.
  28. void setByteOrder(ByteOrder bo) { m_byteOrder = bo; }
  29. /// Get byte order.
  30. ByteOrder byteOrder() const { return m_byteOrder; }
  31. /// Serialize the given data.
  32. virtual uint serialize( void * data, uint len ) = 0;
  33. /// Move to the given position in the archive.
  34. virtual void seek( uint pos ) = 0;
  35. /// Return the current position in the archive.
  36. virtual uint tell() const = 0;
  37. /// Return the current size of the archive.
  38. virtual uint size() const = 0;
  39. /// Determine if there has been any error.
  40. virtual bool isError() const = 0;
  41. /// Clear errors.
  42. virtual void clearError() = 0;
  43. /// Return true if the stream is at the end.
  44. virtual bool isAtEnd() const = 0;
  45. /// Return true if the stream is seekable.
  46. virtual bool isSeekable() const = 0;
  47. /// Return true if this is an input stream.
  48. virtual bool isLoading() const = 0;
  49. /// Return true if this is an output stream.
  50. virtual bool isSaving() const = 0;
  51. void advance(uint offset) { seek(tell() + offset); }
  52. // friends
  53. friend Stream & operator<<( Stream & s, bool & c ) {
  54. #if NV_OS_DARWIN && !NV_CC_CPP11
  55. nvStaticCheck(sizeof(bool) == 4);
  56. uint8 b = c ? 1 : 0;
  57. s.serialize( &b, 1 );
  58. c = (b == 1);
  59. #else
  60. nvStaticCheck(sizeof(bool) == 1);
  61. s.serialize( &c, 1 );
  62. #endif
  63. return s;
  64. }
  65. friend Stream & operator<<( Stream & s, char & c ) {
  66. nvStaticCheck(sizeof(char) == 1);
  67. s.serialize( &c, 1 );
  68. return s;
  69. }
  70. friend Stream & operator<<( Stream & s, uint8 & c ) {
  71. nvStaticCheck(sizeof(uint8) == 1);
  72. s.serialize( &c, 1 );
  73. return s;
  74. }
  75. friend Stream & operator<<( Stream & s, int8 & c ) {
  76. nvStaticCheck(sizeof(int8) == 1);
  77. s.serialize( &c, 1 );
  78. return s;
  79. }
  80. friend Stream & operator<<( Stream & s, uint16 & c ) {
  81. nvStaticCheck(sizeof(uint16) == 2);
  82. return s.byteOrderSerialize( &c, 2 );
  83. }
  84. friend Stream & operator<<( Stream & s, int16 & c ) {
  85. nvStaticCheck(sizeof(int16) == 2);
  86. return s.byteOrderSerialize( &c, 2 );
  87. }
  88. friend Stream & operator<<( Stream & s, uint32 & c ) {
  89. nvStaticCheck(sizeof(uint32) == 4);
  90. return s.byteOrderSerialize( &c, 4 );
  91. }
  92. friend Stream & operator<<( Stream & s, int32 & c ) {
  93. nvStaticCheck(sizeof(int32) == 4);
  94. return s.byteOrderSerialize( &c, 4 );
  95. }
  96. friend Stream & operator<<( Stream & s, uint64 & c ) {
  97. nvStaticCheck(sizeof(uint64) == 8);
  98. return s.byteOrderSerialize( &c, 8 );
  99. }
  100. friend Stream & operator<<( Stream & s, int64 & c ) {
  101. nvStaticCheck(sizeof(int64) == 8);
  102. return s.byteOrderSerialize( &c, 8 );
  103. }
  104. friend Stream & operator<<( Stream & s, float & c ) {
  105. nvStaticCheck(sizeof(float) == 4);
  106. return s.byteOrderSerialize( &c, 4 );
  107. }
  108. friend Stream & operator<<( Stream & s, double & c ) {
  109. nvStaticCheck(sizeof(double) == 8);
  110. return s.byteOrderSerialize( &c, 8 );
  111. }
  112. protected:
  113. /// Serialize in the stream byte order.
  114. Stream & byteOrderSerialize( void * v, uint len ) {
  115. if( m_byteOrder == getSystemByteOrder() ) {
  116. serialize( v, len );
  117. }
  118. else {
  119. for( uint i = len; i > 0; i-- ) {
  120. serialize( (uint8 *)v + i - 1, 1 );
  121. }
  122. }
  123. return *this;
  124. }
  125. private:
  126. ByteOrder m_byteOrder;
  127. };
  128. } // nv namespace
  129. #endif // NV_CORE_STREAM_H