David Doria
David Doria

Reputation: 10273

Serializing `std::type_index`

I've been using std::type_index to store a std::unordered_map<std::type_index, MyProperty> in MyClass. Now I would like to serialize (with boost::serialization) MyClass. The compiler says struct std::type_index has no member named serialize which indicates that boost::serialization doesn't support std::type_index. So the question is, what to do in this case? Does anyone have a serialize function for std::type_index? Or is there a different object that can be used for the key for this map that I need that is already serializable that can do the same type of thing. Namely, when using a function template I do :

template <typename T>
void MyClass::func(T)
{
  myMap.find(std::type_index(typeid(T)));
}

Here is a demo of the lack of support:

#include <boost/archive/text_oarchive.hpp>

#include <fstream>
#include <typeindex>

int main()
{
    std::type_index myTypeIndex = typeid(double);

    std::ofstream outputStream("test.txt");
    boost::archive::text_oarchive outputArchive(outputStream);

    outputArchive << myTypeIndex;
    outputStream.close();

    return 0;
}

Upvotes: 1

Views: 992

Answers (1)

Richard Hodges
Richard Hodges

Reputation: 69882

Step 1: Define your own boost::serialization::load/save<> overloads for std::type_index

Step 2: provide a means of mapping strings to type_index and vice-versa.

Step 3: store the type_index in the archive in terms of its name.

You will of course need to remember to register the name of every type you intend to use as a key in your map. In the example below you would do this by calling register_name("Foo", typeid(Foo)); etc.

#include <iostream>
#include <boost/archive/text_oarchive.hpp>
#include <boost/serialization/split_free.hpp>

#include <fstream>
#include <sstream>
#include <typeindex>
#include <tuple>
#include <vector>
#include <string>

struct nothing {};

using named_typeindex = std::tuple<std::string, std::type_index>;
std::vector<named_typeindex> name_register =
{
};

std::type_index type_for_name(const std::string& name)
{
    auto i = std::find_if(std::begin(name_register), std::end(name_register),
                          [&name](const auto& entry) { return std::get<std::string>(entry) == name; } );
    if (i == std::end(name_register))
        return typeid(nothing);
    return std::get<std::type_index>(*i);
}

std::string const& name_for_type(std::type_index type)
{
    auto i = std::find_if(std::begin(name_register), std::end(name_register),
                          [type](const auto& entry) { return std::get<std::type_index>(entry) == type; } );

    using namespace std::string_literals;
    if (i == std::end(name_register))
        throw std::logic_error("unregistered type "s + type.name());

    return std::get<std::string>(*i);
}

bool register_name(std::string name, std::type_index ti)
{
    if (type_for_name(name) == typeid(nothing))
    {
        name_register.push_back(std::make_tuple(std::move(name), ti));
        return true;
    }
    return false;
}

namespace boost {
    namespace serialization {

        template<class Archive>
        void save(Archive & ar, const std::type_index & t, unsigned int version)
        {
            ar << name_for_type(t);
        }

        template<class Archive>
        void load(Archive & ar, std::type_index & t, unsigned int version)
        {
            std::string s;
            ar >> s;
            t = type_for_name(s);
        }

    } // namespace serialization
} // namespace boost

BOOST_SERIALIZATION_SPLIT_FREE(std::type_index);

int main()
{
    std::type_index myTypeIndex = typeid(double);

    std::ostringstream outputStream {};
    boost::archive::text_oarchive outputArchive(outputStream);

    outputArchive << myTypeIndex;

    std::cout << outputStream.str() << std::endl;

    return 0;
}

Upvotes: 1

Related Questions