From eb2806f34887271b8bf73cbf59b8931ec8c3e633 Mon Sep 17 00:00:00 2001 From: Davis King Date: Thu, 8 May 2014 20:40:22 -0400 Subject: [PATCH] Added a simplified operator << and >> syntax for serializing to and from files. --- dlib/serialize.h | 71 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 68 insertions(+), 3 deletions(-) diff --git a/dlib/serialize.h b/dlib/serialize.h index 1e10bdced..e62d20f71 100644 --- a/dlib/serialize.h +++ b/dlib/serialize.h @@ -4,9 +4,8 @@ #define DLIB_SERIALIZe_ /*! - There are two global functions in the dlib namespace that provide - serialization and deserialization support. Their signatures and specifications - are as follows: + There are two global functions in the dlib namespace that provide serialization and + deserialization support. Their signatures and specifications are as follows: void serialize ( const serializable_type& item, @@ -47,6 +46,16 @@ - any other exception *!/ + For convenience, you can also serialize to a file using this syntax: + serialize("your_file.dat") << some_object << another_object; + + That overwrites the contents of your_file.dat with the serialized data from some_object + and another_object. Then to recall the objects from the file you can do: + deserialize("your_file.dat") >> some_object >> another_object; + + Finally, you can chain as many objects together using the << and >> operators as you + like. + This file provides serialization support to the following object types: - The C++ base types (NOT including pointer types) @@ -131,6 +140,7 @@ #include #include #include +#include #include #include #include @@ -145,6 +155,7 @@ #include "unicode.h" #include "byte_orderer.h" #include "float_details.h" +#include "smart_pointers/shared_ptr.h" namespace dlib { @@ -1271,6 +1282,60 @@ namespace dlib } // ---------------------------------------------------------------------------------------- + + class proxy_serialize + { + public: + explicit proxy_serialize ( + const std::string& filename + ) + { + fout.reset(new std::ofstream(filename.c_str())); + if (!(*fout)) + throw serialization_error("Unable to open " + filename + " for writing."); + } + + template + inline proxy_serialize& operator<<(const T& item) + { + serialize(item, *fout); + return *this; + } + + private: + shared_ptr fout; + }; + + class proxy_deserialize + { + public: + explicit proxy_deserialize ( + const std::string& filename + ) + { + fin.reset(new std::ifstream(filename.c_str())); + if (!(*fin)) + throw serialization_error("Unable to open " + filename + " for reading."); + } + + template + inline proxy_deserialize& operator>>(T& item) + { + deserialize(item, *fin); + return *this; + } + + private: + shared_ptr fin; + }; + + inline proxy_serialize serialize(const std::string& filename) + { return proxy_serialize(filename); } + inline proxy_deserialize deserialize(const std::string& filename) + { return proxy_deserialize(filename); } + +// ---------------------------------------------------------------------------------------- + } // forward declare the MessageLite object so we can reference it below.