2

As implied by the title of the Q, I have a few entities and need to keep them as constant objects in a C++ project. Each entity contains very complex data structures (lists of enums, maps of maps, etc.). In addition, it is possible in future to have more entities for extending the project. For keeping the complex data of the entities, two approaches are proposed:

  1. Keep them as some external .xml files and load them when the project starts.
    • This needs lots of convertors in the code to convert my C++ types into/from strings coming from xml file.
    • I lose the type information in the .xml file. As a result, some changes in C++ code in future may require some changes in .xml files that cannot be easily detected.
  2. Keep them as some constant C++ object in some C++ files.
    • For extending the project in future, I need to touch the code.

What do you recommend in this case? Keeping these complex constant data structures in the .xml files? or in C++ files?

TonySalimi
  • 211
  • 1
  • 6
  • Why would you lose type information? C++ is at least as strongly typed as XML. Just bite the bullet and implement a proper Interpreter pattern so that you'll never need to deal with it again. – Kilian Foth Oct 12 '22 at 08:50
  • @KilianFoth Well, everything in xml is string, no? So, it means that the type system information that I have in C++ code will be lost in xml files. In addition, as far as I understand, you are a fan of the .xml files approach, right? – TonySalimi Oct 12 '22 at 09:54
  • 1
    @ToniSalimi Oh, you meant you lose type information *when converting tp XML*! But that's not really true either - XML supports *schemas* which allow you to express exactly what kind of elements are allowed to go where. – Kilian Foth Oct 12 '22 at 11:12
  • @KilianFoth Even in that case, how I can map my C++ types into XML files? – TonySalimi Oct 12 '22 at 12:14
  • @TonySalimi take a look at XSD, they are XML files used to define XML files. You can alternatively use JSON which has a bit of type in it too. – f222 Oct 12 '22 at 12:42

2 Answers2

3

If this data is the same for all users, then you might as well have a constant C++ object, and one source file where it is initialised. It's a C++ source file, but an XML file would also be a source file. And writing it in C++ means you have type checking and you don't have to write code for it. If a new version changes the layout of this class, you can change it all in one place.

If the data is different for different users, say you have 100 companies as customers and there is some data specific to each customer, I'd probably but it into 100 xml files, so you don't have to recompile and re-link your application code. In that situation I'd probably have ONE database with the information for all customers, and a tool that can generate the XML file for any specific customer, put it into a zip file together with the rest of the application, and mail it to the customer. That's especially useful if the layout of this constant data changes, so you only need to change your database and your generator tool once.

gnasher729
  • 42,090
  • 4
  • 59
  • 119
1

You wrote for #2

For extending the project in future, I need to touch the code.

but so you would have to do for solution #1 with your XML file, if you are the maintainer of both. And when you have to extend your project in the future, and these changes include structural changes, you would have to change the XML files and your C++ code.

An external storage file brings you only benefits when someone else shall maintain it, someone who gets only the compiled binary of your C++ program and is not allowed or qualified to make changes to the program itself. If that's your scenario, you need something like an XML file together with some serialization / deserialization mechanism.

Doc Brown
  • 199,015
  • 33
  • 367
  • 565
  • An external XML file earns it's keep if it contains data that changes value while the application code stays the same. I have used them to hold tax rate data that can change yearly. I managed to convince the test lead that if the automated regression tests pass, it was only necessary to check results for the new year in detail. – kiwiron Oct 13 '22 at 02:55
  • @kiwiron: "someone else" is just a thought model. Surely the dev who wrote the program can take the role of "someone else". The point is, **the OP** has to analyse **their requirements** - is there a process in their system's context for changing the content of a separate configuration file separately from the program, or isn't there such a process? That's something the OP must answer, we cannot decide that for them. – Doc Brown Oct 14 '22 at 16:12