开发者

Generating a set of methods for checking messages' content

开发者 https://www.devze.com 2023-01-14 22:32 出处:网络
in my unit test framework, for some of the messages ( which are simply POD structures ) I need a method to compare two such messages ( structs ) for equality of all fields. That is if for example I ha

in my unit test framework, for some of the messages ( which are simply POD structures ) I need a method to compare two such messages ( structs ) for equality of all fields. That is if for example I have a message:

struct SExampleMessage
{
    int someField;
    int someField2;
    char someField3[10];
};

I have a method that takes two pointers to SExampleMessage and returns true if all fields are equal in both structs:

bool compareExampleMessage(SExampleMessage* expectedMsg,
                           SExampleMessage* receivedMsg);

( The pointers of course could be void* and casted to the correct type )

My question is - is there any way to avoid writing this boilerplate code while staying typesafe and in the C++ realm? So instead of:

  1. One generic method that simply compares the binary content of the messages ( getting two void pointers and the size of the structure )

  2. Some external script that analyses the header file of the structure and generates the comparing method

is there any开发者_Go百科 metaprogramming voodoo that makes similar thing? Possibly there isn't because then someone would easily implement C++ reflection with it, but it's worth a try :).


If the structures are plain POD (no pointer internals) then you don't need to have a function and doing var A == var B of the same type is fine.

In C++0x they even relaxed the POD rules to allow classes with constructors and other things to remove this burden of boilerplate

http://www2.research.att.com/~bs/C++0xFAQ.html#PODs

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号