diff --git a/dlib/type_safe_union/type_safe_union_kernel.h b/dlib/type_safe_union/type_safe_union_kernel.h index f48ba1601..36f4ba522 100644 --- a/dlib/type_safe_union/type_safe_union_kernel.h +++ b/dlib/type_safe_union/type_safe_union_kernel.h @@ -309,84 +309,20 @@ namespace dlib return type_identity == 0; } - private: - struct serialize_helper - { - /* - This is a function object to help us serialize type_safe_unions - */ - - std::ostream& out; - serialize_helper(std::ostream& out_): out(out_) {} - template - void operator() (const T& item) const { serialize(item, out); } - }; public: + template < + typename t1, typename t2, typename t3, typename t4, typename t5, + typename t6, typename t7, typename t8, typename t9, typename t10, + typename t11, typename t12, typename t13, typename t14, typename t15, + typename t16, typename t17, typename t18, typename t19, typename t20 + > friend void serialize ( - const type_safe_union& item, + const type_safe_union& item, std::ostream& out - ) - { - try - { - // save the type_identity - serialize(item.type_identity, out); - item.apply_to_contents(serialize_helper(out)); - } - catch (serialization_error& e) - { - throw serialization_error(e.info + "\n while serializing an object of type type_safe_union"); - } - } + ); - friend void deserialize ( - type_safe_union& item, - std::istream& in - ) - { - try - { - int type_identity; - deserialize(type_identity, in); - switch (type_identity) - { - // swap an empty type_safe_union into item since it should be in the empty state - case 0: type_safe_union().swap(item); break; - - case 1: deserialize(item.get(), in); break; - case 2: deserialize(item.get(), in); break; - case 3: deserialize(item.get(), in); break; - case 4: deserialize(item.get(), in); break; - case 5: deserialize(item.get(), in); break; - - case 6: deserialize(item.get(), in); break; - case 7: deserialize(item.get(), in); break; - case 8: deserialize(item.get(), in); break; - case 9: deserialize(item.get(), in); break; - case 10: deserialize(item.get(), in); break; - - case 11: deserialize(item.get(), in); break; - case 12: deserialize(item.get(), in); break; - case 13: deserialize(item.get(), in); break; - case 14: deserialize(item.get(), in); break; - case 15: deserialize(item.get(), in); break; - - case 16: deserialize(item.get(), in); break; - case 17: deserialize(item.get(), in); break; - case 18: deserialize(item.get(), in); break; - case 19: deserialize(item.get(), in); break; - case 20: deserialize(item.get(), in); break; - - default: throw serialization_error("Corrupt data detected while deserializing type_safe_union"); - } - } - catch (serialization_error& e) - { - throw serialization_error(e.info + "\n while deserializing an object of type type_safe_union"); - } - } template < typename T @@ -637,6 +573,103 @@ namespace dlib is_convertible::value; }; +// ---------------------------------------------------------------------------------------- + + namespace impl + { + struct serialize_helper + { + /* + This is a function object to help us serialize type_safe_unions + */ + + std::ostream& out; + serialize_helper(std::ostream& out_): out(out_) {} + template + void operator() (const T& item) const { serialize(item, out); } + }; + } + + template < + typename T1, typename T2, typename T3, typename T4, typename T5, + typename T6, typename T7, typename T8, typename T9, typename T10, + typename T11, typename T12, typename T13, typename T14, typename T15, + typename T16, typename T17, typename T18, typename T19, typename T20 + > + void serialize ( + const type_safe_union& item, + std::ostream& out + ) + { + try + { + // save the type_identity + serialize(item.type_identity, out); + item.apply_to_contents(dlib::impl::serialize_helper(out)); + } + catch (serialization_error& e) + { + throw serialization_error(e.info + "\n while serializing an object of type type_safe_union"); + } + } + +// ---------------------------------------------------------------------------------------- + + template < + typename T1, typename T2, typename T3, typename T4, typename T5, + typename T6, typename T7, typename T8, typename T9, typename T10, + typename T11, typename T12, typename T13, typename T14, typename T15, + typename T16, typename T17, typename T18, typename T19, typename T20 + > + void deserialize ( + type_safe_union& item, + std::istream& in + ) + { + try + { + typedef type_safe_union tsu_type; + + int type_identity; + deserialize(type_identity, in); + switch (type_identity) + { + // swap an empty type_safe_union into item since it should be in the empty state + case 0: tsu_type().swap(item); break; + + case 1: deserialize(item.template get(), in); break; + case 2: deserialize(item.template get(), in); break; + case 3: deserialize(item.template get(), in); break; + case 4: deserialize(item.template get(), in); break; + case 5: deserialize(item.template get(), in); break; + + case 6: deserialize(item.template get(), in); break; + case 7: deserialize(item.template get(), in); break; + case 8: deserialize(item.template get(), in); break; + case 9: deserialize(item.template get(), in); break; + case 10: deserialize(item.template get(), in); break; + + case 11: deserialize(item.template get(), in); break; + case 12: deserialize(item.template get(), in); break; + case 13: deserialize(item.template get(), in); break; + case 14: deserialize(item.template get(), in); break; + case 15: deserialize(item.template get(), in); break; + + case 16: deserialize(item.template get(), in); break; + case 17: deserialize(item.template get(), in); break; + case 18: deserialize(item.template get(), in); break; + case 19: deserialize(item.template get(), in); break; + case 20: deserialize(item.template get(), in); break; + + default: throw serialization_error("Corrupt data detected while deserializing type_safe_union"); + } + } + catch (serialization_error& e) + { + throw serialization_error(e.info + "\n while deserializing an object of type type_safe_union"); + } + } + // ---------------------------------------------------------------------------------------- }