Thursday 15 March 2012

c++ - validating structures - how to pass string to sizeof and offset methods? -


My programs have auto-generated structures when I need to validate them with "server" information - Then I can be sure that my automatically generated structure is up to date server and local structure are valid if they are of the same size, then there are fields with the same name and size (and also validated the type should go).

This is what I have written so far: Null Planivator :: Validate (cg_scheme_desc_t * schemedesc, logger and logger) {struct cg_message_desc_t * msgdesc = schemedesc- & gt; ; message; While (msgdesc) {struct cg_field_desc_t * fielddesc = msgdesc- & gt; Field; Char * structName = msgdesc- & gt; Name; Size_t structSize = msgdesc- & gt; Shape; Logger.Debug ("Message% s, Block Size =% d", structName, structSize); If (strcmp (structName, "order")) {ifof (order)! = StructSize) {printf ("Verifier error, structure 'command', local size =% d server size =% d! '', Sizeof), straight-size); Std :: Exception throw ("Verifier error, composition 'command' wrong size! ');} While (fieldDesk) {logger.debug (" \ tField% s =% s [size =% d, offset =% d] " , Field-dc- & gt; name, field-dsc-> type, field-dc- & gt; shape, fielddesk -> offset); if (offsetf (structure order, field-DC- & gt; Name) = field-dc-> offset) {std :: exception ("order structure wrong offset")}} TODO: Field-ddc-> Size == field in field structure corresponding to field = Dsc = fielddesc-> next;}} and

There are many problems:

> Was written (if stringm (structure name, "order")) because afterwards i size (order) and offsetof (struct command, field-> name) There are many expressions needed to use the command . But I have many structures and I copy-paste this block for each of them Interpretation will. sizeof and Offsetof passed the string verbatim to the methods of any kind may or desired effect in some other way?

offsetof (struct command, fielddesc- & gt; name) does not work for the same reason - the second parameter can not be a string, I error C2039: 'Fielddesc' is available: not a member of 'order' error

for the same reason, I will call fielddesc-> I can validate size .

How can I get the desired verification without deep copying and how can I get the values ​​of hard-coding?

I believe that you define structs by typing structs and using a templated function By doing so you can complete whatever you want. The following should be replaced by what you have (clearly not tested)

  struct command type {typedef cg_field_desc_t * ListItem; Typedef order class; Static constant std :: string name; }; Std :: string command type :: name = "command"; Template & lt; Class T & gt; CheckStuff (T :: ListItem fielddesc, const char * name, size_t structSize) {if (strcmp (name, T :: Name.c_str ())} {ifof (T :: class)! = StructSize} {printf ( "Verifier error, structure 'order', local size =% d server size =% d!", Sizeof (T :: class), structSize); Std :: Exception throw ("Verifier error, composition 'command' wrong size! ');} While (fielddesk) {wooden.debug (" \ tField% s =% s [size =% d, offset =% d] " , Field-dc- & gt; name, field-dcc- & gt; type, field-dc- & gt; shape, fielddesk - & gt; offset); if (offset (T: class, fielddesk-> Name) = field-dc-> offset) {studs :: exception ("order structure offset wrong") //} TODO: Field-ddc-> Size == field in field structure corresponding to field = Dsc = fielddesc-> next;}} and   

Then you can set your if statement to

  checkStuff & lt; OrderType &   

You can define new structs and extend it to other types

  struct other Type {typedef some_other_t * ListItem; typingf bizbau class; static constant std :: string name;}; std :: string other type :: name = "foobar";    

No comments:

Post a Comment