Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for std::bitset #32

Merged
merged 4 commits into from
Jan 15, 2024
Merged

Support for std::bitset #32

merged 4 commits into from
Jan 15, 2024

Conversation

finger563
Copy link
Contributor

@finger563 finger563 commented Jan 10, 2024

Added support for (de-)serialization of std::bitset using alpaca.

Related to #21 (since std::bitfield provides some of the same use case as struct bitfields).

Example showing bitfield serialization and deserialization works:
CleanShot 2024-01-10 at 10 16 34

Test code:

    fmt::print("Starting bitfield serialization example!\n");
    //! [bitfield serialization example]
    struct MyBits {
      std::vector<bool> bits; // testing to see if std::vector<bool> space-efficient bitfield
                              // serialization works
      std::bitset<4> bitset;  // testing to see if std::bitset space-efficient bitfield
                              // serialization works
    };
    std::error_code ec;
    // here we try to make 4 bits followed by 4 bits
    MyBits object{{true, false, true, false}, 0b1010};
    fmt::print("object.bits:   {}\n", object.bits);
    fmt::print("object.bitset: {}\n", object.bitset);
    std::vector<uint8_t> buffer;
    auto bytes_written = alpaca::serialize<alpaca::options::none>(object, buffer);
    fmt::print("Serialized {}B to std::vector\n", bytes_written);
    fmt::print("Serialized buffer: {::x}\n", buffer);
    auto new_object = alpaca::deserialize<alpaca::options::none, MyBits>(buffer, ec);
    if (!ec && new_object.bits == object.bits && new_object.bitset == object.bitset) {
      fmt::print("Deserialized successfully!\n");
      fmt::print("\tbits:   {}\n"
                 "\tbitset: {}\n",
                  new_object.bits, new_object.bitset);
    } else {
      fmt::print("Deserialization failed: {}\n", ec.message());
      fmt::print("\tbits:   {}\n"
                 "\tbitset: {}\n",
                  new_object.bits, new_object.bitset);
    }
    //! [bitfield serialization example]

@finger563 finger563 marked this pull request as draft January 10, 2024 15:33
@finger563 finger563 marked this pull request as ready for review January 10, 2024 16:19
@finger563 finger563 requested a review from p-ranav January 11, 2024 01:06
@p-ranav p-ranav merged commit 6bed0a0 into p-ranav:master Jan 15, 2024
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants