Assistance with Serializing Objects

37 views
Skip to first unread message

Daniel Selnick

unread,
Apr 14, 2014, 2:47:05 PM4/14/14
to helium...@googlegroups.com
Hi,

I'm using Helium::Persist for saving and loading data.

I have a case where the type translator is deducing an Object as a bool (I think).

Here's a simplified example of what's happening:

class Foo: public Helium::Reflect::Object
{
public:
float X;
float Y;
// Helium declare class ( Foo, Object )
 // populate meta type { type.AddField<Foo, float> etc }
};
// Helium define class

class Bar: public Helium::Reflect::Object
{
public:
Foo* foo;
float Scale;
// Helium declare class (Bar, Object)
// populate meta type { type.AddField<Bar, Foo*> etc }
};
// Helium define class

// To write
{
Bar* bar = new Bar();
bar.foo = new Foo();
// Fill values
ObjectPtr* objPtr = new ObjectPtr(bar);
WriteToFile(filePath, *objPtr);
}

Now when it's being serialized, it's serializing Foo* as a bool, and not as class.
I know I'm doing something wrong...
Maybe Foo needs to be a struct?
Any help/insight will be a great help. :)

Daniel Selnick

unread,
Apr 14, 2014, 3:47:05 PM4/14/14
to helium...@googlegroups.com
So if Foo is a Helium Struct, then this will work ok.

From debugging it, I'm seeing that inside of 
void ArchiveWriterJson::SerializeTranslator( RapidJsonWriter& writer, Pointer pointer, Translator* translator, const Field* field, Object* object )
 
There's a bug where it falls through to boolean scalar translator.

gor...@gmail.com

unread,
Apr 15, 2014, 11:29:57 AM4/15/14
to helium...@googlegroups.com
So a couple of things jump out at me with your code:

1 - Aggregate pointers need to be either an ObjectPtr or StrongPtr<>, not a bare pointer (like your 'foo' member).  We don't interpret a naked pointer inside a structure as being strict enough ownership of the pointed-to-object to have it be processed by the Serialize code.  This is probably causing the behavior you are seeing, but its an interesting edge case we can't easily defend against.
2 - Don't specify the template arguments to AddField, you are only going to buy yourself pain here.  Stick to the example code in Reflect's README.md file.
3 - Don't heap allocate (use new/delete) on a StrongPtr (like your objPtr).  They are cheap to create and destroy, and you don't want the cost of the heap ops unless you absolutely need them (which you don't).  You should omit that local variable entirely, make `bar` a StrongPtr<Bar>, and pass `bar` into ArchiveWriter::WriteToFile.

--
You received this message because you are subscribed to the Google Groups "Helium Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to heliumprojec...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Selnick

unread,
Apr 15, 2014, 6:53:36 PM4/15/14
to helium...@googlegroups.com
Changing Foo* to StrongPtr< Foo > fixed the problem.

After I wrote this, and before your reply, I removed template arguments. You're correct with the fact that they did just buy me pain.

Yup, heap alloc's are costly compared to stack.
Reply all
Reply to author
Forward
0 new messages