File: stdSharedPtrSerialization.h

package info (click to toggle)
stopt 5.12%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 8,860 kB
  • sloc: cpp: 70,456; python: 5,950; makefile: 72; sh: 57
file content (48 lines) | stat: -rw-r--r-- 1,207 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
// Copyright (C) 2016 EDF
// All Rights Reserved
// This code is published under the GNU Lesser General Public License (GNU LGPL)
#ifndef STDSHAREDPTRSERIALIZATION_H
#define STDSHAREDPTRSERIALIZATION_H
#include <boost/serialization/split_free.hpp>
#include <boost/unordered_map.hpp>

//---/ Wrapper for std::shared_ptr<> /------------------------------------------

namespace boost
{
namespace serialization
{

template<class Archive, class Type>
void save(Archive &archive, const std::shared_ptr<Type> &value, const unsigned int /*version*/)
{
    Type *data = value.get();
    archive << data;
}

template<class Archive, class Type>
void load(Archive &archive, std::shared_ptr<Type> &value, const unsigned int /*version*/)
{
    Type *data;
    archive >> data;

    typedef std::weak_ptr<Type> WeakPtr;
    static boost::unordered_map<void *, WeakPtr> hash;

    if (hash[data].expired())
    {
        value = std::shared_ptr<Type>(data);
        hash[data] = value;
    }
    else value = hash[data].lock();
}

template<class Archive, class Type>
inline void serialize(Archive &archive, std::shared_ptr<Type> &value, const unsigned int version)
{
    split_free(archive, value, version);
}

}
}
#endif