Add Data::LoadFromStream(stream, size)

This commit is contained in:
Sebastian Messmer 2015-10-25 11:32:40 +01:00
parent ea30ac641b
commit f88221a0c6
2 changed files with 6 additions and 4 deletions

View File

@ -31,9 +31,7 @@ std::streampos Data::_getStreamSize(istream &stream) {
return endpos - current_pos; return endpos - current_pos;
} }
Data Data::LoadFromStream(istream &stream) { Data Data::LoadFromStream(istream &stream, size_t size) {
size_t size = _getStreamSize(stream);
Data result(size); Data result(size);
stream.read(static_cast<char*>(result.data()), result.size()); stream.read(static_cast<char*>(result.data()), result.size());
return std::move(result); return std::move(result);

View File

@ -37,6 +37,7 @@ public:
//TODO Test LoadFromStream/StoreToStream //TODO Test LoadFromStream/StoreToStream
static Data LoadFromStream(std::istream &stream); static Data LoadFromStream(std::istream &stream);
static Data LoadFromStream(std::istream &stream, size_t size);
void StoreToStream(std::ostream &stream) const; void StoreToStream(std::ostream &stream) const;
private: private:
@ -122,11 +123,14 @@ inline void Data::StoreToFile(const boost::filesystem::path &filepath) const {
StoreToStream(file); StoreToStream(file);
} }
inline void Data::StoreToStream(std::ostream &stream) const { inline void Data::StoreToStream(std::ostream &stream) const {
stream.write(static_cast<const char*>(_data), _size); stream.write(static_cast<const char*>(_data), _size);
} }
inline Data Data::LoadFromStream(std::istream &stream) {
return LoadFromStream(stream, _getStreamSize(stream));
}
inline bool operator==(const Data &lhs, const Data &rhs) { inline bool operator==(const Data &lhs, const Data &rhs) {
return lhs.size() == rhs.size() && 0 == memcmp(lhs.data(), rhs.data(), lhs.size()); return lhs.size() == rhs.size() && 0 == memcmp(lhs.data(), rhs.data(), lhs.size());
} }