Date: Thursday, January 26, 2006 @ 22:21:28
  Author: marc
    Path: /cvsroot/carob/carob/src

Modified: ParameterStatement.cpp (1.8 -> 1.9)

Templatized stream settings specific to floats and doubles. Inf & NaN still not 
handled/"translated". CAROB-31 & CAROB-55.


------------------------+
 ParameterStatement.cpp |   68 ++++++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 64 insertions(+), 4 deletions(-)


Index: carob/src/ParameterStatement.cpp
diff -u carob/src/ParameterStatement.cpp:1.8 
carob/src/ParameterStatement.cpp:1.9
--- carob/src/ParameterStatement.cpp:1.8        Tue Jan 24 19:37:27 2006
+++ carob/src/ParameterStatement.cpp    Thu Jan 26 22:21:28 2006
@@ -139,6 +139,64 @@
   std::locale locC = std::locale::classic();
 }
 
+namespace CarobNS {
+
+using std::basic_ostream;
+
+
+// Template<datatype> stream manipulators
+// No pointer to function template (type safety?), so let's do a functor
+template <class T, class Ch = wchar_t>
+class streamSettingsFor
+{
+public:
+  void doManip(basic_ostream<Ch>& s) const { };
+};
+
+// stream operator<< for the functor just above
+template <class T, class Ch>
+basic_ostream<Ch>&
+operator<< (basic_ostream<Ch>& s, streamSettingsFor<T, Ch> m)
+{
+  m.doManip(s);
+  return s;
+}
+
+// partial specialization code shared by float & double
+template<class Ch> void
+streamSettingsForFloatingPoint(basic_ostream<Ch>& s)
+{
+  s.imbue(locC);
+  s.setf(std::ios_base::scientific, std::ios_base::floatfield);
+}
+
+// template specialization for float
+template<class Ch>
+class streamSettingsFor<float, Ch>
+{
+public:
+  void doManip(basic_ostream<Ch>& s) const
+  {
+    streamSettingsForFloatingPoint(s);
+    s.precision(9);
+  };
+};
+
+// template specialization for float
+template<class Ch>
+class streamSettingsFor<double, Ch>
+{
+public:
+  void doManip(basic_ostream<Ch>& s) const
+  {
+    streamSettingsForFloatingPoint(s);
+    s.precision(17);
+  };
+};
+
+} // namespace CarobNS
+
+
 template <class T> void ParameterStatement::set(const int paramIndex, const 
std::wstring &typeTag, 
         const T& value) 
     throw (DriverException, UnexpectedException)
@@ -146,12 +204,14 @@
   if (paramIndex < 1 || paramIndex > (int)inStrings.size())
     throw DriverException(L"Parameter index out of range.");
     
+  // As an optimization, we could make this stream a member.
+  // That would just require a member lock for thread-safety
   std::wostringstream buffer;
-  buffer.imbue(locC);
-  buffer.setf(std::ios_base::scientific, std::ios_base::floatfield);
-  buffer.precision(18); // std::numeric_limits<double>::digits10 + some
 
-  buffer << START_PARAM_TAG << typeTag << value << END_PARAM_TAG;
+
+  // TODO: catch inf & NaN
+  buffer << START_PARAM_TAG << typeTag << streamSettingsFor<T>() << value << 
END_PARAM_TAG;
+
   inStrings[paramIndex-1] = buffer.str();
 }
 

_______________________________________________
Carob-commits mailing list
[email protected]
https://forge.continuent.org/mailman/listinfo/carob-commits

Reply via email to