Boost C ++ serialization char *

I have a class and I'm trying to serialize shared_ptr, but the normal object serialization method does not work:

class Object {
public:
    Object();
    ~Object();

    shared_ptr<char>objectone;

    friend class boost::serialization::access;
    template <typename Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & objectone;
    }
};

I even tried to do it this way, but it still doesn't work:

    void serialize(Archive &ar, const unsigned int version)
    {
        for (int i = 0; i < (strlen(objectone.get())); i++)
             ar & objectone.get()[i];
    }

Any ideas how to approach this? Thank.

Additional Information:

I have already included both shared_ptr header files:

#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/shared_ptr_132.hpp>

I tried converting to a string and serializing it this way, but it causes the following error: raising :: archive :: archive_exception "what (): stream error

friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
    if (objectone.get()) {
        string temp(objectone.get());
        ar & temp;
    }
    ar & objectone;
}
+5
source share
3 answers

You are missing some key concepts. You no longer need share_ptr if you use std :: string.

You can do

class Object {
public:
    Object();
    ~Object();

    std::string name;

    template <typename Archive>
    void serialize(Archive &ar, const unsigned int version)
    {
        ar & name;
    }
};

and you're done.

Adding

, ,

class test
{
public:

friend class boost::serialization::access;

test() 
{       
}

template<class Archive>
void save(Archive & ar, const unsigned int version) const
{
    int len = strlen(data.get());
    ar  & len;
    for (int index = 0; index < len; ++index)
        ar & data[index];       
}
template<class Archive>
void load(Archive & ar, const unsigned int version)
{
    int len = 0;
    ar  & len;
    data = boost::shared_array<char>(new char[len+1]);
    for (int index = 0; index < len; ++index)
        ar & data[index];
    data[len] = 0;
}
BOOST_SERIALIZATION_SPLIT_MEMBER()

boost::shared_array<char> data;
};
+2

, .

-, shared_ptr . .

-, C-, . , vector string, .

char*, vector/string . , vector/string , , , ... .. , , , . .

:

, , . , , . . if (Archive::is_saving) . , .

+4

, Crazy Eddie , shared_ptr . , .

boost :: serialization provides a make_array wrapper that takes a pointer and wraps it as an array for serialization. This requires that the size of the array be known before serialization.

Then you can do something like:

void serialize(Archive &ar, const unsigned int version)
{
    ar & array_size; // Figure out how to determine this (perhaps strlen(objectone.get())

    if (ar::is_loading)
    {
        objectone.reset(new char[array_size]); // allocate the array before we load it
    }

    if (objectone.get() != NULL)
    {
        ar & make_array(objectone.get(), array_size);
    }
}

This code sample is rather rudimentary, but conveys the concept.

+4
source

All Articles