Skip to content

Commit b1d23f3

Browse files
committed
Add serialisation for akrzemi1::optional
This puts cereal from 1.2.2 to the latest develop branch, which includes support for serialising std::optional. Added support to serialise akrzemi1::optional (copied the function to serialise std::optional). This allows seemless loading and saving across the two different optionals.
1 parent 04b637f commit b1d23f3

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ set(HEADERS
114114
${CMAKE_CURRENT_SOURCE_DIR}/include/eos/video/keyframe_merging.hpp
115115
${CMAKE_CURRENT_SOURCE_DIR}/include/eos/cpp17/optional.hpp
116116
${CMAKE_CURRENT_SOURCE_DIR}/include/eos/cpp17/detail/akrzemi1_optional.hpp
117+
${CMAKE_CURRENT_SOURCE_DIR}/include/eos/cpp17/detail/akrzemi1_optional_serialization.hpp
117118
)
118119

119120
add_library(eos INTERFACE)
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* eos - A 3D Morphable Model fitting library written in modern C++11/14.
3+
*
4+
* File: include/eos/cpp17/detail/akrzemi1_optional_serialization.hpp
5+
*
6+
* Copyright 2018 Patrik Huber
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
#pragma once
21+
22+
#ifndef EOS_AKRZEMI1_OPTIONAL_SERIALIZATION_HPP_
23+
#define EOS_AKRZEMI1_OPTIONAL_SERIALIZATION_HPP_
24+
25+
#include "eos/cpp17/detail/akrzemi1_optional.hpp"
26+
27+
#include "cereal/cereal.hpp"
28+
29+
namespace cereal {
30+
31+
//! Saving for akrzemi1::optional
32+
template <class Archive, typename T>
33+
inline void CEREAL_SAVE_FUNCTION_NAME(Archive& ar, const akrzemi1::optional<T>& optional)
34+
{
35+
if (!optional)
36+
{
37+
ar(CEREAL_NVP_("nullopt", true));
38+
} else
39+
{
40+
ar(CEREAL_NVP_("nullopt", false), CEREAL_NVP_("data", *optional));
41+
}
42+
}
43+
44+
//! Loading for akrzemi1::optional
45+
template <class Archive, typename T>
46+
inline void CEREAL_LOAD_FUNCTION_NAME(Archive& ar, akrzemi1::optional<T>& optional)
47+
{
48+
bool nullopt;
49+
ar(CEREAL_NVP_("nullopt", nullopt));
50+
51+
if (nullopt)
52+
{
53+
optional = akrzemi1::nullopt;
54+
} else
55+
{
56+
T value;
57+
ar(CEREAL_NVP_("data", value));
58+
optional = std::move(value);
59+
}
60+
}
61+
62+
} // namespace cereal
63+
64+
#endif /* EOS_AKRZEMI1_OPTIONAL_SERIALIZATION_HPP_ */

include/eos/cpp17/optional.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
#ifdef __APPLE__
2626
#include "eos/cpp17/detail/akrzemi1_optional.hpp"
27+
#include "eos/cpp17/detail/akrzemi1_optional_serialization.hpp"
2728
namespace eos {
2829
namespace cpp17 = ::akrzemi1;
2930
}

0 commit comments

Comments
 (0)