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

Serialization of types without default constructor #2795

Closed
krivenko opened this issue Aug 2, 2017 · 6 comments · Fixed by #2805
Closed

Serialization of types without default constructor #2795

krivenko opened this issue Aug 2, 2017 · 6 comments · Fixed by #2805

Comments

@krivenko
Copy link

krivenko commented Aug 2, 2017

Dear HPX developers,

Are there any plans to support serialization of std::vector<T> (and similar STL containers), where T has no default constructor? This feature exists in boost::serialization but not in hpx::serialization, as far as I can see.

Thanks in advance,
Igor

@AntonBikineev
Copy link
Contributor

AntonBikineev commented Aug 2, 2017 via email

@krivenko
Copy link
Author

krivenko commented Aug 8, 2017

Thank you very much for adding this feature!
I've been trying to verify #2805, and there is something I don't quite understand.
What is the actual serialization protocol, or in other words, what should be done in serialize() and what in save_construct_data()/load_construct_data()?

To illustrate my problem I took tests/unit/serialization/serialization_list.cpp and added one more test function to it.

void test_non_default_constructible_single() {

    std::vector<char> buffer;
    hpx::serialization::output_archive oarchive(buffer);

    B single_b_1(1);
    single_b_1.set_b(2.0);
    oarchive << single_b_1;

    hpx::serialization::input_archive iarchive(buffer);

    B single_b_2(3);
    single_b_2.set_b(4.0);
    iarchive >> single_b_2;
    
    HPX_TEST_EQ(single_b_1.get_a(), single_b_2.get_a());
    HPX_TEST_EQ(single_b_1.get_b(), single_b_2.get_b());
}

Here is what I get:

hpx.git/tests/unit/serialization/serialization_list.cpp(262): test 'single_b_1.get_a() == single_b_2.get_a()' failed in function 'void test_non_default_constructible_single()': '1' != '3'
0 sanity checks and 1 test failed.

That check fails because load_construct_data() is never called. Therefore, deserialization of B::a never occurs.

@AntonBikineev
Copy link
Contributor

AntonBikineev commented Aug 10, 2017 via email

@krivenko
Copy link
Author

Thanks a lot for the fix and the explanation!

Note that this implementation is compatible with that of Boost.Serialization — it only calls save_construct_data/load_construct_data for collections and pointers (this implementation is currently missing deserialization of pointers to non-default-constructible data).

Okay, I guess it indeed makes no sense to overcomplicate things and to go beyond what Boost does.

I'll then adjust my code to construct some dummy objects in save_construct_data/load_construct_data and to do the actual serialization in serialize(). This would still be much better than adding default constructors everywhere.

@hkaiser
Copy link
Member

hkaiser commented Aug 11, 2017

@krivenko I take it that you're fine with merging #2805 now. Is that assumption correct?

@krivenko
Copy link
Author

@hkaiser Yes, go ahead!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants