Jason
Jason

Reputation: 2197

converting C++ structure to C# structure at build time

So here's the background: We have a legacy program that writes data logs in C++. the data is contained in different structures. The program that reads the log files uses those same structures to display the data. I rewrote the program that reads the log files and C# and had to create C# copy of all those structures by hand. Is there a better way to do this? I have considered setting up a lookup path to the structures and a sort of parser that would generate a C# structure at build time, but it seems excessively complicated to handle all the special cases. Are there any suggestions to do this? it seems kind of ridiculous that C# doesn't have any backwards compatibility to handle C/C++ structures.

Upvotes: 2

Views: 208

Answers (2)

petebu
petebu

Reputation: 1837

Summary of your problem: You have a large number of structs in an existing C++ program that you serialize to disk. You want to port the structs to C# so you can deserialize the data from disk into your C# program. You don't just want to do this once. You want to keep the two sets of structs in sync as both programs evolve.

What you need is an Interface Definition Language (IDL) in which you can describe your data in a language independent way. Something like Apache Thrift, Google Protocol Buffers or MessagePack.

The steps you'd have to take would be:

  1. Convert your existing C++ structs into IDL. This is a one-off process. Use a custom script or look for an existing one. Someone must have solved this by now.
  2. Setup your build system to generate both the C# and the C++ definitions at build time.
  3. Use the Thrift/ProtoBuf/MessagePack C# and C++ APIs to serialize and deserialize your data as needed.

The disadvantages are:

  1. You need build-time code generation. But you already considered this yourself and this way the work has already been done for you.
  2. You will have to change both your C++ and C# to correctly use whatever data structures are generated for you.
  3. The binary on disk format will change so legacy logs won't be readable. But you can write some C++ to convert them to the new format quite easily.

I think this is outweighed by the advantages:

  1. The IDL will contain the canonical description of your data. Any changes to it will be reflected in both your C++ and C#. You won't have to manually update your C# version when the C++ one changes.
  2. The binary data format will be machine-independent. You will be able to read/write your data from many different languages on a variety of platforms.
  3. The third-party libraries I mentioned are robust and widely used. Better than hacking something yourself.

Upvotes: 0

Roy Dictus
Roy Dictus

Reputation: 33149

How many structures are there and how complicated are they?

It's a costs vs benefits question I'd say. I'll bet that, judging from your question, just quickly coding the structs in C# is the best way to go.

Just my 2 cents, before taxes...

Upvotes: 1

Related Questions