Created
May 26, 2013 09:47
-
-
Save xicalango/5652246 to your computer and use it in GitHub Desktop.
MemoryDataStream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "MemoryDataStream.hpp" | |
#include <iostream> | |
struct bla { | |
int a; | |
int b; | |
char c[10]; | |
}; | |
int main() { | |
bla b = { 5, 6, "abc" }; | |
bla b2 = { 6, 7, "def" }; | |
bla b3; | |
bla b4; | |
char dest[1024] = {0}; | |
dbi::MemoryDataStream* mds = new dbi::MemoryDataStream(dest, 1024); | |
mds->write( &b, sizeof(bla) ); | |
mds->write( &b2, sizeof(bla) ); | |
delete mds; | |
mds = new dbi::MemoryDataStream(dest, 1024); | |
mds->copy( &b3, sizeof(bla) ); | |
mds->copy( &b4, sizeof(bla) ); | |
std::cout << b3.a << "," << b3.b << "," << b3.c << std::endl; | |
std::cout << b4.a << "," << b4.b << "," << b4.c << std::endl; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: MemoryDataStream.cpp | |
* Author: alexx | |
* | |
* Created on 24. Mai 2013, 13:15 | |
*/ | |
#include <cstring> | |
#include <cassert> | |
//#include <glog/logging.h> | |
#include "MemoryDataStream.hpp" | |
namespace dbi { | |
MemoryDataStream::MemoryDataStream(void* data, uint64_t length) | |
: _data(data) | |
, _length(length) | |
, _offset(0) { | |
} | |
MemoryDataStream::~MemoryDataStream() {} | |
bool MemoryDataStream::hasNext(uint64_t numBytes) { | |
return _length - _offset >= numBytes; | |
} | |
void* MemoryDataStream::read(uint64_t numBytes) { | |
if( !hasNext(numBytes) ) { | |
assert(false); | |
// LOG(FATAL) << "Out Of Bounds"; | |
} | |
uint64_t oldOffset = _offset; | |
_offset += numBytes; | |
return (char*)_data + oldOffset; | |
} | |
void MemoryDataStream::write(void* data, uint64_t numBytes) { | |
if( !hasNext(numBytes) ) { | |
assert(false); | |
// LOG(FATAL) << "Out Of Bounds"; | |
} | |
memcpy( (char*)_data + _offset, data, numBytes ); | |
_offset += numBytes; | |
} | |
void MemoryDataStream::setPos(uint64_t offset) { | |
_offset = offset; | |
} | |
void MemoryDataStream::skip(uint64_t offset) { | |
_offset += offset; | |
} | |
void* MemoryDataStream::copy( void* dest, uint64_t numBytes ) { | |
return memcpy( dest, this->read(numBytes), numBytes ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* File: MemoryDataStream.hpp | |
* Author: alexx | |
* | |
* Created on 24. Mai 2013, 13:15 | |
*/ | |
#ifndef MEMORYDATASTREAM_HPP | |
#define MEMORYDATASTREAM_HPP | |
#include <cstdint> | |
namespace dbi { | |
class MemoryDataStream { | |
public: | |
MemoryDataStream(void* data, uint64_t length); | |
virtual ~MemoryDataStream(); | |
private: | |
void* _data; | |
uint64_t _length; | |
uint64_t _offset; | |
public: | |
bool hasNext(uint64_t numBytes = 1); | |
void* read(uint64_t numBytes); | |
void write(void* data, uint64_t numBytes); | |
void* copy( void* dest, uint64_t numBytes ); | |
void setPos(uint64_t offset); | |
void skip(uint64_t offset); | |
}; | |
} | |
#endif /* MEMORYDATASTREAM_HPP */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment