Skip to content

Commit

Permalink
Added hash functions as a tool to generate serialization ids or just
Browse files Browse the repository at this point in the history
hash objects during compile time.
  • Loading branch information
eyalz800 committed Dec 23, 2021
1 parent a34f3a0 commit dd02681
Show file tree
Hide file tree
Showing 4 changed files with 969 additions and 156 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,40 @@ Note that the serialization ids of types in the variant must match in length, or
compilation error will issue.

You may also use any sequence of bytes instead of a readable string, as well as an integer
or any literal type.
or any literal type, here is an example of how to use a hash of a string as a serialization
id:
```cpp
using namespace zpp::bits::literals;

// Inside:
using serialize_id = zpp::bits::id<"v1::person"_sha1>; // Sha1
using serialize_id = zpp::bits::id<"v1::person"_sha256>; // Sha256

// Outside:
auto serialize_id(const person &) -> zpp::bits::id<"v1::person"_sha1>; // Sha1
auto serialize_id(const person &) -> zpp::bits::id<"v1::person"_sha256>; // Sha256
```
You can also serialize just the first bytes of the hash, like so:
```cpp
// First 4 bytes of hash:
using serialize_id = zpp::bits::id<"v1::person"_sha256, 4>; // First 4 bytes of sha256
```

The type is then converted to bytes at compile time using (... wait for it) `zpp::bits::out`
at compile time, so as long as your literal type is serializable according to the above,
you can use it as a serialization id.

* If you want to serialize the variant without an id, or if you know that a variant is going to
have a particular ID upon deserialize, you may do it using `zpp::bits::known_id` to wrap your variant:
```cpp
std::variant<v1::person, v2::person> v;

// Id assumed to be v2::person, and is not serialized / deserialized.
out(zpp::bits::known_id<"v2::person"_sha256, 4>(v));
in(zpp::bits::known_id<"v2::person"_sha256, 4>(v));
```
* As part of the library implementation it was required to implement some reflection types, for
counting members and visiting members, and the library exposes these to the user:
```cpp
Expand Down
20 changes: 20 additions & 0 deletions test/src/test_hash.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include "test.h"

namespace test_hash
{
using namespace zpp::bits::literals;
using namespace std::literals;

static_assert(""_sha1 ==
"da39a3ee5e6b4b0d3255bfef95601890afd80709"_unhexlify);
static_assert(
""_sha256 ==
"e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"_unhexlify);

static_assert("The quick brown fox jumps over the lazy dog"_sha1 ==
"2fd4e1c67a2d28fced849ee1bb76e7391b93eb12"_unhexlify);

static_assert(
"The quick brown fox jumps over the lazy dog"_sha256 ==
"d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592"_unhexlify);
} // namespace test_hash
Loading

0 comments on commit dd02681

Please sign in to comment.