diff options
author | John Ankarström <john@ankarstrom.se> | 2022-08-16 13:42:11 +0200 |
---|---|---|
committer | John Ankarström <john@ankarstrom.se> | 2022-08-16 13:42:11 +0200 |
commit | 1cb00589065fd05b8d7cf0030eed84c488e9634d (patch) | |
tree | f6054ff80e8c87d8a4b7a53dbfced30f05dd6dd2 /c/data.cpp | |
parent | 5095c2e2ba9aadf468514804fb3a872d10fdc17d (diff) | |
download | EpisodeBrowser-1cb00589065fd05b8d7cf0030eed84c488e9634d.tar.gz |
Avoid serialization.
A great benefit of this is that the program doesn't need to COPY the
data from the file view to the struct.
Diffstat (limited to 'c/data.cpp')
-rw-r--r-- | c/data.cpp | 33 |
1 files changed, 8 insertions, 25 deletions
@@ -18,7 +18,7 @@ inline unsigned char* BufToVal(unsigned char* const buf, T& val) return buf+sizeof(val); } -DatView::DatView(const wchar_t* const filename, const size_t cb) +FileView::FileView(const wchar_t* const filename, const size_t cb) { hf = CreateFile(filename, GENERIC_READ|GENERIC_WRITE, 0, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); @@ -39,12 +39,12 @@ DatView::DatView(const wchar_t* const filename, const size_t cb) if (!hm) throw Win32Error{}; - view = MapViewOfFile(hm, FILE_MAP_ALL_ACCESS, 0, 0, 0); + view = reinterpret_cast<unsigned char*>(MapViewOfFile(hm, FILE_MAP_ALL_ACCESS, 0, 0, 0)); if (!view) throw Win32Error{}; } -DatView::~DatView() +FileView::~FileView() { FlushViewOfFile(view, 0); UnmapViewOfFile(view); @@ -52,31 +52,14 @@ DatView::~DatView() CloseHandle(hf); } -unsigned char* Serialize(const ElvData& e, unsigned char* buf) +void Write(unsigned char* buf, const ElvData& e) { - unsigned char version = 'a'; - buf = ValToBuf(version, buf); - buf = ValToBuf(e.rating, buf); - buf = ValToBuf(e.bWatched, buf); - buf = ValToBuf(e.bTVOriginal, buf); - buf = ValToBuf(e.sRating, buf); - buf = ValToBuf(e.siEp, buf); - buf = ValToBuf(e.title, buf); - return buf; + memcpy(buf, reinterpret_cast<const unsigned char*>(&e), sizeof(e)); } -unsigned char* Unserialize(ElvData& e, unsigned char* buf) +ElvData* Read(unsigned char* const buf) { - unsigned char version; - buf = BufToVal(buf, version); - if (version != 'a') + if (buf[0] != 'a') return nullptr; - - buf = BufToVal(buf, e.rating); - buf = BufToVal(buf, e.bWatched); - buf = BufToVal(buf, e.bTVOriginal); - buf = BufToVal(buf, e.sRating); - buf = BufToVal(buf, e.siEp); - buf = BufToVal(buf, e.title); - return buf; + return reinterpret_cast<ElvData*>(buf); } |