Stream.h 4.6 KB

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