Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I know you're asking for a library. If you can't find one (::boggle::, you'd think this was a solved problem!), here is an outline for a solution:</p> <p>You should be able to write a code generator[1] to serialize trees/graphs without (run-time) pre-processing fairly simply. </p> <p>You'll need to parse the node structure (<code>typedef</code> handling?), and write the included data values in a straight ahead fashion, but treat the pointers with some care.</p> <ul> <li><p>For pointer to other objects (i.e. <code>char *name;</code>) which you <em>know</em> are singly referenced, you can serialize the target data directly. </p></li> <li><p>For objects that might be multiply refernced and for other nodes of your tree you'll have to represent the pointer structure. Each object gets assigned a serialization number, which is what is written out in-place of the pointer. Maintain a translation structure between current memory position and serialization number. On encountering a pointer, see if it is already assigned a number, if not, give it one and queue that object up for serialization.</p></li> </ul> <p>Reading back also requires a node-#/memory-location translation step, and might be easier to do in two passes: regenerate the nodes with the node numbers in the pointer slots (bad pointer, be warned) to find out where each node gets put, then walk the structure again fixing the pointers.</p> <p>I don't know anything about tpl, but you might be able to piggy-back on it.</p> <hr> <p>The on-disk/network format should probably be framed with some type information. You'll need a name-mangling scheme.</p> <hr> <p>[1] <A href="http://root.cern.ch/" rel="noreferrer">ROOT</A> uses this mechanism to provide very flexible serialization support in C++.</p> <hr> <p><strong>Late addition:</strong> It occurs to me that this is not always as easy as I implied above. Consider the following (contrived and badly designed) declaration:</p> <pre><code>enum { mask_none = 0x00, mask_something = 0x01, mask_another = 0x02, /* ... */ mask_all = 0xff }; typedef struct mask_map { int mask_val; char *mask_name; } mask_map_t; mask_map_t mask_list[] = { {mask_something, "mask_something"}, {mask_another, "mask_another"}, /* ... */ }; struct saved_setup { char* name; /* various configuration data */ char* mask_name; /* ... */ }; </code></pre> <p>and assume that we initalize out <code>struct saved_setup</code> items so that <code>mask_name</code> points at <code>mask_list[foo].mask_name</code>.</p> <p>When we go to serialize the data, what do we do with <code>struct saved_setup.mask_name</code>?</p> <p><strong>You will need to take care in designing your data structures and/or bring some case-specific intelligence to the serialization process.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload