7 #ifndef TURI_SERIALIZE_HPP 8 #include <core/storage/serialization/serialize.hpp> 12 #ifndef TURI_OARCHIVE_HPP 13 #define TURI_OARCHIVE_HPP 17 #include <core/logging/assertions.hpp> 18 #include <core/storage/serialization/is_pod.hpp> 19 #include <core/storage/serialization/has_save.hpp> 20 #include <core/util/branch_hints.hpp> 82 std::ostream* out = NULL;
84 std::vector<char>* vchar = NULL;
94 inline oarchive(std::vector<char>& vec) {
104 inline void expand_buf(
size_t s) {
111 buf = (
char*)realloc(buf, len);
118 inline void write(
const char* c, std::streamsize s) {
121 memcpy(buf + off, c, s);
127 template <
typename T>
128 inline void direct_assign(
const T& t) {
130 expand_buf(
sizeof(T));
131 std::memcpy(buf + off, &t,
sizeof(T));
135 out->write(reinterpret_cast<const char*>(&t),
sizeof(T));
139 inline void advance(
size_t s) {
144 out->seekp(s, std::ios_base::cur);
150 return out == NULL ? false : out->fail();
153 std::string get_prefix() {
154 ASSERT_NE(dir, NULL);
176 : oarc(new
oarchive(outstream)), mine(true) { }
187 inline void write(
const char* c, std::streamsize s) {
190 template <
typename T>
191 inline void direct_assign(
const T& t) {
192 oarc->direct_assign(t);
199 std::string get_prefix() {
200 return oarc->get_prefix();
204 if (mine)
delete oarc;
208 namespace archive_detail {
211 template <
typename OutArcType,
typename T>
212 struct serialize_hard_or_soft_fail {
213 inline static void exec(OutArcType& oarc,
const T& t) {
219 template <
typename T>
220 struct serialize_hard_or_soft_fail<oarchive_soft_fail, T> {
221 inline static void exec(oarchive_soft_fail& oarc,
const T& t) {
225 save_or_fail(*(oarc.oarc), t);
236 template <
typename OutArcType,
typename T,
bool IsPOD,
typename Enable =
void>
237 struct serialize_impl {
238 static void exec(OutArcType& oarc,
const T& t) {
239 serialize_hard_or_soft_fail<OutArcType, T>::exec(oarc, t);
244 template <
typename OutArcType,
typename T>
245 struct serialize_impl<OutArcType, T, true> {
246 inline static void exec(OutArcType& oarc,
const T& t) {
247 oarc.direct_assign(t);
255 template <
typename OutArcType,
typename T>
256 struct serialize_impl<OutArcType, const T, true> {
257 inline static void exec(OutArcType& oarc,
const T& t) {
258 serialize_impl<OutArcType, T, true>::exec(oarc, t);
265 template <
typename OutArcType,
typename T>
266 struct serialize_impl<OutArcType, const T, false> {
267 inline static void exec(OutArcType& oarc,
const T& t) {
268 serialize_impl<OutArcType, T, false>::exec(oarc, t);
281 template <
typename T>
283 archive_detail::serialize_impl<
oarchive,
294 template <
typename T>
309 const size_t length) {
311 oarc.
write(reinterpret_cast<const char*>(str),
312 (std::streamsize)length);
313 assert(!oarc.
fail());
323 const size_t length) {
325 oarc.
write(reinterpret_cast<const char*>(str),
326 (std::streamsize)length);
327 assert(!oarc.fail());
346 #define BEGIN_OUT_OF_PLACE_SAVE(arc, tname, tval) \ 347 namespace turi{ namespace archive_detail { \ 348 template <typename OutArcType> struct serialize_impl<OutArcType, tname, false> { \ 349 static void exec(OutArcType& arc, const tname & tval) { 351 #define END_OUT_OF_PLACE_SAVE() } }; } } std::string get_next_write_prefix()
void write(const char *c, std::streamsize s)
oarchive_soft_fail(std::ostream &outstream)
constructor. Takes a generic std::ostream object
When this archive is used to serialize an object, and the object does not support serialization...
general_ofstream * get_output_stream()
void write(const char *c, std::streamsize s)
The serialization output archive object which, provided with a reference to an ostream, will write to the ostream, providing serialization capabilities.
oarchive(std::ostream &outstream)
constructor. Takes a generic std::ostream object
Tests if T is a POD type.
bool fail()
Returns true if the underlying stream is in a failure state.