Unravel Engine C++ Reference
Loading...
Searching...
No Matches
cereal_optional_nvp.h
Go to the documentation of this file.
1/*
2The MIT License (MIT)
3
4Copyright (c) 2017 Yehonatan Ballas
5
6Permission is hereby granted, free of charge, to any person obtaining a copy
7of this software and associated documentation files (the "Software"), to deal
8in the Software without restriction, including without limitation the rights
9to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10copies of the Software, and to permit persons to whom the Software is
11furnished to do so, subject to the following conditions:
12
13The above copyright notice and this permission notice shall be included in all
14copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22SOFTWARE.
23*/
24
25#ifndef ser20_optional_nvp_h
26#define ser20_optional_nvp_h
27
28#include <ser20/ser20.hpp>
29#include <ser20/details/traits.hpp>
30
31namespace ser20
32{
33class JSONInputArchive;
34class XMLInputArchive;
35class YAMLInputArchive;
36
37// Optionally load an NVP if its name equals to the current node's name
38// Loading members should be done in the same order they were saved
39// return true if NVP found
40template<class Archive, class T>
41typename std::enable_if_t<traits::is_same_archive_v<Archive, JSONInputArchive> ||
42 traits::is_same_archive_v<Archive, XMLInputArchive> ||
43 traits::is_same_archive_v<Archive, YAMLInputArchive>,
44 bool>
45make_optional_nvp(Archive& ar, const char* name, T&& value)
46{
47 const auto node_name = ar.getNodeName();
48
49 // if names are equal
50 if(node_name != nullptr && strcmp(name, node_name) == 0)
51 {
52 ar(make_nvp(name, std::forward<T>(value))); // load the NVP. Advances to the next node
53 return true;
54 }
55
56 return false;
57}
58
59template<class Archive, class T>
60void make_optional_nvp(OutputArchive<Archive>& ar, const char* name, T&& value)
61{
62 ar(make_nvp(name, std::forward<T>(value)));
63}
64
65// Saves NVP if predicate is true. Useful for avoiding splitting into save & load if also saving optionally.
66template<class Archive, class T, class Predicate>
67void make_optional_nvp(OutputArchive<Archive>& ar, const char* name, T&& value, Predicate predicate)
68{
69 if(predicate())
70 ar(make_nvp(name, std::forward<T>(value)));
71}
72
73template<class Archive, class T, class Predicate>
74typename std::enable_if_t<traits::is_same_archive_v<Archive, JSONInputArchive> ||
75 traits::is_same_archive_v<Archive, XMLInputArchive> ||
76 traits::is_same_archive_v<Archive, YAMLInputArchive>,
77 bool>
78make_optional_nvp(Archive& ar, const char* name, T&& value, Predicate predicate)
79{
80 return make_optional_nvp(ar, name, std::forward<T>(value));
81}
82} // namespace ser20
83
84// Macros for using the variable name as the NVP name
85#define EXPAND(x) x
86#define GET_CEREAL_OPTIONAL_NVP_MACRO(_1, _2, _3, NAME, ...) NAME
87#define CEREAL_OPTIONAL_NVP(...) \
88 EXPAND(GET_CEREAL_OPTIONAL_NVP_MACRO(__VA_ARGS__, CEREAL_OPTIONAL_NVP_3, CEREAL_OPTIONAL_NVP_2)(__VA_ARGS__))
89
90#define CEREAL_OPTIONAL_NVP_2(ar, T) ::ser20::make_optional_nvp(ar, #T, T)
91#define CEREAL_OPTIONAL_NVP_3(ar, T, P) ::ser20::make_optional_nvp(ar, #T, T, P)
92
93#endif // ser20_optional_nvp_h
std::string name
Definition hub.cpp:27
Definition yaml.hpp:46
std::enable_if_t< traits::is_same_archive_v< Archive, JSONInputArchive >||traits::is_same_archive_v< Archive, XMLInputArchive >||traits::is_same_archive_v< Archive, YAMLInputArchive >, bool > make_optional_nvp(Archive &ar, const char *name, T &&value)