Unverified Commit 9697fa5d authored by pfeatherstone's avatar pfeatherstone Committed by GitHub
Browse files

[SERIALIZATION] support for std::optional (#2364)



* added support for std::optional if using C++

* oops, bug fix + check if item already holds a type

* oops, another bug fix

* remove warnings about unused parameters
Co-authored-by: default avatarpf <pf@me>
parent 11212a94
...@@ -90,6 +90,7 @@ ...@@ -90,6 +90,7 @@
- std::unique_ptr - std::unique_ptr
- std::shared_ptr - std::shared_ptr
- std::variant (if C++17 is used) - std::variant (if C++17 is used)
- std::optional (if C++17 is used)
- dlib::uint64 - dlib::uint64
- dlib::int64 - dlib::int64
- float_details - float_details
...@@ -121,6 +122,7 @@ ...@@ -121,6 +122,7 @@
- std::unique_ptr - std::unique_ptr
- std::shared_ptr - std::shared_ptr
- std::variant (if C++17 is used) - std::variant (if C++17 is used)
- std::optional (if C++17 is used)
- dlib::uint64 - dlib::uint64
- dlib::int64 - dlib::int64
- float_details - float_details
...@@ -237,6 +239,7 @@ ...@@ -237,6 +239,7 @@
#include <utility> #include <utility>
#if __cplusplus >= 201703L #if __cplusplus >= 201703L
#include <variant> #include <variant>
#include <optional>
#endif #endif
#include "uintn.h" #include "uintn.h"
#include "interfaces/enumerable.h" #include "interfaces/enumerable.h"
...@@ -1272,6 +1275,29 @@ namespace dlib ...@@ -1272,6 +1275,29 @@ namespace dlib
detail::deserialize_variant_helper(item, in, index); detail::deserialize_variant_helper(item, in, index);
} }
template<typename T>
void serialize(const std::optional<T>& item, std::ostream& out)
{
serialize(item.has_value(), out);
if (item)
serialize(item.value(), out);
}
template <typename T>
void deserialize(std::optional<T>& item, std::istream& in)
{
bool has_value = false;
deserialize(has_value, in);
if (has_value)
{
deserialize(item.has_value() ? item.value() : item.emplace(), in);
}
else
{
item.reset();
}
}
#endif #endif
// ---------------------------------------------------------------------------------------- // ----------------------------------------------------------------------------------------
......
...@@ -422,9 +422,9 @@ namespace ...@@ -422,9 +422,9 @@ namespace
immutable_type(immutable_type&& other) = delete; immutable_type(immutable_type&& other) = delete;
immutable_type& operator=(immutable_type&& other) = delete; immutable_type& operator=(immutable_type&& other) = delete;
friend void serialize(const immutable_type& x, std::ostream& out) {} friend void serialize(const immutable_type&, std::ostream&) {}
friend void deserialize(immutable_type& x, std::istream& in) {} friend void deserialize(immutable_type&, std::istream&) {}
bool operator==(const immutable_type& other) const {return true;} bool operator==(const immutable_type&) const {return true;}
}; };
struct my_custom_type struct my_custom_type
...@@ -449,12 +449,13 @@ namespace ...@@ -449,12 +449,13 @@ namespace
std::vector<std::complex<double>> p; std::vector<std::complex<double>> p;
#if __cplusplus >= 201703L #if __cplusplus >= 201703L
std::variant<int,float,std::string,immutable_type> q; std::variant<int,float,std::string,immutable_type> q;
std::optional<std::vector<std::string>> r;
#endif #endif
bool operator==(const my_custom_type& rhs) const bool operator==(const my_custom_type& rhs) const
{ {
#if __cplusplus >= 201703L #if __cplusplus >= 201703L
const bool cpp17_ok = q == rhs.q; const bool cpp17_ok = std::tie(q, r) == std::tie(rhs.q, rhs.r);
#else #else
const bool cpp17_ok = true; const bool cpp17_ok = true;
#endif #endif
...@@ -465,7 +466,7 @@ namespace ...@@ -465,7 +466,7 @@ namespace
} }
#if __cplusplus >= 201703L #if __cplusplus >= 201703L
DLIB_DEFINE_DEFAULT_SERIALIZATION(my_custom_type, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, ptr_shared1, ptr_shared2, q); DLIB_DEFINE_DEFAULT_SERIALIZATION(my_custom_type, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, ptr_shared1, ptr_shared2, q, r);
#else #else
DLIB_DEFINE_DEFAULT_SERIALIZATION(my_custom_type, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, ptr_shared1, ptr_shared2); DLIB_DEFINE_DEFAULT_SERIALIZATION(my_custom_type, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, ptr_shared1, ptr_shared2);
#endif #endif
...@@ -1152,6 +1153,7 @@ namespace ...@@ -1152,6 +1153,7 @@ namespace
t1.p.push_back(rng.get_random_gaussian()); t1.p.push_back(rng.get_random_gaussian());
#if __cplusplus >= 201703L #if __cplusplus >= 201703L
t1.q = "hello there from std::variant, welcome!"; t1.q = "hello there from std::variant, welcome!";
t1.r = {"hello from optional vector of string"};
#endif #endif
t2.a = 2; t2.a = 2;
t2.b = 4.0; t2.b = 4.0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment