I have a configuration file, that is a json. I have created a class (ConfigFile) that reads that file and store the values (using boost parser and ptree). I am wandering is it a good practice to use the ptree as a member of the ConfigFile class, or I shall use it just for reading the json and store the values in a map member?
Asked
Active
Viewed 62 times
0
-
it's a matter of taste and judgement – sehe Oct 16 '15 at 18:29
-
Can you be more explicit, like give some examples, please? – sop Oct 19 '15 at 08:02
-
Do you have time for that? If not, why would I? – sehe Oct 19 '15 at 08:31
-
The idea is that I thought to use the ptree as a private member of the class and in the getValForField() function I do the ptree::get and verification that the value is correct (like not empty for string, or positive of between bounds for int), but I got a colleague suggestion to use a map and use the ptree just for reading and do all the verifications in the constructor. What would you suggest and why? :) – sop Oct 19 '15 at 09:01
-
1Ask your colleague? A good reason could be: less code changes if implementation changes. Another classic reason: keep property tree out of the header. The latter can also be fixed with the pimpl idiom – sehe Oct 19 '15 at 11:06
-
Well he left a while ago (from the enterprise), so what do you suggest? Do you have an opinion, or 2 ? :) – sop Oct 21 '15 at 13:25
-
1I already gave you mine. It's worth more to decide for the right reasons than because someone gave you blank advice – sehe Oct 21 '15 at 13:26
-
Ok, my opinion is to have the map, like this I am throwing the exceptions at reading time, if any miss-format – sop Oct 21 '15 at 13:28
-
Sounds like a good reason. Everything has flipsides (Eg likely you just hard coded the mapped values to std::string). It would be hard/impossible for an outsider to with these factors – sehe Oct 21 '15 at 13:31
1 Answers
2
I'd say what matters is the ConfigFile
's interface. If you can keep it consistent with either version, it shouldn't be a problem to just choose one and switch to the other if you feel the need without breaking anything.
Keep property tree out of the header. The latter can also be fixed with the pimpl idiom.
@sehe's comment makes a lot of sense here as well and is something to remember.

Bartek Banachewicz
- 38,596
- 7
- 91
- 135
-
so that's also why not saving all in the ptree and just do reads in the "getters"... :) – sop Oct 21 '15 at 13:57