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

[Feature requestion] Integration with Boost::Describe #1143

Open
pfeatherstone opened this issue Oct 10, 2024 · 0 comments
Open

[Feature requestion] Integration with Boost::Describe #1143

pfeatherstone opened this issue Oct 10, 2024 · 0 comments

Comments

@pfeatherstone
Copy link

pfeatherstone commented Oct 10, 2024

I've been playing with a Boost::Describe integration.

Here is an example that packs a custom struct into a stream. It only supports packing for now.

#include <cstdio>
#include <msgpack.hpp>
#include <boost/describe/class.hpp>

template <
    bool as_map,
    class Stream,
    class T,
    class D1 = boost::describe::describe_members<T, boost::describe::mod_any_access>
>
void msgpack_pack(msgpack::packer<Stream>& packer, const T& obj, std::integral_constant<bool, as_map>)
{
    if constexpr (as_map)
    {
        packer.pack_map(boost::mp11::mp_size<D1>::value);

        boost::mp11::mp_for_each<D1>([&](auto D) {
            packer.pack(D.name);
            packer.pack(obj.*D.pointer);
        });
    }
    else
    {
        packer.pack_array(boost::mp11::mp_size<D1>::value);

        boost::mp11::mp_for_each<D1>([&](auto D) {
            packer.pack(obj.*D.pointer);
        });
    }
}

template <
    bool as_map,
    class Stream,
    class T,
    class D1 = boost::describe::describe_members<T, boost::describe::mod_any_access>
>
void msgpack_pack(Stream& stream, const T& obj, std::integral_constant<bool, as_map> option)
{
    msgpack::packer<Stream> packer(&stream);
    msgpack_pack(packer, obj, option);
}

struct example_struct
{
    std::string msg;
    std::vector<float> v; 
};

BOOST_DESCRIBE_STRUCT(example_struct, (), (msg, v))

int main()
{
    example_struct data;
    data.msg = "Hello there!";
    data.v.resize(10);

    msgpack::sbuffer buf1, buf2;
    msgpack_pack(buf1, data, std::true_type{}); // as map
    msgpack_pack(buf2, data, std::false_type{}); // as array
    
    printf("Serialized as map %zu - serialized as array %zu\n", buf1.size(), buf2.size());
    return 0;
}

Now i've implemented my own msgpack_pack function. What i would really like is to reuse the msgpack API. Can you think of ways to achieve this?

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

No branches or pull requests

1 participant