From a057517431ac0d73d507656d91112c984d84dc8a Mon Sep 17 00:00:00 2001 From: Bryce Adelstein-Lelbach Date: Wed, 8 Dec 2010 18:25:07 +0000 Subject: [PATCH] Added an iomanip implementation to boost/detail. GNU's libstdc++ version 4.5+ contains an iomanip header which uses C++0x features that only GCC supports, causing breakage with ICC and Clang. Also added a test, but I'm not sure how to set it up to be run by the testing machines. [SVN r67111] --- include/boost/detail/iomanip.hpp | 222 +++++++++++++++++++++++++++++++ test/Jamfile | 21 +++ test/iomanip_test.cpp | 58 ++++++++ 3 files changed, 301 insertions(+) create mode 100644 include/boost/detail/iomanip.hpp create mode 100644 test/Jamfile create mode 100644 test/iomanip_test.cpp diff --git a/include/boost/detail/iomanip.hpp b/include/boost/detail/iomanip.hpp new file mode 100644 index 0000000..7b41cdd --- /dev/null +++ b/include/boost/detail/iomanip.hpp @@ -0,0 +1,222 @@ +/*<-============================================================================ + Copyright (c) 2010 Bryce Lelbach + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +============================================================================->*/ + +#ifndef BOOST_DETAIL_IOMANIP_HPP +#define BOOST_DETAIL_IOMANIP_HPP + +#include +#include + +namespace boost { +namespace detail { + +//[resetiosflags +class resetiosflags_manip { + private: + std::ios_base::fmtflags mask; + + public: + explicit resetiosflags_manip (std::ios_base::fmtflags m): + mask(m) { } + + template + friend std::basic_istream& + operator>> (std::basic_istream& is, + resetiosflags_manip const& x) { + is.unsetf(x.mask); + return is; + } + + template + friend std::basic_ostream& + operator<< (std::basic_ostream& os, + resetiosflags_manip const& x) { + os.unsetf(x.mask); + return os; + } +}; + +inline resetiosflags_manip resetiosflags (std::ios_base::fmtflags mask) { + return resetiosflags_manip(mask); +} +//] + +//[setiosflags +class setiosflags_manip { + private: + std::ios_base::fmtflags mask; + + public: + explicit setiosflags_manip (std::ios_base::fmtflags m): + mask(m) { } + + template + friend std::basic_istream& + operator>> (std::basic_istream& is, + setiosflags_manip const& x) { + is.setf(x.mask); + return is; + } + + template + friend std::basic_ostream& + operator<< (std::basic_ostream& os, + setiosflags_manip const& x) { + os.setf(x.mask); + return os; + } +}; + +inline setiosflags_manip setiosflags (std::ios_base::fmtflags mask) { + return setiosflags_manip(mask); +} +//] + +//[setbase +class setbase_manip { + private: + int base; + + public: + explicit setbase_manip (int b): + base(b) { } + + template + friend std::basic_istream& + operator>> (std::basic_istream& is, setbase_manip const& x) { + using namespace std; + switch (x.base) { + case 8: + is.setf(ios_base::oct, ios_base::basefield); + return is; + case 10: + is.setf(ios_base::dec, ios_base::basefield); + return is; + case 16: + is.setf(ios_base::hex, ios_base::basefield); + return is; + default: + is.setf(ios_base::fmtflags(0), ios_base::basefield); + return is; + } + } + + template + friend std::basic_ostream& + operator<< (std::basic_ostream& os, setbase_manip const& x) { + using namespace std; + switch (x.base) { + case 8: + os.setf(ios_base::oct, ios_base::basefield); + return os; + case 10: + os.setf(ios_base::dec, ios_base::basefield); + return os; + case 16: + os.setf(ios_base::hex, ios_base::basefield); + return os; + default: + os.setf(ios_base::fmtflags(0), ios_base::basefield); + return os; + } + } +}; + +inline setbase_manip setbase (int base) { + return setbase_manip(base); +} +//] + +//[setfill +template +class setfill_manip { + private: + CharT fill; + + public: + explicit setfill_manip (CharT c): + fill(c) { } + + template + friend std::basic_ostream& + operator<< (std::basic_ostream& os, setfill_manip const& x) { + os.fill(x.fill); + return os; + } +}; + +template +inline setfill_manip setfill (CharT c) { + return setfill_manip(c); +} +//] + +//[setprecision +class setprecision_manip { + private: + int n; + + public: + explicit setprecision_manip (int n_): + n(n_) { } + + template + friend std::basic_istream& + operator>> (std::basic_istream& is, + setprecision_manip const& x) { + is.precision(x.n); + return is; + } + + template + friend std::basic_ostream& + operator<< (std::basic_ostream& os, + setprecision_manip const& x) { + os.precision(x.n); + return os; + } +}; + +inline setprecision_manip setprecision (int n_) { + return setprecision_manip(n_); +} +//] + +//[setw +class setw_manip { + private: + int n; + + public: + explicit setw_manip (int n_): + n(n_) { } + + template + friend std::basic_istream& + operator>> (std::basic_istream& is, setw_manip const& x) { + is.width(x.n); + return is; + } + + template + friend std::basic_ostream& + operator<< (std::basic_ostream& os, setw_manip const& x) { + os.width(x.n); + return os; + } +}; + +inline setw_manip setw (int n_) { + return setw_manip(n_); +} +//] + +} // detail +} // boost + +#endif // BOOST_DETAIL_IOMANIP_HPP + diff --git a/test/Jamfile b/test/Jamfile new file mode 100644 index 0000000..9413621 --- /dev/null +++ b/test/Jamfile @@ -0,0 +1,21 @@ +################################################################*# Jam #*####### +# Copyright (C) 2010 Bryce Lelbach +# +# Distributed under the Boost Software License, Version 1.0. (See accompanying +# file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +################################################################################ + +project detail/test + : requirements + clang:-Wno-unused + clang:-Wno-tautological-compare + clang:-ftemplate-depth-300 + gcc:-ftemplate-depth-300 + darwin:-ftemplate-depth-300 + ; + +for tests in [ glob *.cpp ] { + run $(tests) : : : : $(tests:B) ; +} + + diff --git a/test/iomanip_test.cpp b/test/iomanip_test.cpp new file mode 100644 index 0000000..0a4f76d --- /dev/null +++ b/test/iomanip_test.cpp @@ -0,0 +1,58 @@ +/*<-============================================================================ + Copyright (c) 2010 Bryce Lelbach + + Distributed under the Boost Software License, Version 1.0. (See accompanying + file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +============================================================================->*/ + +#include +#include + +#include +#include + +int main (void) { + using namespace boost::detail; + + std::ostringstream oss(std::ostringstream::out); + + //[setbase_test + oss << setbase(8) << 8; + BOOST_TEST(oss.str() == "10"); + + oss.str(""); + oss << setbase(10) << 10; + BOOST_TEST(oss.str() == "10"); + + oss.str(""); + oss << setbase(16) << 16; + BOOST_TEST(oss.str() == "10"); + //] + + //[setiosflags_test + oss.str(""); + oss << setiosflags(std::ios_base::showbase | std::ios_base::hex) << 16; + BOOST_TEST(oss.str() == "0x10"); + //] + + //[resetiosflags_test + oss.str(""); + oss << resetiosflags(std::ios_base::showbase | std::ios_base::hex) << 16; + BOOST_TEST(oss.str() == "16"); + //] + + //[setprecision_test + oss.str(""); + oss << setprecision(4) << 3.14159; + BOOST_TEST(oss.str() == "3.142"); + //] + + //[setfill_and_setw_test + oss.str(""); + oss << setfill('*') << setw(5) << 9; + BOOST_TEST(oss.str() == "****9"); + //] + + return boost::report_errors(); +} +