Serilization basics working
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import vector
|
||||
import conversions
|
||||
import mem
|
||||
import util
|
||||
|
||||
fun serialize<T(Serializable)>(it: T): vector::vector<char> {
|
||||
return it.serialize()
|
||||
}
|
||||
|
||||
fun serialize<T>(it: T): vector::vector<char> {
|
||||
var char_data = conversions::cast_ptr<*T,*char>(&it)
|
||||
@@ -10,8 +15,17 @@ fun serialize<T>(it: T): vector::vector<char> {
|
||||
return toRet
|
||||
}
|
||||
|
||||
fun unserialize<T>(it: vector::vector<char>):T {
|
||||
return *conversions::cast_ptr<*char,*T>(it.getBackingMemory())
|
||||
// dead simple wrapper for ease of use
|
||||
fun unserialize<T>(it: vector::vector<char>): T {
|
||||
return unserialize<T>(it, 0).first
|
||||
}
|
||||
fun unserialize<T>(it: vector::vector<char>, pos: int): util::pair<T,int> {
|
||||
return util::make_pair(*conversions::cast_ptr<*char,*T>(it.getBackingMemory()+pos), pos + mem::sizeof<T>())
|
||||
}
|
||||
fun unserialize<T(Serializable)>(it: vector::vector<char>, pos: int): util::pair<T,int> {
|
||||
var toRet: T
|
||||
pos = toRet.unserialize(it, pos)
|
||||
return util::make_pair(toRet, pos)
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,6 +18,23 @@ fun make_pair<T,U>(first: T, second: U): pair<T,U> {
|
||||
return it
|
||||
}
|
||||
|
||||
// little ugly, but it works
|
||||
fun unpack<T,U>(first: ref T, second: ref U): unpack_dummy<T,U> {
|
||||
var toRet: unpack_dummy<T,U>
|
||||
toRet.first = &first
|
||||
toRet.second = &second
|
||||
return toRet
|
||||
}
|
||||
obj unpack_dummy<T,U> {
|
||||
var first: *T
|
||||
var second: *U
|
||||
fun operator=(p: ref pair<T,U>) {
|
||||
*first = p.first
|
||||
*second = p.second
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
obj pair<T,U> (Object) {
|
||||
var first: T
|
||||
var second: U
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import mem:*;
|
||||
import util:*;
|
||||
import io:*;
|
||||
import serialize:*;
|
||||
import util:*;
|
||||
|
||||
fun vector<T>():vector<T> {
|
||||
var out.construct():vector<T>
|
||||
@@ -13,7 +15,7 @@ fun vector<T>(in:T):vector<T> {
|
||||
return out
|
||||
}
|
||||
|
||||
obj vector<T> (Object) {
|
||||
obj vector<T> (Object, Serializable) {
|
||||
var data: *T;
|
||||
var size: int;
|
||||
var available: int;
|
||||
@@ -38,6 +40,23 @@ obj vector<T> (Object) {
|
||||
maybe_copy_construct(&data[i], &old->data[i]);
|
||||
//addEnd(old->get(i))
|
||||
}
|
||||
fun serialize(): vector<char> {
|
||||
var toRet = serialize(size)
|
||||
for (var i = 0; i < size; i++;)
|
||||
toRet += serialize(data[i])
|
||||
return toRet
|
||||
}
|
||||
fun unserialize(it: vector<char>, pos: int): int {
|
||||
unpack(size, pos) = unserialize<int>(it, pos)
|
||||
data = new<T>(size)
|
||||
available = size
|
||||
for (var i = 0; i < size; i++;) {
|
||||
var curr = unserialize<T>(it, pos)
|
||||
pos = curr.second
|
||||
maybe_copy_construct(&data[i], &curr.first);
|
||||
}
|
||||
return pos
|
||||
}
|
||||
|
||||
fun destruct(): void {
|
||||
if (data)
|
||||
|
||||
@@ -1 +1,3 @@
|
||||
7 = 7
|
||||
9 , 11 = 9 , 11
|
||||
1 2 3 4 5
|
||||
|
||||
@@ -1,14 +1,48 @@
|
||||
import io:*
|
||||
import serialize:*
|
||||
import string:*
|
||||
import util:*
|
||||
import vector:*
|
||||
import vector_literals:*
|
||||
|
||||
fun main():int {
|
||||
var intA = 7
|
||||
write_file_binary(string("bintest.bin"), serialize(intA))
|
||||
var intB = unserialize<int>(read_file_binary(string("bintest.bin")))
|
||||
var intAout = unserialize<int>(read_file_binary(string("bintest.bin")))
|
||||
print(intA)
|
||||
print(" = ")
|
||||
println(intB)
|
||||
println(intAout)
|
||||
|
||||
// ok, lets do more than one
|
||||
|
||||
var intB = 9
|
||||
var intC = 11
|
||||
write_file_binary(string("bintest.bin"), serialize(intB) + serialize(intC))
|
||||
var bin = read_file_binary(string("bintest.bin"))
|
||||
// remember, these are pairs
|
||||
var intBout: int
|
||||
var intCout: int
|
||||
var pos = 0
|
||||
unpack(intBout, pos) = unserialize<int>(bin, pos)
|
||||
unpack(intCout, pos) = unserialize<int>(bin, pos)
|
||||
print(intB)
|
||||
print(" , ")
|
||||
print(intC)
|
||||
print(" = ")
|
||||
print(intBout)
|
||||
print(" , ")
|
||||
println(intCout)
|
||||
|
||||
|
||||
// ok, lets do a vector
|
||||
write_file_binary(string("bintest.bin"), serialize(vector(1,2,3,4,5)))
|
||||
bin = read_file_binary(string("bintest.bin"))
|
||||
var back = vector<int>()
|
||||
pos = 0
|
||||
unpack(back, pos) = unserialize<vector<int>>(bin, 0)
|
||||
back.for_each(fun(i: int) { print(i); print(" "); })
|
||||
println()
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user