Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r71402 - trunk/libs/math/example
From: pbristow_at_[hidden]
Date: 2011-04-21 13:49:41


Author: pbristow
Date: 2011-04-21 13:49:40 EDT (Thu, 21 Apr 2011)
New Revision: 71402
URL: http://svn.boost.org/trac/boost/changeset/71402

Log:
Refinements to examples to avoid temp locales
Text files modified:
   trunk/libs/math/example/lexical_cast_nonfinite_facets.cpp | 17 +++++++++++------
   trunk/libs/math/example/nonfinite_legacy.cpp | 15 +++++++++------
   trunk/libs/math/example/nonfinite_loopback_ok.cpp | 18 ++++++++++++------
   3 files changed, 32 insertions(+), 18 deletions(-)

Modified: trunk/libs/math/example/lexical_cast_nonfinite_facets.cpp
==============================================================================
--- trunk/libs/math/example/lexical_cast_nonfinite_facets.cpp (original)
+++ trunk/libs/math/example/lexical_cast_nonfinite_facets.cpp 2011-04-21 13:49:40 EDT (Thu, 21 Apr 2011)
@@ -66,15 +66,20 @@
   std::cout << "finite_num_facet with lexical_cast example." << std::endl;
 
   // Example of using non_finite num_put and num_get facets with lexical_cast.
- locale old_locale;
- locale tmp_locale(old_locale, new nonfinite_num_put<char>);
- // Create a new temporary output locale, and add the output nonfinite_num_put facet.
+ //locale old_locale;
+ //locale tmp_locale(old_locale, new nonfinite_num_put<char>);
+ //// Create a new temporary output locale, and add the output nonfinite_num_put facet.
 
- locale new_locale(tmp_locale, new nonfinite_num_get<char>);
+ //locale new_locale(tmp_locale, new nonfinite_num_get<char>);
   // Create a new output locale (from the tmp locale), and add the input nonfinite_num_get facet.
 
- // Note that is necessary to do add get and put facets in two steps.
-
+ // Note that you can only add facets one at a time,
+ // unless you chain thus:
+
+ std::locale new_locale(std::locale(std::locale(),
+ new boost::math::nonfinite_num_put<char>),
+ new boost::math::nonfinite_num_get<char>);
+
   locale::global(new_locale); // Newly constructed streams
   // (including those streams inside lexical_cast)
   // now use new_locale with nonfinite facets.

Modified: trunk/libs/math/example/nonfinite_legacy.cpp
==============================================================================
--- trunk/libs/math/example/nonfinite_legacy.cpp (original)
+++ trunk/libs/math/example/nonfinite_legacy.cpp 2011-04-21 13:49:40 EDT (Thu, 21 Apr 2011)
@@ -28,6 +28,11 @@
 #include <iostream>
 using std::cout;
 using std::endl;
+
+#include <iomanip>
+using std::setfill;
+using std::setw;
+
 #include <locale>
 using std::locale;
 
@@ -40,12 +45,10 @@
 
 int main()
 {
-
- locale old_locale;
- locale tmp_locale(old_locale, new nonfinite_num_put<char>(legacy));
- locale new_locale(tmp_locale, new nonfinite_num_get<char>(legacy));
- // Note that to add two facets, nonfinite_num_put and nonfinite_num_get,
- // you have to add one at a time, using a temporary locale.
+ // Create a new locale with both the nonfinite facets.
+ std::locale new_locale(std::locale(std::locale(),
+ new boost::math::nonfinite_num_put<char>),
+ new boost::math::nonfinite_num_get<char>);
 
   {
     stringstream ss;

Modified: trunk/libs/math/example/nonfinite_loopback_ok.cpp
==============================================================================
--- trunk/libs/math/example/nonfinite_loopback_ok.cpp (original)
+++ trunk/libs/math/example/nonfinite_loopback_ok.cpp 2011-04-21 13:49:40 EDT (Thu, 21 Apr 2011)
@@ -40,14 +40,20 @@
 
 int main()
 {
- locale old_locale; // Current global locale.
+ //locale old_locale; // Current global locale.
   // Create tmp_locale and store the output nonfinite_num_put facet in it.
- locale tmp_locale(old_locale, new nonfinite_num_put<char>);
+ //locale tmp_locale(old_locale, new nonfinite_num_put<char>);
   // Create new_locale and store the input nonfinite_num_get facet in it.
- locale new_locale(tmp_locale, new nonfinite_num_get<char>);
- // Seems necessary to add one facet at a time, hence need a tmp_locale.
-
- stringstream ss; // Both input and output.
+ //locale new_locale(tmp_locale, new nonfinite_num_get<char>);
+ // Can only add one facet at a time, hence need a tmp_locale.
+ // Unless we write:
+
+ std::locale new_locale(std::locale(std::locale(std::locale(),
+ new boost::math::nonfinite_num_put<char>),
+ new boost::math::nonfinite_num_get<char>));
+
+ stringstream ss; // Both input and output, so need both get and put facets.
+
   ss.imbue(new_locale);
 
   double inf = numeric_limits<double>::infinity();


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk