Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reimplemented io support for basic types #144

Merged
merged 7 commits into from
Jun 19, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
removed boost dependencies
  • Loading branch information
regnirpsj committed Dec 18, 2013
commit 0e3cebf23a48c0ddb3763d9d7eedaf4cfef664d8
46 changes: 38 additions & 8 deletions glm/gtx/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/// @author Jan P Springer ([email protected])
///
/// @see core (dependence)
/// @see gtx_quaternion (dependence)
/// @see gtc_quaternion (dependence)
///
/// @defgroup gtx_io GLM_GTX_io
/// @ingroup gtx
Expand All @@ -51,11 +51,9 @@
# pragma message("GLM: GLM_GTX_io extension included")
#endif

#include <boost/io_fwd.hpp> // basic_ios_all_saver<> (fwd)
#include <boost/noncopyable.hpp> // boost::noncopyable
#include <iosfwd> // std::basic_ostream<> (fwd)
#include <locale> // std::locale, std::locale::facet, std::locale::id
#include <utility> // std::pair<>
#include <iosfwd> // std::basic_ostream<> (fwd)
#include <locale> // std::locale, std::locale::facet, std::locale::id
#include <utility> // std::pair<>

namespace glm
{
Expand Down Expand Up @@ -92,7 +90,37 @@ namespace glm
};

template <typename CTy, typename CTr = std::char_traits<CTy> >
class basic_format_saver : private boost::noncopyable {
class basic_state_saver {

public:

explicit basic_state_saver(std::basic_ios<CTy,CTr>&);
~basic_state_saver();

private:

typedef ::std::basic_ios<CTy,CTr> state_type;
typedef typename state_type::char_type char_type;
typedef ::std::ios_base::fmtflags flags_type;
typedef ::std::streamsize streamsize_type;
typedef ::std::locale const locale_type;

state_type& state_;
flags_type flags_;
streamsize_type precision_;
streamsize_type width_;
char_type fill_;
locale_type locale_;

basic_state_saver& operator=(basic_state_saver const&);

};

typedef basic_state_saver<char> state_saver;
typedef basic_state_saver<wchar_t> wstate_saver;

template <typename CTy, typename CTr = std::char_traits<CTy> >
class basic_format_saver {

public:

Expand All @@ -101,7 +129,9 @@ namespace glm

private:

boost::io::basic_ios_all_saver<CTy> const ias_;
basic_state_saver<CTy> const bss_;

basic_format_saver& operator=(basic_format_saver const&);

};

Expand Down
41 changes: 30 additions & 11 deletions glm/gtx/io.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
// OpenGL Mathematics Copyright (c) 2005 - 2013 G-Truc Creation (www.g-truc.net)
///////////////////////////////////////////////////////////////////////////////////////////////////
// Created : 2013-11-22
// Updated : 2013-12-17
// Updated : 2013-12-18
// Licence : This source is under MIT License
// File : glm/gtx/inl.inl
///////////////////////////////////////////////////////////////////////////////////////////////////

#include <boost/io/ios_state.hpp> // boost::io::ios_all_saver
#include <iomanip> // std::setfill<>, std::fixed, std::setprecision, std::right,
// std::setw
#include <ostream> // std::basic_ostream<>
#include <iomanip> // std::setfill<>, std::fixed, std::setprecision, std::right, std::setw
#include <ostream> // std::basic_ostream<>

namespace glm
{
Expand Down Expand Up @@ -49,11 +47,32 @@ namespace glm

template <typename CTy> std::locale::id format_punct<CTy>::id;

template <typename CTy, typename CTr>
/* explicit */ GLM_FUNC_QUALIFIER
basic_state_saver<CTy,CTr>::basic_state_saver(std::basic_ios<CTy,CTr>& a)
: state_ (a),
flags_ (a.flags()),
precision_(a.precision()),
width_ (a.width()),
fill_ (a.fill()),
locale_ (a.getloc())
{}

template <typename CTy, typename CTr>
GLM_FUNC_QUALIFIER
basic_state_saver<CTy,CTr>::~basic_state_saver()
{
state_.imbue(locale_);
state_.fill(fill_);
state_.width(width_);
state_.precision(precision_);
state_.flags(flags_);
}

template <typename CTy, typename CTr>
/* explicit */ GLM_FUNC_QUALIFIER
basic_format_saver<CTy,CTr>::basic_format_saver(std::basic_ios<CTy,CTr>& a)
: boost::noncopyable(),
ias_ (a)
: bss_(a)
{
a.imbue(std::locale(a.getloc(), new format_punct<CTy>(get_facet<format_punct<CTy>>(a))));
}
Expand Down Expand Up @@ -171,7 +190,7 @@ namespace glm
io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy>>(os));

if (fmt.formatted) {
boost::io::basic_ios_all_saver<CTy> const ias(os);
io::basic_state_saver<CTy> const bss(os);

os << std::fixed
<< std::right
Expand Down Expand Up @@ -201,7 +220,7 @@ namespace glm
io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy>>(os));

if (fmt.formatted) {
boost::io::basic_ios_all_saver<CTy> const ias(os);
io::basic_state_saver<CTy> const bss(os);

os << std::fixed
<< std::right
Expand Down Expand Up @@ -229,7 +248,7 @@ namespace glm
io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy>>(os));

if (fmt.formatted) {
boost::io::basic_ios_all_saver<CTy> const ias(os);
io::basic_state_saver<CTy> const bss(os);

os << std::fixed
<< std::right
Expand Down Expand Up @@ -258,7 +277,7 @@ namespace glm
io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy>>(os));

if (fmt.formatted) {
boost::io::basic_ios_all_saver<CTy> const ias(os);
io::basic_state_saver<CTy> const bss(os);

os << std::fixed
<< std::right
Expand Down
43 changes: 35 additions & 8 deletions test/gtx/gtx_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <glm/gtc/type_precision.hpp>
#include <glm/gtx/io.hpp>
#include <iostream>
#include <sstream>
#include <typeinfo>

namespace {
Expand All @@ -32,6 +33,32 @@ namespace {
return os;
}

template <typename U, glm::precision P, typename T, typename CTy, typename CTr>
std::basic_string<CTy>
type_name(std::basic_ostream<CTy,CTr>& os, T const&)
{
std::basic_ostringstream<CTy,CTr> ostr;

if (typeid(T) == typeid(glm::detail::tquat<U,P>)) { ostr << "quat"; }
else if (typeid(T) == typeid(glm::detail::tvec2<U,P>)) { ostr << "vec2"; }
else if (typeid(T) == typeid(glm::detail::tvec3<U,P>)) { ostr << "vec3"; }
else if (typeid(T) == typeid(glm::detail::tvec4<U,P>)) { ostr << "vec4"; }
else if (typeid(T) == typeid(glm::detail::tmat2x2<U,P>)) { ostr << "mat2x2"; }
else if (typeid(T) == typeid(glm::detail::tmat2x3<U,P>)) { ostr << "mat2x3"; }
else if (typeid(T) == typeid(glm::detail::tmat2x4<U,P>)) { ostr << "mat2x4"; }
else if (typeid(T) == typeid(glm::detail::tmat3x2<U,P>)) { ostr << "mat3x2"; }
else if (typeid(T) == typeid(glm::detail::tmat3x3<U,P>)) { ostr << "mat3x3"; }
else if (typeid(T) == typeid(glm::detail::tmat3x4<U,P>)) { ostr << "mat3x4"; }
else if (typeid(T) == typeid(glm::detail::tmat4x2<U,P>)) { ostr << "mat4x2"; }
else if (typeid(T) == typeid(glm::detail::tmat4x3<U,P>)) { ostr << "mat4x3"; }
else if (typeid(T) == typeid(glm::detail::tmat4x4<U,P>)) { ostr << "mat4x4"; }
else { ostr << "unknown"; }

ostr << '<' << typeid(U).name() << ',' << P << '>';

return ostr.str();
}

} // namespace {

template <typename T, glm::precision P, typename OS>
Expand All @@ -47,14 +74,14 @@ int test_io_quat(OS& os)
glm::io::basic_format_saver<typename OS::char_type> const iofs(os);

os << glm::io::precision(2) << glm::io::width(1 + 2 + 1 + 2)
<< "quat<" << typeid(T).name() << ',' << P << ">: " << q << '\n';
<< type_name<T,P>(os, q) << ": " << q << '\n';
}

{
glm::io::basic_format_saver<typename OS::char_type> const iofs(os);

os << glm::io::unformatted()
<< "quat<" << typeid(T).name() << ',' << P << ">: " << q << '\n';
<< type_name<T,P>(os, q) << ": " << q << '\n';
}

return 0;
Expand All @@ -71,16 +98,16 @@ int test_io_vec(OS& os)
glm::detail::tvec3<T,P> const v3(2, 3, 4);
glm::detail::tvec4<T,P> const v4(5, 6, 7, 8);

os << "vec2<" << typeid(T).name() << ',' << P << ">: " << v2 << '\n'
<< "vec3<" << typeid(T).name() << ',' << P << ">: " << v3 << '\n'
<< "vec4<" << typeid(T).name() << ',' << P << ">: " << v4 << '\n';
os << type_name<T,P>(os, v2) << ": " << v2 << '\n'
<< type_name<T,P>(os, v3) << ": " << v3 << '\n'
<< type_name<T,P>(os, v4) << ": " << v4 << '\n';

glm::io::basic_format_saver<typename OS::char_type> const iofs(os);

os << glm::io::precision(2) << glm::io::width(1 + 2 + 1 + 2)
<< "vec2<" << typeid(T).name() << ',' << P << ">: " << v2 << '\n'
<< "vec3<" << typeid(T).name() << ',' << P << ">: " << v3 << '\n'
<< "vec4<" << typeid(T).name() << ',' << P << ">: " << v4 << '\n';
<< type_name<T,P>(os, v2) << ": " << v2 << '\n'
<< type_name<T,P>(os, v3) << ": " << v3 << '\n'
<< type_name<T,P>(os, v4) << ": " << v4 << '\n';

return 0;
}
Expand Down