comparison mde/lookup/Options.d @ 82:ac1e3fd07275

New ssi file format. (De)serializer now supports non-ascii wide characters (encoded to UTF-8) and no longer supports non-ascii 8-bit chars which would result in bad UTF-8. Moved/renamed a few things left over from the last commit.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 30 Aug 2008 09:37:35 +0100
parents d8fccaa45d5f
children e0f1ec7fe73a
comparison
equal deleted inserted replaced
81:d8fccaa45d5f 82:ac1e3fd07275
180 } 180 }
181 } 181 }
182 182
183 private Logger logger; 183 private Logger logger;
184 static this() { 184 static this() {
185 logger = Log.getLogger ("mde.options"); 185 logger = Log.getLogger ("mde.lookup.Options");
186 } 186 }
187 } 187 }
188 //END Static 188 //END Static
189 189
190 190
191 //BEGIN Non-static 191 //BEGIN Non-static
192 /+ NOTE: according to spec: "Templates cannot be used to add non-static members or virtual
193 functions to classes." However, this appears to work (but linking problems did occur).
194 Alternative: use mixins. From OptionsChanges:
195 // setT (used to be a template, but:
196 // Templates cannot be used to add non-static members or virtual functions to classes. )
197 template setMixin(A...) {
198 static if (A.length) {
199 const char[] setMixin = `void set`~TName!(A[0])~` (ID id, `~A[0].stringof~` x) {
200 `~TName!(T)~`s[id] = x;
201 }
202 ` ~ setMixin!(A[1..$]);
203 } else
204 const char[] setMixin = ``;
205 }+/
192 /** Set option symbol of an Options sub-class to val. 206 /** Set option symbol of an Options sub-class to val.
193 * 207 *
194 * Due to the way options are handled generically, string IDs must be used to access the options 208 * Due to the way options are handled generically, string IDs must be used to access the options
195 * via hash-maps, which is a little slower than direct access but necessary since the option 209 * via hash-maps, which is a little slower than direct access but necessary since the option
196 * must be changed in two separate places. */ 210 * must be changed in two separate places. */
197 void set(T) (char[] symbol, T val) { 211 void set(T) (char[] symbol, T val) {
198 static assert (TIsIn!(T,TYPES), "Options does not support type "~T.stringof); 212 static assert (TIsIn!(T,TYPES), "Options does not support type "~T.stringof);
199 213
200 mixin (`alias opts`~TName!(T)~` optsVars;`);
201
202 changed = true; // something got set (don't bother checking this isn't what it already was) 214 changed = true; // something got set (don't bother checking this isn't what it already was)
203 215
204 try { 216 try {
205 *(optsVars[cast(ID) symbol]) = val; 217 mixin (`*(opts`~TName!(T)~`[cast(ID) symbol]) = val;`);
206 optionChanges.set!(T) (cast(ID) symbol, val); 218 mixin (`optionChanges.`~TName!(T)~`s[symbol] = val;`);
207 } catch (ArrayBoundsException) { 219 } catch (ArrayBoundsException) {
208 // log and ignore: 220 // log and ignore:
209 logger.error ("Options.set: invalid symbol"); 221 logger.error ("Options.set: invalid symbol");
210 } 222 }
211 } 223 }
396 dlg ("`~A[0].stringof~`", id, serialize (val)); 408 dlg ("`~A[0].stringof~`", id, serialize (val));
397 ` ~ writeAllMixin!(A[1..$]); 409 ` ~ writeAllMixin!(A[1..$]);
398 } else 410 } else
399 const char[] writeAllMixin = ``; 411 const char[] writeAllMixin = ``;
400 } 412 }
401
402 } 413 }
403 //END Templates 414 //END Templates
404 // These store the actual values, but are never accessed directly except when initially added. 415 // These store the actual values, but are never accessed directly except when initially added.
405 // optsX store pointers to each item added along with the ID and are used for access. 416 // optsX store pointers to each item added along with the ID and are used for access.
406 mixin(Vars!(TYPES)); 417 mixin(Vars!(TYPES));
407 418
408 this () {} 419 this () {}
409 420
410 void set(T) (ID id, T x) {
411 static assert (Options.TIsIn!(T,TYPES), "Options does not support type "~T.stringof);
412
413 mixin (`alias `~TName!(T)~`s vars;`);
414 vars[id] = x;
415 }
416
417 //BEGIN Mergetag loading/saving code 421 //BEGIN Mergetag loading/saving code
418 // HIGH_LOW priority: only load symbols not currently existing 422 // HIGH_LOW priority: only load symbols not currently existing
419 void addTag (char[] tp, ID id, char[] dt) { 423 void addTag (char[] tp, ID id, char[] dt) {
420 mixin (addTagMixin!(TYPES).addTagMixin); 424 mixin (addTagMixin!(TYPES).addTagMixin);
421 } 425 }