I'm writing an interface between two configuration models that use different structures. While I know that there is no "magic" way to do the translation from a structure type to another, I wandered if there might be some best practices or an efficient/effective way to setup such a system. I think it's similar to how programs may manage different configuration structures accross versions.

Here's a simple example: let's say we have a structure of type A which contains 3 parameters:

struct A {
  int alpha;
  bool beta;
  bool charlie;

The second configuration structure share the same parameters, but in a different order, let's see structure B:

struct B {
  union {
    struct {
      char beta : 1;
      char charlie : 1;
    } bit;
    char field;
  int alpha;

A simple program to convert an A to a B would be:

void convert(A const *aelem, B *belem) {
      belem->alpha = aelem->alpha;
      belem->bit.beta = aelem->beta;
      belem->bit.charlie = aelem->charlie;

So, while this may seem fairly simple with such basic structures, I'm dealing with dozens of structures, each of one contains around 50 parameters. So do anybody knows of a good pattern to follow for such a problem ?

  • 3
    It's C or C++? typedef is not required in C++ to define types... – Klaim Jan 31 '12 at 16:28
  • I'd prefer the answer to be oop based so C++. – Alex Garcia Jan 31 '12 at 16:35
  • 1
    Can't help myself, especially if you're going to have dozens of these functions -- I'd recommend using "const" ==> void convert(A const *aelem, B *belem) - safer, but also more importantly, better readability - the const indicates where you're reading & where you're writing. – Radian Jan 31 '12 at 20:03
  • Corrected! You're right, it's not because it's an example that I should not write it correctly ^^ – Alex Garcia Feb 1 '12 at 9:47

Some of the later research into Remote Procedure Call optimization involved problems like this. The only one I can both remember and find is Flux RPC. Those folks wrote a 1995 paper about optimizing RPC that might hold some interest for you. If I recall correctly (I didn't re-read the paper), they had some serialization/marshalling code that did pretty much what you're talking about, and did it as fast or faster than hand-coded marshalling, like ONC RPC.

That said, I think your best bet is to bite the bullet and do it by hand. It's pretty hard to parse C or C++ in the face of typedefs and macros and so forth, so unless you can scrounge up a struct parser, or maybe a system to read struct defs and write code to translate, you'll end up doing it by hand. Be very organized. Do as much mechanical checking as you can. Even counting "to" and "from" elements and ensuring that counts match will catch some errors.

  • I'm affraid I won't have anything to do but "bite the bullet" as you said. Thanks for the paper though, it's very informative ^^ – Alex Garcia Feb 1 '12 at 9:50

If you're using .Net and managed C++ code, you could think about using Automapper.

I haven't found a generic C++ approach, though.

  • Well this is for an embedded linux environment so i'm affraid not. Thanks for the link though i'll look into it. – Alex Garcia Feb 1 '12 at 9:53
  • @AlexGarcia: D'oh! Sorry, it wasn't clear from the question that it was an embedded environment. – Peter K. Feb 1 '12 at 14:06
  • No, my bad for it, that's why I gave you an upvote. This is not a bad answer but this would not work for me ^^ – Alex Garcia Feb 1 '12 at 15:12

There is not a way to manage this in C/C++ during runtime execution since these languages do not support reflection. However, there are methods that can help during design time such as a code generation tool. There are many code generation tools (a simple google will help you identify them).

If you use Excel, you could try the following: Place each of your structures in different columns. Then, using a custom formula (such as name/type matching), populate a column with the intended mapping results. Then simply copy/paste the mapping results into a text file and complete it where necessary. This approach won't get you 100% there but could be a good starting point to help ease the process.

  • Thanks, the problem is the mapping is not always one for one in my case. I have some instances where one enum in A may hold 3 different parameters in the sibling structure in configuration B. Thanks anyway, as this may be very useful in simple cases. – Alex Garcia Feb 1 '12 at 9:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.