////////////////////////////////////////////////////////////////////////
//
-// Integer types all use to_number for parsing and '<<' for
-// displaying
-//
-#define INT_PARAM(type) \
-bool \
-parseParam(const string &s, type &value) \
-{ \
- return to_number(s, value); \
-} \
- \
-void \
-showParam(ostream &os, type value) \
-{ \
- os << 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
-
-//
-// Floating-point types
+// The base implementations use to_number for parsing and '<<' for
+// displaying, suitable for integer types.
//
+template <class T>
+bool
+parseParam(const string &s, T &value)
+{
+ return to_number(s, value);
+}
+
+template <class T>
+void
+showParam(ostream &os, T const &value)
+{
+ os << value;
+}
+
+//
+// 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)
{
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)
{
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()
// 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; \