File: SerializationUtils.h

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (80 lines) | stat: -rw-r--r-- 2,177 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#pragma once
#include "Serialization.h"
#include "Core/Array.h"

namespace storm {

	/**
	 * This file contains some utilities for using the serialization interface in C++.
	 *
	 * In particular, this file contains:
	 * - Serialization/deserialization of primitive types.
	 * - Serialization/deserialization of arrays/maps/etc. (currently only when they contain objects).
	 */
	template <class T>
	struct Serialize {};

	template <class T>
	struct Serialize<T *> {
		static T *read(ObjIStream *from) {
			return (T *)from->readClass(StormInfo<T *>::type(from->engine()));
		}
		static void write(T *value, ObjOStream *to) {
			value->write(to);
		}
	};

	// Helper struct for the primitives.
	template <class T, StoredId id> // , void (OStream::*write)(T)>
	struct PrimitiveSerialize {
		static T read(ObjIStream *from) {
			T result;
			from->readPrimitiveValue(id, &result);
			return result;
		}

		static void write(T value, ObjOStream *to) {
			to->startPrimitive(id);
			to->to->writeT(value);
			to->end();
		}
	};

	template <>
	struct Serialize<Bool> : PrimitiveSerialize<Bool, boolId> {};
	template <>
	struct Serialize<Byte> : PrimitiveSerialize<Byte, byteId> {};
	template <>
	struct Serialize<Int> : PrimitiveSerialize<Int, intId> {};
	template <>
	struct Serialize<Nat> : PrimitiveSerialize<Nat, natId> {};
	template <>
	struct Serialize<Long> : PrimitiveSerialize<Long, longId> {};
	template <>
	struct Serialize<Word> : PrimitiveSerialize<Word, wordId> {};
	template <>
	struct Serialize<Float> : PrimitiveSerialize<Float, floatId> {};
	template <>
	struct Serialize<Double> : PrimitiveSerialize<Double, doubleId> {};

	// Array of something (currently only objects).
	template <class T>
	struct Serialize<Array<T> *> {
		static Array<T> *read(ObjIStream *from) {
			return (Array<T> *)from->readClass(StormInfo<Array<T> *>::type(from->engine()));
		}

		static void write(Array<T> *value, ObjOStream *to) {
			if (!to->startClass(StormInfo<Array<T> *>::type(to->engine()), value))
				return;

			Serialize<Nat>::write(value->count(), to);
			for (Nat i = 0; i < value->count(); i++) {
				Serialize<T>::write(value->at(i), to);
			}

			to->end();
		}
	};

}