| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2009, Google Inc. | 2 * Copyright 2009, Google Inc. |
| 3 * All rights reserved. | 3 * All rights reserved. |
| 4 * | 4 * |
| 5 * Redistribution and use in source and binary forms, with or without | 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are | 6 * modification, are permitted provided that the following conditions are |
| 7 * met: | 7 * met: |
| 8 * | 8 * |
| 9 * * Redistributions of source code must retain the above copyright | 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. | 10 * notice, this list of conditions and the following disclaimer. |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 48 | 48 |
| 49 #include <string.h> | 49 #include <string.h> |
| 50 #include "base/basictypes.h" | 50 #include "base/basictypes.h" |
| 51 | 51 |
| 52 namespace o3d { | 52 namespace o3d { |
| 53 | 53 |
| 54 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 54 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 55 class MemoryReadStream { | 55 class MemoryReadStream { |
| 56 public: | 56 public: |
| 57 MemoryReadStream(const uint8 *memory, size_t n) | 57 MemoryReadStream(const uint8 *memory, size_t n) |
| 58 : memory_(memory), length_(n), read_index_(0) {} | 58 : memory_(memory), read_index_(0), length_(n) {} |
| 59 | 59 |
| 60 // Explicit copy constructor | 60 // Explicit copy constructor |
| 61 MemoryReadStream(const MemoryReadStream &stream) | 61 MemoryReadStream(const MemoryReadStream &stream) |
| 62 : memory_(stream.memory_), | 62 : memory_(stream.memory_), |
| 63 length_(stream.length_), | 63 read_index_(stream.read_index_), |
| 64 read_index_(stream.read_index_) {} | 64 length_(stream.length_) {} |
| 65 | 65 |
| 66 virtual ~MemoryReadStream() {} | 66 virtual ~MemoryReadStream() {} |
| 67 | 67 |
| 68 // Similar to fread(), will try to copy |n| bytes to address |p| | 68 // Similar to fread(), will try to copy |n| bytes to address |p| |
| 69 // (copies fewer bytes if the stream doesn't have enough data) | 69 // (copies fewer bytes if the stream doesn't have enough data) |
| 70 // returns the number of bytes copied | 70 // returns the number of bytes copied |
| 71 virtual size_t Read(void *p, size_t n) { | 71 virtual size_t Read(void *p, size_t n) { |
| 72 size_t remaining = GetRemainingByteCount(); | 72 size_t remaining = GetRemainingByteCount(); |
| 73 size_t bytes_to_read = (n < remaining) ? n : remaining; | 73 size_t bytes_to_read = (n < remaining) ? n : remaining; |
| 74 memcpy(p, memory_ + read_index_, bytes_to_read); | 74 memcpy(p, memory_ + read_index_, bytes_to_read); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 void operator=(const MemoryReadStream&); | 172 void operator=(const MemoryReadStream&); |
| 173 }; | 173 }; |
| 174 | 174 |
| 175 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | 175 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 176 class MemoryWriteStream { | 176 class MemoryWriteStream { |
| 177 public: | 177 public: |
| 178 // May be completely initialized by calling Assign() | 178 // May be completely initialized by calling Assign() |
| 179 MemoryWriteStream() : memory_(NULL), write_index_(0), length_(0) {} | 179 MemoryWriteStream() : memory_(NULL), write_index_(0), length_(0) {} |
| 180 | 180 |
| 181 MemoryWriteStream(uint8 *memory, size_t n) | 181 MemoryWriteStream(uint8 *memory, size_t n) |
| 182 : memory_(memory), length_(n), write_index_(0) {} | 182 : memory_(memory), write_index_(0), length_(n) {} |
| 183 | 183 |
| 184 // Explicit copy constructor | 184 // Explicit copy constructor |
| 185 MemoryWriteStream(const MemoryWriteStream &stream) | 185 MemoryWriteStream(const MemoryWriteStream &stream) |
| 186 : memory_(stream.memory_), | 186 : memory_(stream.memory_), |
| 187 length_(stream.length_), | 187 write_index_(stream.write_index_), |
| 188 write_index_(stream.write_index_) {} | 188 length_(stream.length_) {} |
| 189 | 189 |
| 190 virtual ~MemoryWriteStream() {} | 190 virtual ~MemoryWriteStream() {} |
| 191 | 191 |
| 192 // In the case where the default constructor was used, this is useful | 192 // In the case where the default constructor was used, this is useful |
| 193 // to assign a pointer to memory and a length in bytes. | 193 // to assign a pointer to memory and a length in bytes. |
| 194 void Assign(uint8 *memory, size_t n) { | 194 void Assign(uint8 *memory, size_t n) { |
| 195 memory_ = memory; | 195 memory_ = memory; |
| 196 length_ = n; | 196 length_ = n; |
| 197 write_index_ = 0; | 197 write_index_ = 0; |
| 198 } | 198 } |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 class StreamProcessor { | 267 class StreamProcessor { |
| 268 public: | 268 public: |
| 269 virtual ~StreamProcessor() {} | 269 virtual ~StreamProcessor() {} |
| 270 virtual int ProcessBytes(MemoryReadStream *stream, | 270 virtual int ProcessBytes(MemoryReadStream *stream, |
| 271 size_t bytes_to_process) = 0; | 271 size_t bytes_to_process) = 0; |
| 272 }; | 272 }; |
| 273 | 273 |
| 274 } // namespace o3d | 274 } // namespace o3d |
| 275 | 275 |
| 276 #endif // O3D_IMPORT_CROSS_MEMORY_STREAM_H_ | 276 #endif // O3D_IMPORT_CROSS_MEMORY_STREAM_H_ |
| OLD | NEW |