Wohoo! We have core serialisation, both encode and decode.
Cut the example out and try it (on the very latest Felix only of course).
Try your own test cases!

Non-pod primitives handled: string. 
So don't try regexps, judy arrays, etc.
No pointers, so don't try lists.

The code below is core, that is, it assumes
the receiver knows the type. The resulting
object is an *untracked* Felix object of course.
[We just build it in a malloc'ed extent]

/////////////////////////////////////////////////////////

proc test [T with Str[T]] (p: &T) {
  // Print all the info about the pointer
  //Collector::print_pointer_data p;

  // Get the shape from the info.
  var rtti = p.address.Collector::get_pointer_data.shape;

  // get the encoder and print the address (to make sure it isn't NULL)
  var encoder = rtti.Rtti::encoder;
  //println$ "Encoder = " + (C_hack::cast[address] encoder).str;

  // get the decoder and print the address (to make sure it isn't NULL)
  var decoder = rtti.Rtti::decoder;
  //println$ "Decoder = " + (C_hack::cast[address] decoder).str;

  // Encode the data
  var encoded_string = encoder p.address;

  // Print the encoded data (will contain binary crap).
  //println$ "Encoded=" + encoded_string;

  // get pointer to the internal array because decoders use
  // arrays whereas encoders use strings.
  var sarr =  encoded_string.cstr;

  // Allocate new storage for a copy of the data.
  var slen = C_hack::sizeof[T];
  var store = C_hack::malloc (slen);

  // decode the encoded data into the store.
  var endix = decoder (store, sarr, 0uz);

  // Check the decoder consumed exactly the string length.
  //println$ "End index = " + endix.str + " should = " + encoded_string.len.str;
  assert endix == encoded_string.len;

  // Now cast the store to the expected type and print it out.
  var scopy = C_hack::cast[&T] store;
  println$ "Decoded copy = " + str (*scopy);
}

// Our first test case is just a string, we know these are nasty.
var p = new "Hello";
println$ *p;
test p;

println$ "-" * 20;
// Our second test case is a struct, containing
// a pod struct and a string.

// The nested struct.
struct X {
  a:int;
  b:int;
};

// The test case.
struct Y {
  c:X;
  d:string;
}

// We need to print it.
instance Str[Y] {
  fun str (y: Y) =>
    "This is a Y: (c:(" + y.c.a.str + ","+y.c.b.str+"),d:"+y.d+")"
  ;
}

// A sample value.
var c = new Y ( X(1,2), "hello world");
println$ c*.str;
test c;

println$ "-" * 20;

// Another example:
var d = new (1,2.3,"World",99L);
println$ *d;
test d;
//////////////////////////////////////////////////////
~/felix>flx --test=build/release ts
Hello
Decoded copy = Hello
--------------------
This is a Y: (c:(1,2),d:hello world)
Decoded copy = This is a Y: (c:(1,2),d:hello world)
--------------------
(1, 2.3, World, 99)
Decoded copy = (1, 2.3, World, 99)
///////////////////////////////////////////////////////
--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Free Next-Gen Firewall Hardware Offer
Buy your Sophos next-gen firewall before the end March 2013 
and get the hardware for free! Learn more.
http://p.sf.net/sfu/sophos-d2d-feb
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to