From 55971b3ddef7db7c9c628223a80064a384d36081 Mon Sep 17 00:00:00 2001 From: Christopher Lackner Date: Sat, 29 Aug 2020 15:38:03 +0200 Subject: [PATCH] HashArchive --- libsrc/core/archive.hpp | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/libsrc/core/archive.hpp b/libsrc/core/archive.hpp index af7efc5e..76cb3567 100644 --- a/libsrc/core/archive.hpp +++ b/libsrc/core/archive.hpp @@ -910,6 +910,49 @@ namespace ngcore } }; + // HashArchive ================================================================= + // This class enables to easily create hashes for archivable objects by xoring + // threw its data + + class NGCORE_API HashArchive : public Archive + { + size_t hash_value; + char* h; + int offset; + public: + HashArchive() : Archive(true) + { h = (char*)&hash_value; } + + using Archive::operator&; + Archive & operator & (double & d) override { return ApplyHash(d); } + Archive & operator & (int & i) override { return ApplyHash(i); } + Archive & operator & (short & i) override { return ApplyHash(i); } + Archive & operator & (long & i) override { return ApplyHash(i); } + Archive & operator & (size_t & i) override { return ApplyHash(i); } + Archive & operator & (unsigned char & i) override { return ApplyHash(i); } + Archive & operator & (bool & b) override { return ApplyHash(b); } + Archive & operator & (std::string & str) override + { for(auto c : str) ApplyHash(c); return *this; } + Archive & operator & (char *& str) override + { char* s = str; while(*s != '\0') ApplyHash(*(s++)); return *this; } + + size_t GetHash() const { return hash_value; } + + private: + template + Archive& ApplyHash(T val) + { + auto n = sizeof(T); + char* pval = (char*)&val; + for(int i = 0; i < n; i++) + { + h[offset++] ^= pval[i]; + offset %= 8; + } + return *this; + } + }; + } // namespace ngcore #endif // NETGEN_CORE_ARCHIVE_HPP