25 #ifndef GRAPHLAB_SERIALIZE_HPP
26 #include <graphlab/serialization/serialize.hpp>
30 #ifndef GRAPHLAB_OARCHIVE_HPP
31 #define GRAPHLAB_OARCHIVE_HPP
35 #include <graphlab/logger/assertions.hpp>
36 #include <graphlab/serialization/is_pod.hpp>
37 #include <graphlab/serialization/has_save.hpp>
94 : out(&outstream),buf(NULL),off(0),len(0) {}
97 : out(NULL),buf(NULL),off(0),len(0) {}
99 inline void expand_buf(
size_t s) {
102 buf = (
char*)realloc(buf, len);
108 inline void write(
const char* c, std::streamsize s) {
111 memcpy(buf + off, c, s);
117 template <
typename T>
118 inline void direct_assign(
const T& t) {
120 expand_buf(
sizeof(T));
121 (*
reinterpret_cast<T*
>(buf + off)) = t;
126 out->write(reinterpret_cast<char*>(&localt),
sizeof(T));
130 inline void advance(
size_t s) {
135 out->seekp(s, std::ios_base::cur);
141 return out == NULL ?
false : out->fail();
162 : oarc(new
oarchive(outstream)), mine(true) { }
167 inline oarchive_soft_fail(
void)
168 : oarc(new oarchive) {}
173 inline void write(
const char* c, std::streamsize s) {
176 template <
typename T>
177 inline void direct_assign(
const T& t) {
178 oarc->direct_assign(t);
186 if (mine)
delete oarc;
190 namespace archive_detail {
193 template <
typename OutArcType,
typename T>
194 struct serialize_hard_or_soft_fail {
195 inline static void exec(OutArcType& oarc,
const T& t) {
201 template <
typename T>
202 struct serialize_hard_or_soft_fail<oarchive_soft_fail, T> {
203 inline static void exec(oarchive_soft_fail& oarc,
const T& t) {
207 save_or_fail(*(oarc.oarc), t);
218 template <
typename OutArcType,
typename T,
bool IsPOD>
219 struct serialize_impl {
220 static void exec(OutArcType& oarc,
const T& t) {
221 serialize_hard_or_soft_fail<OutArcType, T>::exec(oarc, t);
226 template <
typename OutArcType,
typename T>
227 struct serialize_impl<OutArcType, T, true> {
228 inline static void exec(OutArcType& oarc,
const T& t) {
229 oarc.direct_assign(t);
237 template <
typename OutArcType,
typename T>
238 struct serialize_impl<OutArcType, const T, true> {
239 inline static void exec(OutArcType& oarc,
const T& t) {
240 serialize_impl<OutArcType, T, true>::exec(oarc, t);
247 template <
typename OutArcType,
typename T>
248 struct serialize_impl<OutArcType, const T, false> {
249 inline static void exec(OutArcType& oarc,
const T& t) {
250 serialize_impl<OutArcType, T, false>::exec(oarc, t);
263 template <
typename T>
264 inline oarchive& operator<<(oarchive& oarc,
const T& t) {
265 archive_detail::serialize_impl<oarchive,
267 gl_is_pod<T>::value >::exec(oarc, t);
276 template <
typename T>
277 inline oarchive_soft_fail& operator<<(oarchive_soft_fail& oarc,
279 archive_detail::serialize_impl<oarchive_soft_fail,
281 gl_is_pod<T>::value >::exec(oarc, t);
289 inline oarchive& serialize(oarchive& oarc,
291 const size_t length) {
293 oarc.write(reinterpret_cast<const char*>(str),
294 (std::streamsize)length);
295 assert(!oarc.fail());
303 inline oarchive_soft_fail& serialize(oarchive_soft_fail& oarc,
305 const size_t length) {
307 oarc.write(reinterpret_cast<const char*>(str),
308 (std::streamsize)length);
309 assert(!oarc.fail());
328 #define BEGIN_OUT_OF_PLACE_SAVE(arc, tname, tval) \
329 namespace graphlab{ namespace archive_detail { \
330 template <typename OutArcType> struct serialize_impl<OutArcType, tname, false> { \
331 static void exec(OutArcType& arc, const tname & tval) {
333 #define END_OUT_OF_PLACE_SAVE() } }; } }