Date: Friday, January 27, 2006 @ 12:48:25
  Author: marc
    Path: /cvsroot/carob/carob/src

Modified: ParameterStatement.cpp (1.9 -> 1.10)

Reworked template setters to add a "oneShotFilter()". Needed to catch infinity 
and NotaNumbers. CAROB-31 CAROB-55


------------------------+
 ParameterStatement.cpp |   59 +++++++++++++++++++++++++++++++++++------------
 1 files changed, 45 insertions(+), 14 deletions(-)


Index: carob/src/ParameterStatement.cpp
diff -u carob/src/ParameterStatement.cpp:1.9 
carob/src/ParameterStatement.cpp:1.10
--- carob/src/ParameterStatement.cpp:1.9        Thu Jan 26 22:21:28 2006
+++ carob/src/ParameterStatement.cpp    Fri Jan 27 12:48:24 2006
@@ -139,30 +139,59 @@
   std::locale locC = std::locale::classic();
 }
 
+
+  /////  Template & specialized filters /////
+
 namespace CarobNS {
 
 using std::basic_ostream;
 
+  /// template filters do nothing by default ///
+
+/**
+ * Stream filter: intercept (only) one value, modify it before printing it, 
and returns the
+ * filtered stream before disappearing.
+ */
+template <class T, class Ch>
+class oneShotFilter
+{
+  basic_ostream<Ch>& wrapped_stream;
+public:
+  oneShotFilter(basic_ostream<Ch>& s) : wrapped_stream(s) { } ;
+  basic_ostream<Ch>&
+ // this is the actual filtering operator
+  operator<< (T value)
+  { // by default filter nothing, just output the value
+    wrapped_stream << value;
+    // then return the underlying stream (we filter only once!)
+    return wrapped_stream;
+  }
+};
 
-// 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
+class streamSettings
 {
 public:
-  void doManip(basic_ostream<Ch>& s) const { };
+  void doManip(basic_ostream<Ch>& s) const { /* do nothing by default */ };  
 };
 
-// stream operator<< for the functor just above
+/**
+ * Operator<< doing the actual filtering work: modify the settings
+ * of the underlying standard stream, then returns a one-shot filter.
+ */
 template <class T, class Ch>
-basic_ostream<Ch>&
-operator<< (basic_ostream<Ch>& s, streamSettingsFor<T, Ch> m)
+oneShotFilter<T, Ch>
+operator<< (basic_ostream<Ch>& filtered_std_stream, const streamSettings<T, 
Ch>& setfunctor)
 {
-  m.doManip(s);
-  return s;
+  // modify settings of underlying stream
+  setfunctor.doManip(filtered_std_stream);
+  // then return a filter instance
+  return oneShotFilter<T, Ch>(filtered_std_stream);
 }
 
-// partial specialization code shared by float & double
+  /////  Filter specializations ////
+
+// Partial specialization code shared by float & double
 template<class Ch> void
 streamSettingsForFloatingPoint(basic_ostream<Ch>& s)
 {
@@ -172,7 +201,7 @@
 
 // template specialization for float
 template<class Ch>
-class streamSettingsFor<float, Ch>
+class streamSettings<float, Ch>
 {
 public:
   void doManip(basic_ostream<Ch>& s) const
@@ -182,9 +211,9 @@
   };
 };
 
-// template specialization for float
+// template specialization for double
 template<class Ch>
-class streamSettingsFor<double, Ch>
+class streamSettings<double, Ch>
 {
 public:
   void doManip(basic_ostream<Ch>& s) const
@@ -196,6 +225,8 @@
 
 } // namespace CarobNS
 
+  //// end of template filters ////
+
 
 template <class T> void ParameterStatement::set(const int paramIndex, const 
std::wstring &typeTag, 
         const T& value) 
@@ -210,7 +241,7 @@
 
 
   // TODO: catch inf & NaN
-  buffer << START_PARAM_TAG << typeTag << streamSettingsFor<T>() << value << 
END_PARAM_TAG;
+  buffer << START_PARAM_TAG << typeTag << streamSettings<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