Merge zizzer:/z/m5/Bitkeeper/m5
[gem5.git] / sim / param.cc
index 8284983a8b528a30fcd0e55333e9c1c152be35b8..d20be8d3358bf215108fc2978714e29ce47b0da6 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003 The Regents of The University of Michigan
+ * Copyright (c) 2002-2004 The Regents of The University of Michigan
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -85,67 +85,76 @@ BaseParam::die(const string &err) const
 ////////////////////////////////////////////////////////////////////////
 
 //
-// Integer types all use to_number for parsing and '<<' for
-// displaying
+// The base implementations use to_number for parsing and '<<' for
+// displaying, suitable for integer types.
 //
-#define INT_PARAM(type)                                \
-bool                                           \
-parseParam(const string &s, type &value)       \
-{                                              \
-    return to_number(s, value);                        \
-}                                              \
-                                                \
-void                                           \
-showParam(ostream &os, type value)             \
-{                                              \
-    os << value;                               \
+template <class T>
+bool
+parseParam(const string &s, T &value)
+{
+    return to_number(s, value);
 }
 
-INT_PARAM(unsigned long long)
-INT_PARAM(signed long long)
-INT_PARAM(unsigned long)
-INT_PARAM(signed long)
-INT_PARAM(unsigned int)
-INT_PARAM(signed int)
-INT_PARAM(unsigned short)
-INT_PARAM(signed short)
-INT_PARAM(unsigned char)
-INT_PARAM(signed char)
-
-#undef INT_PARAM
+template <class T>
+void
+showParam(ostream &os, T const &value)
+{
+    os << value;
+}
 
 //
-// Floating-point types
+// Template specializations:
+// - char (8-bit integer)
+// - floating-point types
+// - bool
+// - string
 //
+
+// Treat 8-bit ints (chars) as ints on output, not as chars
+template <>
+void
+showParam(ostream &os, const char &value)
+{
+    os << (int)value;
+}
+
+
+template <>
+void
+showParam(ostream &os, const unsigned char &value)
+{
+    os << (unsigned int)value;
+}
+
+
+// Use sscanf() for FP types as to_number() only handles integers
+template <>
 bool
 parseParam(const string &s, float &value)
 {
     return (sscanf(s.c_str(), "%f", &value) == 1);
 }
 
+template <>
 bool
 parseParam(const string &s, double &value)
 {
     return (sscanf(s.c_str(), "%lf", &value) == 1);
 }
 
-void showParam(ostream &os, float value)  { os << value; }
-void showParam(ostream &os, double value) { os << value; }
-
-//
-// bool
-//
+// Be flexible about what we take for bool
+template <>
 bool
 parseParam(const string &s, bool &value)
 {
-    const string &lower = to_lower(s);
+    const string &ls = to_lower(s);
 
-    if (lower == "true" || lower == "t" || lower == "yes" || lower == "y") {
+    if (ls == "true" || ls == "t" || ls == "yes" || ls == "y" || ls == "1") {
         value = true;
         return true;
     }
 
-    if (lower == "false" || lower == "f" || lower == "no" || lower == "n") {
+    if (ls == "false" || ls == "f" || ls == "no" || ls == "n" || ls == "0") {
         value = false;
         return true;
     }
@@ -153,17 +162,17 @@ parseParam(const string &s, bool &value)
     return false;
 }
 
-
+// Display bools as strings
+template <>
 void
-showParam(ostream &os, bool value)
+showParam(ostream &os, const bool &value)
 {
     os << (value ? "true" : "false");
 }
 
 
-//
-// string
-//
+// String requires no processing to speak of
+template <>
 bool
 parseParam(const string &s, string &value)
 {
@@ -171,13 +180,6 @@ parseParam(const string &s, string &value)
     return true;
 }
 
-
-void
-showParam(ostream &os, const string &value)
-{
-    os << value;
-}
-
 //
 // End of parseParam/showParam definitions.  Now we move on to
 // incorporate them into the Param/VectorParam parse() and showValue()
@@ -209,6 +211,11 @@ template <class T>
 void
 VectorParam<T>::parse(const string &s)
 {
+    if (s.empty()) {
+        wasSet = true;
+        return;
+    }
+
     vector<string> tokens;
 
     tokenize(tokens, s, ' ');
@@ -274,6 +281,8 @@ template VectorParam<type>;
 // instantiate all four methods (parse/show, scalar/vector) for basic
 // types that can use the above templates
 #define INSTANTIATE_PARAM_TEMPLATES(type, typestr)                     \
+template bool parseParam<type>(const string &s, type &value);          \
+template void showParam<type>(ostream &os, type const &value);         \
 template void Param<type>::parse(const string &);                      \
 template void VectorParam<type>::parse(const string &);                        \
 template void Param<type>::showValue(ostream &) const;                 \
@@ -551,15 +560,27 @@ SimObjectBaseParam::parse(const string &s, vector<SimObject *>&value)
 
 list<ParamContext *> *ParamContext::ctxList = NULL;
 
-ParamContext::ParamContext(const string &_iniSection, bool noAutoParse)
+ParamContext::ParamContext(const string &_iniSection, InitPhase _initPhase)
     : iniFilePtr(NULL),        // initialized on call to parseParams()
-      iniSection(_iniSection), paramList(NULL)
+      iniSection(_iniSection), paramList(NULL),
+      initPhase(_initPhase)
 {
-    if (!noAutoParse) {
+    // Put this context on global list for initialization
+    if (initPhase != NoAutoInit) {
         if (ctxList == NULL)
             ctxList = new list<ParamContext *>();
 
-        (*ctxList).push_back(this);
+        // keep list sorted by ascending initPhase values
+        list<ParamContext *>::iterator i = ctxList->begin();
+        list<ParamContext *>::iterator end = ctxList->end();
+        for (; i != end; ++i) {
+            if (initPhase <= (*i)->initPhase) {
+                // found where we want to insert
+                break;
+            }
+        }
+        // (fall through case: insert at end)
+        ctxList->insert(i, this);
     }
 }