5

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 ?

Alex Garcia
  • 388
  • 2
  • 10
  • 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 01 '12 at 09:47

3 Answers3

5

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.

Bruce Ediger
  • 3,535
  • 16
  • 16
  • 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 01 '12 at 09:50
1

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

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

Peter K.
  • 3,828
  • 1
  • 23
  • 34
1

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.

k rey
  • 571
  • 3
  • 9
  • 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 01 '12 at 09:52