diff mde/lookup/Options.d @ 98:49e7cfed4b34

All types of Option have been converted to use ValueContent classes, and their values can be displayed.
author Diggory Hardy <diggory.hardy@gmail.com>
date Wed, 12 Nov 2008 13:18:51 +0000
parents 2a364c7d82c9
children 71f0f1f83620
line wrap: on
line diff
--- a/mde/lookup/Options.d	Mon Nov 10 16:44:44 2008 +0000
+++ b/mde/lookup/Options.d	Wed Nov 12 13:18:51 2008 +0000
@@ -81,8 +81,9 @@
     // All supported types, for generic handling via templates. It should be possible to change
     // the supported types simply by changing this list now (untested).
     template store(A...) { alias A store; }
-    alias store!(bool, int, double, char[]) TYPES;
-    alias store!(int, double, char[]) TYPES2;
+    // NOTE: currently all types have transitioned to the new method, but the old method remains
+    alias store!(bool, int, double, char[]) TYPES;    // all types
+    alias store!(bool, int, double, char[]) CTYPES;   // types stored with a content
     //BEGIN Templates: internal
     private {
         // Get name of a type. Basically just stringof, but special handling for arrays.
@@ -97,7 +98,7 @@
         // Pointer lists
         template PLists(A...) {
             static if (A.length) {
-                static if (is (T == bool)) {
+                static if (TIsIn!(A[0], CTYPES)) {
                     const char[] PLists = PLists!(A[1..$]);
                 } else
                     const char[] PLists = A[0].stringof~"*[ID] opts"~TName!(A[0])~";\n" ~ PLists!(A[1..$]);
@@ -118,11 +119,11 @@
         
         // For addTag
         template addTagMixin(T, A...) {
-            static if (is(T == bool)) {
+            static if (TIsIn!(T, CTYPES)) {
                 const char[] ifBlock = `if (tp == "`~T.stringof~`") {
     auto p = id in opts;
     if (p) {
-        auto q = cast(BoolContent) (*p);
+        auto q = cast(`~VContentN!(T)~`) (*p);
         if (q) q.v = parseTo!(`~T.stringof~`) (dt);
     }
 }`;
@@ -140,7 +141,10 @@
         // For list
         template listMixin(A...) {
             static if (A.length) {
-                const char[] listMixin = `ret ~= opts`~TName!(A[0])~`.keys;` ~ listMixin!(A[1..$]);
+                static if (TIsIn!(A, CTYPES))
+                    const char[] listMixin = listMixin!(A[1..$]);
+                else
+                    const char[] listMixin = `ret ~= opts`~TName!(A[0])~`.keys;` ~ listMixin!(A[1..$]);
             } else
                 const char[] listMixin = ``;
         }
@@ -248,7 +252,7 @@
      * via hash-maps, which is a little slower than direct access but necessary since the option
      * must be changed in two separate places. */
     void set(T) (char[] symbol, T val) {
-        static assert (TIsIn!(T,TYPES) && !is(T == bool), "Options does not support type "~T.stringof);
+        static assert (TIsIn!(T,TYPES) && !TIsIn!(T, CTYPES), "Options.set does not support type "~T.stringof);
         
         changed = true;     // something got set (don't bother checking this isn't what it already was)
         
@@ -280,7 +284,7 @@
     /** List the names of all options of a specific type. */
     char[][] list () {
         char[][] ret;
-        mixin (listMixin!(TYPES2));
+        mixin (listMixin!(TYPES));
         return ret;
     }
     
@@ -297,7 +301,7 @@
         OptionChanges optionChanges;	// all changes to options (for saving)
     	
         // The "pointer lists", e.g. char[]*[ID] optscharA;
-        mixin (PLists!(TYPES2)); //FIXME
+        mixin (PLists!(TYPES));
         ValueContent[char[]] opts;      // generic list of option values
     }
     
@@ -315,16 +319,10 @@
     private {
         // Replace, e.g., bool, with BoolContent
         template contentName(A) {
-            static if (is(A == bool)) {
-                const char[] contentName = "BoolContent";
-            } else static if (is(A == int)) {
-                const char[] contentName = "int";// no IntContent yet
-            } else static if (is(A == double)) {
-                const char[] contentName = "double";
-            } else static if (is(A == char[])) {
-                const char[] contentName = "char[]";
+            static if (TIsIn!(A, CTYPES)) {
+                const char[] contentName = VContentN!(A);
             } else
-                static assert (false, "unsuppurted type: "~ A);
+                const char[] contentName = A.stringof;
         }
         // Return index of first comma, or halts if not found.
         template cIndex(char[] A) {
@@ -370,14 +368,14 @@
                         aaVars!(A[cIndex!(A)+1..$]);
         }
         // May have a trailing comma. Assumes cIndex always returns less than A.$ .
-        template aaVarsBool(char[] A) {//FIXME
+        template aaVarsContent(char[] A) {//FIXME
             static if (A.length == 0)
-            const char[] aaVarsBool = "";
+            const char[] aaVarsContent = "";
             else static if (A[0] == ' ')
-                const char[] aaVarsBool = aaVarsBool!(A[1..$]);
+                const char[] aaVarsContent = aaVarsContent!(A[1..$]);
             else
-                const char[] aaVarsBool = "\""~A[0..cIndex!(A)]~"\"[]:"~A[0..cIndex!(A)] ~ "," ~
-                aaVarsBool!(A[cIndex!(A)+1..$]);
+                const char[] aaVarsContent = "\""~A[0..cIndex!(A)]~"\"[]:cast(ValueContent)"~A[0..cIndex!(A)] ~ "," ~
+                aaVarsContent!(A[cIndex!(A)+1..$]);
         }
         // strip Trailing Comma
         template sTC(char[] A) {
@@ -403,21 +401,20 @@
                 const char[] catOrNothing = ``;
         }
         // foreach decl...
-        template createBCs(char[] A) {
+        template createContents(T, char[] A) {
             static if (A.length == 0)
-                const char[] createBCs = "";
+                const char[] createContents = "";
             else static if (A[0] == ' ')
-                const char[] createBCs = createBCs!(A[1..$]);
+                const char[] createContents = createContents!(T,A[1..$]);
             else
-                const char[] createBCs = A[0..cIndex!(A)]~ ` = (new BoolContent ("`~A[0..cIndex!(A)]~"\")).addChangeCb (&optionChanges.set);\n"~
-                createBCs!(A[cIndex!(A)+1..$]);
+                const char[] createContents = "opts[\""~A[0..cIndex!(A)]~"\"] = " ~ A[0..cIndex!(A)]~ " = (new "~VContentN!(T)~" (\""~A[0..cIndex!(A)]~"\")).addChangeCb (&optionChanges.set);\n"~
+                createContents!(T,A[cIndex!(A)+1..$]);
         }
         // for recursing on TYPES
         template optionsThisInternal(char[] A, B...) {
             static if (B.length) {
-                static if (is(B[0] == bool)) {//FIXME
-                    const char[] optionsThisInternal = createBCs!(parseT!(B[0].stringof,A))~
-                    `opts = `~listOrNull!(sTC!(aaVarsBool!(parseT!(B[0].stringof,A))))~";\n" ~
+                static if (TIsIn!(B[0], CTYPES)) {
+                    const char[] optionsThisInternal = createContents!(B[0],parseT!(B[0].stringof,A))~
                     optionsThisInternal!(A,B[1..$]);
                 } else
                 const char[] optionsThisInternal = `opts`~TName!(B[0])~` = `~listOrNull!(sTC!(aaVars!(parseT!(B[0].stringof,A))))~";\n" ~ optionsThisInternal!(A,B[1..$]);
@@ -557,18 +554,18 @@
 /** A home for all miscellaneous options, at least for now. */
 MiscOptions miscOpts;
 class MiscOptions : Options {
-    const A = "bool exitImmediately; int maxThreads, logOptions; double pollInterval; char[] L10n;";
+    const A = "bool exitImmediately; int maxThreads, logLevel, logOutput; double pollInterval; char[] L10n;";
     //pragma (msg, impl!(A));
     mixin (impl!(A));
     
     void validate() {
         // Try to enforce sensible values, whilst being reasonably flexible:
-        if (miscOpts.maxThreads < 1 || miscOpts.maxThreads > 64) {
+        if (maxThreads() < 1 || maxThreads() > 64) {
             logger.warn ("maxThreads must be in the range 1-64. Defaulting to 4.");
-            miscOpts.set!(int)("maxThreads", 4);
+            maxThreads = 4;
         }
-        if (pollInterval !<= 1.0 || pollInterval !>= 0.0)
-            set!(double) ("pollInterval", 0.01);
+        if (pollInterval() !<= 1.0 || pollInterval() !>= 0.0)
+            pollInterval = 0.01;
     }
     
     static this() {