Note that there are some explanatory texts on larger screens.

plurals
  1. POC run time type checking
    primarykey
    data
    text
    <p>I have a C library storing records with a number of fields. Schema are read in from a text file, including the type of each field in the record.</p> <p>To simplify for question purposes, imagine I have</p> <pre><code>typedef enum my_type_enum { INT32, //32-bit integer MYSTRUCT, //some struct I have, details irrelevant ... } my_type_enum; typedef struct my_var { my_type_enum typetag; unsigned char* data; } my_var; my_var myrecord[numfields]; </code></pre> <p>The schema file says whether each field of myrecord should hold an int32_t or a mystruct. My library reads the schema file and for each my_var in myrecord sets the tag and allocates the right amount space for the data.</p> <p>my_var is opaque and client programs basically use, for simple data</p> <pre><code>void set(my_var* record, size_t field, void * src) { memcpy(record[field].data, src, datatypes[record[field].typetag].size); } int32_t x = 5; set(myrecord, 0, &amp;x); </code></pre> <p>to store a value in a record, and a similar get() to take things out.</p> <p>The tagged my_var type allows type checking once data is inside the my_var, but if the schema says record holds three INT32s there is of course nothing to check that src points to an int32_t and not a mystruct when you're trying to set() data into that my_var.</p> <p>Obviously the check needs to take place in something wrapping set(), before the int32_t* or mystruct* is converted to void*. I have seen compile-time checking with typeof() trickery. I feel like what I want probably isn't possible, but you never know all the tricks... </p> <p>Is there anything I can do better than providing facilities that read a schema at client program compilation time and generate a set_CHECKED() wrapper macro that will give a compiler error if someone tries to copy an int32_t into a my_var tagged to hold mystruct? GCC extensions are fine.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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