20 #ifndef KJB_L_SERIALIZATION_H
21 #define KJB_L_SERIALIZATION_H
23 #include <l/l_sys_def.h>
24 #include <l/l_sys_lib.h>
25 #include <l/l_sys_io.h>
34 #include <boost/format.hpp>
35 #include <boost/utility/enable_if.hpp>
36 #include <boost/type_traits.hpp>
39 #ifdef KJB_HAVE_BST_SERIAL
40 #include <boost/archive/text_iarchive.hpp>
41 #include <boost/archive/text_oarchive.hpp>
42 #include <boost/archive/binary_iarchive.hpp>
43 #include <boost/archive/binary_oarchive.hpp>
46 #include <boost/concept_check.hpp>
47 #include <boost/concept_archetype.hpp>
48 #include <boost/static_assert.hpp>
86 #ifdef KJB_HAVE_BST_SERIAL
88 std::ofstream ofs(
"");
89 std::ifstream ifs(
"");
91 boost::archive::text_oarchive oa(ofs);
92 boost::archive::text_iarchive ia(ifs);
99 static const bool boost_serialization_not_available =
true;
100 BOOST_STATIC_ASSERT(boost_serialization_not_available ==
true);
116 template <
class Archive,
class KJB_readable_writable>
119 #ifdef KJB_HAVE_BST_SERIAL
122 std::string obj_as_string;
124 char tmp_fname[ MAXPATHLEN ];
127 KJB(
ETX( BUFF_GET_TEMP_FILE_NAME( tmp_fname ) ) );
133 if(Archive::is_saving::value ==
true)
135 obj.write(tmp_fname);
137 std::ifstream ifs(tmp_fname);
140 obj_as_string.assign(
141 std::istreambuf_iterator<char>(ifs),
142 std::istreambuf_iterator<char>());
149 if(Archive::is_loading::value ==
true)
153 std::ofstream ofs(tmp_fname);
154 ofs << obj_as_string;
158 KJB_readable_writable tmp_matrix(tmp_fname);
159 obj.swap(tmp_matrix);
163 ETX( kjb_c::kjb_unlink( tmp_fname ) );
169 template <
class Archive,
class KJB_readable_writable>
170 void kjb_serialize(Archive & ar, KJB_readable_writable& obj,
const unsigned int version)
199 template <
class Serializable>
200 #ifdef KJB_HAVE_BST_SERIAL
201 void load(Serializable& obj,
const std::string& fname)
203 void load(Serializable&,
const std::string&)
206 #ifdef KJB_HAVE_BST_SERIAL
211 std::ifstream ifs(fname.c_str());
219 boost::archive::text_iarchive ia(ifs);
222 catch(boost::archive::archive_exception& ex)
224 KJB_THROW_3(
IO_error,
"Error reading %s. Format may be invalid. Boost error was: %s", (fname.c_str())(ex.what()));
248 template <
class SerializableOutputIterator>
252 typename std::iterator_traits<SerializableOutputIterator>::value_type
255 const size_t size = indices.
size();
256 boost::format fmt(fmt_str);
257 for(
size_t ii = 0; ii < size; ii++)
259 size_t i = indices[ii];
261 std::string fname = (fmt %
i).str();
276 template <
class SerializableOutputIterator>
277 void load_many(SerializableOutputIterator it,
const std::string& fmt_str,
size_t num_files,
size_t first_index = 0,
size_t modulo = 0)
280 typename std::iterator_traits<SerializableOutputIterator>::value_type
285 first_index + num_files - 1,
291 template <
class Value_type,
class SerializablePtrOutputIterator>
296 boost::format fmt(fmt_str);
297 const size_t size = indices.
size();
298 for(
size_t ii = 0; ii < size; ii++)
300 size_t i = indices[ii];
302 Value_type* ptr =
new Value_type();
304 std::string fname = (fmt %
i).str();
311 template <
class Value_type,
class SerializablePtrOutputIterator>
316 boost::format fmt(fmt_str);
317 const size_t size = indices.
size();
318 for(
size_t ii = 0; ii < size; ii++)
320 size_t i = indices[ii];
321 boost::shared_ptr<Value_type> ptr(
new Value_type());
323 std::string fname = (fmt %
i).str();
324 load(*ptr.get(), fname);
331 template <
class SerializablePtrOutputIterator>
332 void load_many_to_ptr(SerializablePtrOutputIterator it,
const std::string& fmt_str,
size_t num_files,
size_t first_index = 0,
size_t modulo = 1)
334 typedef typename std::iterator_traits<SerializablePtrOutputIterator>::value_type Ptr_type;
337 first_index + num_files - 1,
351 template <
class SerializablePtrOutputIterator,
class Index_range_type>
352 typename boost::enable_if<boost::is_convertible<Index_range_type, Index_range>,
void>::type
353 load_many_to_ptr(SerializablePtrOutputIterator it,
const std::string& fmt_str,
const Index_range_type& indices)
355 typedef typename std::iterator_traits<SerializablePtrOutputIterator>::value_type Ptr_type;
378 template <
class Serializable>
379 #ifdef KJB_HAVE_BST_SERIAL
380 void save(
const Serializable& obj,
const std::string& fname)
382 void save(
const Serializable&,
const std::string&)
385 #ifdef KJB_HAVE_BST_SERIAL
389 std::ofstream ofs(fname.c_str());
397 boost::archive::text_oarchive oa(ofs);
402 KJB_THROW_3(
IO_error,
"Error serializing object to file %s. Probably a bug in the serialization routine.", (fname.c_str()));
void load_many_to_ptr(SerializablePtrOutputIterator it, const std::string &fmt_str, size_t num_files, size_t first_index=0, size_t modulo=1)
Definition: l_serialization.h:332
#define ETX(a)
Definition: l_exception.h:67
size_t size() const
Definition: l_index.h:273
#define KJB(x)
Definition: l_util.h:9
void kjb_serialize_default(Archive &ar, KJB_readable_writable &obj, const unsigned int)
Implements boost serialization for any object that implements the KjbReadableWritable concept...
Definition: l_serialization.h:117
void kjb_serialize(Archive &ar, KJB_readable_writable &obj, const unsigned int version)
Definition: l_serialization.h:170
BOOST_CONCEPT_USAGE(SerializableConcept)
Definition: l_serialization.h:84
void load(Edge_set &edges, const std::string &fname)
Definition: edge.h:603
void load_many(SerializableOutputIterator it, const std::string &fmt_str, const Index_range &indices)
Definition: l_serialization.h:249
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
void load_many_dynamic_dispatch(Value_type *, SerializablePtrOutputIterator it, const std::string &fmt_str, const Index_range &indices)
Definition: l_serialization.h:292
#define KJB_THROW_3(ex, fmt, params)
Definition: l_exception.h:56
void save(const Edge_set &edges, const std::string &fname)
Definition: edge.h:608
get the indices of edges in each direction for i
Definition: APPgetLargeConnectedEdges.m:48
Object thrown when input or output fails.
Definition: l_exception.h:496
Definition: l_serialization.h:82
Support for error handling exception classes in libKJB.
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539