Forum Navigation
A noobs questions to Variant and the "right way" to do things.
Posted: 09/06/08 18:03:37Hi
I am writing a simple module to store my games settings in a map and have some basic D/Tango questions. 1. I was wondering what the recomended way is to overload opEquals, opCmp, and toHash. Is it even needed in my case? 2. Will the way im comparing Variant to null result in problems when I store atomic types and objects? 3. What type of ma should I use to store my settings? atm im using a treemap with the setting name as a key.
module util.Config; import tango.core.Variant; import tango.util.collection.TreeMap; class Setting { this(char[] name, Variant value, Variant defaultValue) { this.name = name; this.value = value; this.defaultValue = defaultValue; } char[] getName() { return name; } Variant getValue() { // TODO problem with null cmp Variant? if(value == null || value.isEmpty) { return defaultValue; } return value; } void setValue(Variant value) { this.value = value; } Variant getDefault() { return defaultValue; } bool opEquals(Setting other) { return this.name == other.name && this.value == other.value && this.defaultValue == other.defaultValue; } int opCmp(Setting other) { if(this == other) return 0; if(this.name > other.name) return 1; return -1; } private { char[] name; Variant value; Variant defaultValue; } invariant() { // setting must have a name assert(name != ""); // default value must be set assert(defaultValue != null); assert(defaultValue.isEmpty == false); // value and default value must be of the same type if(value != null) assert(value.type == defaultValue.type); } }