Added Data::dataOffset() and made most Data functions inline

This commit is contained in:
Sebastian Messmer 2015-10-05 02:28:53 +02:00
parent 63fe94ffe6
commit 4b84f5d211
2 changed files with 86 additions and 67 deletions

View File

@ -1,6 +1,5 @@
#include "Data.h"
#include <stdexcept>
#include <fstream>
using std::istream;
using std::ofstream;
@ -11,63 +10,6 @@ namespace bf = boost::filesystem;
namespace cpputils {
Data::Data(size_t size)
: _size(size), _data(std::malloc(size)) {
if (nullptr == _data) {
throw std::bad_alloc();
}
}
Data::Data(Data &&rhs)
: _size(rhs._size), _data(rhs._data) {
// Make rhs invalid, so the memory doesn't get freed in its destructor.
rhs._data = nullptr;
rhs._size = 0;
}
Data &Data::operator=(Data &&rhs) {
std::free(_data);
_data = rhs._data;
_size = rhs._size;
rhs._data = nullptr;
rhs._size = 0;
return *this;
}
Data::~Data() {
std::free(_data);
_data = nullptr;
}
Data Data::copy() const {
Data copy(_size);
std::memcpy(copy._data, _data, _size);
return copy;
}
void *Data::data() {
return const_cast<void*>(const_cast<const Data*>(this)->data());
}
const void *Data::data() const {
return _data;
}
size_t Data::size() const {
return _size;
}
Data &Data::FillWithZeroes() {
std::memset(_data, 0, _size);
return *this;
}
void Data::StoreToFile(const bf::path &filepath) const {
ofstream file(filepath.c_str(), ios::binary | ios::trunc);
file.write((const char*)_data, _size);
}
boost::optional<Data> Data::LoadFromFile(const bf::path &filepath) {
ifstream file(filepath.c_str(), ios::binary);
if (!file.good()) {
@ -93,15 +35,9 @@ size_t Data::_getStreamSize(istream &stream) {
return endpos - current_pos;
}
void Data::_readFromStream(istream &stream) {
stream.read((char*)_data, _size);
}
bool operator==(const Data &lhs, const Data &rhs) {
return lhs.size() == rhs.size() && 0 == memcmp(lhs.data(), rhs.data(), lhs.size());
}
bool operator!=(const Data &lhs, const Data &rhs) {
return !operator==(lhs, rhs);
}
}

View File

@ -7,13 +7,14 @@
#include <boost/optional.hpp>
#include "../macros.h"
#include <memory>
#include <fstream>
namespace cpputils {
class Data {
class Data final {
public:
explicit Data(size_t size);
virtual ~Data();
~Data();
Data(Data &&rhs); // move constructor
Data &operator=(Data &&rhs); // move assignment
@ -23,6 +24,10 @@ public:
void *data();
const void *data() const;
//TODO Test dataOffset
void *dataOffset(size_t offset);
const void *dataOffset(size_t offset) const;
size_t size() const;
Data &FillWithZeroes();
@ -43,6 +48,84 @@ private:
bool operator==(const Data &lhs, const Data &rhs);
bool operator!=(const Data &lhs, const Data &rhs);
// ---------------------------
// Inline function definitions
// ---------------------------
inline Data::Data(size_t size)
: _size(size), _data(std::malloc(size)) {
if (nullptr == _data) {
throw std::bad_alloc();
}
}
inline Data::Data(Data &&rhs)
: _size(rhs._size), _data(rhs._data) {
// Make rhs invalid, so the memory doesn't get freed in its destructor.
rhs._data = nullptr;
rhs._size = 0;
}
inline Data &Data::operator=(Data &&rhs) {
std::free(_data);
_data = rhs._data;
_size = rhs._size;
rhs._data = nullptr;
rhs._size = 0;
return *this;
}
inline Data::~Data() {
std::free(_data);
_data = nullptr;
}
inline Data Data::copy() const {
Data copy(_size);
std::memcpy(copy._data, _data, _size);
return copy;
}
inline void *Data::data() {
return const_cast<void*>(const_cast<const Data*>(this)->data());
}
inline const void *Data::data() const {
return _data;
}
inline void *Data::dataOffset(size_t offset) {
return const_cast<void*>(const_cast<const Data*>(this)->dataOffset(offset));
}
inline const void *Data::dataOffset(size_t offset) const {
return static_cast<const uint8_t*>(data()) + offset;
}
inline size_t Data::size() const {
return _size;
}
inline Data &Data::FillWithZeroes() {
std::memset(_data, 0, _size);
return *this;
}
inline void Data::StoreToFile(const boost::filesystem::path &filepath) const {
std::ofstream file(filepath.c_str(), std::ios::binary | std::ios::trunc);
file.write((const char*)_data, _size);
}
inline bool operator==(const Data &lhs, const Data &rhs) {
return lhs.size() == rhs.size() && 0 == memcmp(lhs.data(), rhs.data(), lhs.size());
}
inline bool operator!=(const Data &lhs, const Data &rhs) {
return !operator==(lhs, rhs);
}
}
#endif