Commit eb2806f3 authored by Davis King's avatar Davis King

Added a simplified operator << and >> syntax for serializing to and from files.

parent 754610f2
...@@ -4,9 +4,8 @@ ...@@ -4,9 +4,8 @@
#define DLIB_SERIALIZe_ #define DLIB_SERIALIZe_
/*! /*!
There are two global functions in the dlib namespace that provide There are two global functions in the dlib namespace that provide serialization and
serialization and deserialization support. Their signatures and specifications deserialization support. Their signatures and specifications are as follows:
are as follows:
void serialize ( void serialize (
const serializable_type& item, const serializable_type& item,
...@@ -47,6 +46,16 @@ ...@@ -47,6 +46,16 @@
- any other exception - 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: This file provides serialization support to the following object types:
- The C++ base types (NOT including pointer types) - The C++ base types (NOT including pointer types)
...@@ -131,6 +140,7 @@ ...@@ -131,6 +140,7 @@
#include <iomanip> #include <iomanip>
#include <cstddef> #include <cstddef>
#include <iostream> #include <iostream>
#include <fstream>
#include <string> #include <string>
#include <vector> #include <vector>
#include <complex> #include <complex>
...@@ -145,6 +155,7 @@ ...@@ -145,6 +155,7 @@
#include "unicode.h" #include "unicode.h"
#include "byte_orderer.h" #include "byte_orderer.h"
#include "float_details.h" #include "float_details.h"
#include "smart_pointers/shared_ptr.h"
namespace dlib namespace dlib
{ {
...@@ -1271,6 +1282,60 @@ 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 <typename T>
inline proxy_serialize& operator<<(const T& item)
{
serialize(item, *fout);
return *this;
}
private:
shared_ptr<std::ofstream> 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 <typename T>
inline proxy_deserialize& operator>>(T& item)
{
deserialize(item, *fin);
return *this;
}
private:
shared_ptr<std::ifstream> 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. // forward declare the MessageLite object so we can reference it below.
......
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