Skip to content

ExtendingSerialization

Josh Blum edited this page Dec 14, 2015 · 1 revision

Extending serialization table of contents

The Pothos::Object container class supports serialization and deserialization of arbitrary data types. Any Object can potentially be serialized into a memory buffer, and that buffer deserialized back into a Object. This allows remote objects to be configured and to communicate across a network link.

The most common data types are supported by default. Mainly booleans, numbers, strings, and common containers of objects like vectors and dictionaries. Also, many of the Pothos library data types are supported as well such as Pothos::BufferChunk and Pothos::DType.

Suppose the user creates a new data type that is either 1) used as a parameter to a Block's constructor, 2) used as a parameter or return type in a Block's call, 3) output by a Block as a message or label, or 4) just used in some other form of registered class: This guide will show you how to add serialization support for that type.

Suppose we have a custom data type defined as follows:

class FooBar
{
public:
    int getFoo(void) const;
    bool getBar(void) const;
    void setFoo(int foo);
    void setBar(bool bar);

private:
    int _foo;
    bool _bar;
};

In a C++ file we have the following:

#include <Pothos/Object/Serialize.hpp>

namespace Pothos { namespace serialization {
template<class Archive>
void save(Archive & ar, const FooBar &t, const unsigned int)
{
    ar << t.getFoo();
    ar << t.getBar();
}

template<class Archive>
void load(Archive & ar, FooBar &t, const unsigned int)
{
    int foo;
    bool bar;
    ar >> foo;
    ar >> bar;
    t.setFoo(foo);
    t.setBar(bar);
}
}}

POTHOS_SERIALIZATION_SPLIT_FREE(FooBar)
POTHOS_OBJECT_SERIALIZE(FooBar) //registers new Object serialization

The C++ source above should be included in the SOURCES list passed to the CMake function POTHOS_MODULE_UTIL(). As long as the module is built and installed, Pothos will load it, and the serialization will be available at runtime.

The serialization library is actually far more extensible and complete than this tutorial can show. The Pothos serialization library is nothing more than Boost serialization transplanted and renamed. To learn more, visit the tutorial for the Boost serialization, and simply substitute namespace "boost" for "Pothos".