Serializer / Pointers

Blitz3D Forums/Blitz3D Userlibs/Serializer / Pointers

Dakkaron(Posted 2014) [#1]
I created a library that allows one to save or send objects of any arbitrary Type with just one line of code (or more if you need any metadata). For that I also created an userlib that allows access to C-pointers so the serializer can read the fields of objects.

So first the Serializer:

    serialize(ptr%, format$, stream%) 
Serializes the given object according to the given format and writes it into the given stream
ptr%    : Pointer to the object that will be serialized (you can get that using pointers_getPointer() )
format$ : Here you should put a string containing all fields of the object according this syntax: 
         b = byte 
         h = short 
         i = int 
         f = float 
         s = String 
         x = Field will be ignored 
stream$ : The file- or network-stream that will be written to 

    deserialize(ptr%, format$, stream%) 
Reads an object from the stream and writes the data into the given object according to the given format.
ptr%    : Pointer to the object that will be written to (you can get that using pointers_getPointer() )
format$ : Has to be the same format that was used to serialize the object 
stream$ : The file- or network-stream that will be read from


There are full samples for saving and sending over a network in the download, but here are some short samples:

Type TTest
    Field a%
    Field b#
    Field do_not_save
    Field c$
End Type

Function saveTest(stream%)
    For t.TTest = each TTest
        serialize(pointers_getPointer(t), "ifxs", stream%)
    Next
End Function

Function loadTest(stream%)
    While Not Eof(stream%)
        t.TTest = new TTest
        deserialize(pointers_getPointer(t), "ifxs", stream%)
    Wend
End Function


Sending over network works pretty much the same, but have a look at the samples if you want to.


Pointers:

Pointers can cause you a lot of headache if you don't know what you are doing, so if you don't know what pointers are, better stay away from them and just use the Serializer.
To put it simple: they allow read and write access to any arbitrary point of the memory. This allows e.g. to read an object of a type without having to create a variable of that type. This allows for more dynamic programming (e.g. the Serializer, which is able to read or write any object without actually knowing much about that object). On the other hand they can cause quite a lot of trouble (e.g. random crashes or variables that have a wrong value for no apparent reason) if you use them the wrong way.

The pointers library offers these functions:

    pointers_getPointer%( obj* ) 
Returns a pointer to the given Object. This object needs to be an object of a type, so it can't be a primitive like an int, float or string, and it also can't be a Dim-array or Blitz-array.
This pointer is necessary for the following commands.
The pointer will point to the address of the first field of the object. If you want to access the other fields, you have to add 4 for each field. So if you want to access the second field, add 4 to the pointer. If you want to access the third field, add 8, and so on.

    pointers_getInt%( ptr% ) 
    pointers_getFloat#( ptr% ) 
    pointers_getString$( ptr% ) 
These commands return the value of the field that the given pointer points to.
Attention: getString is not save. That means, if you call it with a ptr% that doesn't point to a string, the program might crash or perform in any other unexpected way!

    pointers_setInt( ptr%, val% ) 
    pointers_setFloat( ptr%, val# ) 
    pointers_setString( ptr%, val$ ) 
These commands set the value of the field that is at the position the given pointer points to to the given value.
Attention: None of these commands are save. That means, if you call it with a ptr% that doesn't point to a field of the given type (int / float / string), the program might crash or perform in any other unexpected way!


Download
Download Pointers only