[LC++]Containers of Containers

Paul M Foster paulf at quillandmouse.com
Mon Dec 23 11:31:01 UTC 2002


I'm looking at creating a program to read various configuration files,
with various syntaxes. I can use flex et al to parse the various config
syntaxes, but all of them share the characteristic of having a name
equal some value. In some cases, config files will yield nested
name/value pairs, like Windows ini files. As in:

computer {
	chassis = ATX
	hard_drive {
		type = SCSI
		capacity = 40G
	}
	mouse = optical
}

Such nesting could go on presumably ad infinitum. The problem is this:
I'd like to represent the various "configurations" in a file as C++
classes, which might contain other configurations, which might contain
other configurations. Each configuration would potentially contain an
array (vector, list) of values. Like this:

class cfg {
	string name;
	...
	list<int> values;
	...
}

But that won't work for configs which contain strings instead of ints,
like this:

class cfg {
	string name;
	...
	list<string> values;
	...
}

Worse, it won't work for "nested" configs (where a config contains a
list of other configs). I thought about a template, where you specify
the type that the class will contain. But that won't work, because if
you have a cfg that contains a list of cfg objects, at some point in the
hierarchy, you're going to have to specify ints, strings or bools.

I could specify what's being contained as a union, like:

union opt_value {
	string *s;
	bool b;
	int i;
	float f;
	cfg *c;
};

But I'm not keen on using unions. Anyone have any ideas on how to do
this? (Sorry if this isn't clear. I'll elaborate, if needed.)

Paul

	



More information about the tuxCPProgramming mailing list