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]
This commit is contained in:
Bryce Adelstein-Lelbach
2010-12-08 18:25:07 +00:00
parent 2a7bc8999f
commit a057517431
3 changed files with 301 additions and 0 deletions

View File

@@ -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 <istream>
#include <ostream>
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<class CharT, class Traits>
friend std::basic_istream<CharT, Traits>&
operator>> (std::basic_istream<CharT, Traits>& is,
resetiosflags_manip const& x) {
is.unsetf(x.mask);
return is;
}
template<class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>&
operator<< (std::basic_ostream<CharT, Traits>& 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<class CharT, class Traits>
friend std::basic_istream<CharT, Traits>&
operator>> (std::basic_istream<CharT, Traits>& is,
setiosflags_manip const& x) {
is.setf(x.mask);
return is;
}
template<class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>&
operator<< (std::basic_ostream<CharT, Traits>& 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<class CharT, class Traits>
friend std::basic_istream<CharT, Traits>&
operator>> (std::basic_istream<CharT, Traits>& 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 <class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>&
operator<< (std::basic_ostream<CharT, Traits>& 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 CharT>
class setfill_manip {
private:
CharT fill;
public:
explicit setfill_manip (CharT c):
fill(c) { }
template<class Traits>
friend std::basic_ostream<CharT, Traits>&
operator<< (std::basic_ostream<CharT, Traits>& os, setfill_manip const& x) {
os.fill(x.fill);
return os;
}
};
template<class CharT>
inline setfill_manip<CharT> setfill (CharT c) {
return setfill_manip<CharT>(c);
}
//]
//[setprecision
class setprecision_manip {
private:
int n;
public:
explicit setprecision_manip (int n_):
n(n_) { }
template<class CharT, class Traits>
friend std::basic_istream<CharT, Traits>&
operator>> (std::basic_istream<CharT, Traits>& is,
setprecision_manip const& x) {
is.precision(x.n);
return is;
}
template<class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>&
operator<< (std::basic_ostream<CharT, Traits>& 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<class CharT, class Traits>
friend std::basic_istream<CharT, Traits>&
operator>> (std::basic_istream<CharT, Traits>& is, setw_manip const& x) {
is.width(x.n);
return is;
}
template <class CharT, class Traits>
friend std::basic_ostream<CharT, Traits>&
operator<< (std::basic_ostream<CharT, Traits>& 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

21
test/Jamfile Normal file
View File

@@ -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
<toolset>clang:<cxxflags>-Wno-unused
<toolset>clang:<cxxflags>-Wno-tautological-compare
<toolset>clang:<cxxflags>-ftemplate-depth-300
<toolset>gcc:<cxxflags>-ftemplate-depth-300
<toolset>darwin:<cxxflags>-ftemplate-depth-300
;
for tests in [ glob *.cpp ] {
run $(tests) : : : : $(tests:B) ;
}

58
test/iomanip_test.cpp Normal file
View File

@@ -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 <iostream>
#include <sstream>
#include <boost/detail/iomanip.hpp>
#include <boost/detail/lightweight_test.hpp>
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();
}