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

glm::io additional operators and properties #1232

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
68 changes: 42 additions & 26 deletions glm/gtx/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,21 +64,23 @@ namespace glm
char_type separator;
char_type delim_left;
char_type delim_right;
char_type fill;
char_type space;
char_type newline;
char_type firstline;
order_type order;

GLM_FUNC_DECL explicit format_punct(size_t a = 0);
GLM_FUNC_DECL explicit format_punct(format_punct const&);
GLM_FUNC_DISCARD_DECL explicit format_punct(size_t a = 0);
GLM_FUNC_DISCARD_DECL explicit format_punct(format_punct const&);
};

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

public:

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

private:

Expand Down Expand Up @@ -106,8 +108,8 @@ namespace glm
{
public:

GLM_FUNC_DECL explicit basic_format_saver(std::basic_ios<CTy,CTr>&);
GLM_FUNC_DECL ~basic_format_saver();
GLM_FUNC_DISCARD_DECL explicit basic_format_saver(std::basic_ios<CTy,CTr>&);
GLM_FUNC_DISCARD_DECL ~basic_format_saver();

private:

Expand All @@ -123,29 +125,36 @@ namespace glm
{
unsigned value;

GLM_FUNC_DECL explicit precision(unsigned);
GLM_FUNC_DISCARD_DECL explicit precision(unsigned = 3);
};

struct width
{
unsigned value;

GLM_FUNC_DECL explicit width(unsigned);
GLM_FUNC_DISCARD_DECL explicit width(unsigned = 8);
};

template<typename CTy>
struct delimeter
{
CTy value[3];

GLM_FUNC_DECL explicit delimeter(CTy /* left */, CTy /* right */, CTy /* separator */ = ',');
GLM_FUNC_DISCARD_DECL explicit delimeter(CTy /* left */ = "[", CTy /* right */ = "]", CTy /* separator */ = ',');
};

template<typename CTy>
struct filler
{
CTy value[4];
GLM_FUNC_DISCARD_DECL explicit filler(CTy /* fill */ = ' ', CTy /* space */ = ' ', CTy /* newline */ = '\n', CTy /* firstline */ = '\n');
};

struct order
{
order_type value;

GLM_FUNC_DECL explicit order(order_type);
GLM_FUNC_DISCARD_DECL explicit order(order_type = column_major);
};

// functions, inlined (inline)
Expand All @@ -157,47 +166,54 @@ namespace glm
template<typename FTy, typename CTy, typename CTr>
std::basic_ios<CTy,CTr>& unformatted(std::basic_ios<CTy,CTr>&);

template<typename FTy, typename CTy, typename CTr>
std::basic_ios<CTy,CTr>& reset(std::basic_ios<CTy,CTr>&);
template<typename FTy, typename CTy, typename CTr>
std::basic_ios<CTy,CTr>& compressed(std::basic_ios<CTy,CTr>&);

template<typename CTy, typename CTr>
std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>&, precision const&);
template<typename CTy, typename CTr>
std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>&, width const&);
template<typename CTy, typename CTr>
std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>&, delimeter<CTy> const&);
template<typename CTy, typename CTr>
std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>&, filler<CTy> const&);
template<typename CTy, typename CTr>
std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>&, order const&);
}//namespace io

template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, qua<T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, qua<T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<1, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<1, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<2, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<2, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<3, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<3, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<4, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, vec<4, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<2, 2, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<2, 2, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<2, 3, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<2, 3, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<2, 4, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<2, 4, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<3, 2, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<3, 2, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<3, 3, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<3, 3, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<3, 4, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<3, 4, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<4, 2, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<4, 2, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<4, 3, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<4, 3, T, Q> const&);
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<4, 4, T, Q> const&);
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>&, mat<4, 4, T, Q> const&);

template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DECL std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr> &,
template<typename CTy, typename CTr, typename T, qualifier Q>
GLM_FUNC_DISCARD_DECL std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr> &,
std::pair<mat<4, 4, T, Q> const, mat<4, 4, T, Q> const> const&);

/// @}
Expand Down
78 changes: 73 additions & 5 deletions glm/gtx/io.inl
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ namespace io
: std::locale::facet(a)
, formatted(true)
, precision(3)
, width(1 + 4 + 1 + precision)
, width(4 + 1 + precision)
, separator(',')
, delim_left('[')
, delim_right(']')
, fill(' ')
, space(' ')
, newline('\n')
, firstline('\n')
, order(column_major)
{}

Expand All @@ -39,8 +41,10 @@ namespace io
, separator(a.separator)
, delim_left(a.delim_left)
, delim_right(a.delim_right)
, fill(a.fill)
, space(a.space)
, newline(a.newline)
, firstline(a.firstline)
, order(a.order)
{}

Expand Down Expand Up @@ -95,13 +99,24 @@ namespace io
value[2] = c;
}

template<typename CTy>
GLM_FUNC_QUALIFIER filler<CTy>::filler(CTy a, CTy b, CTy c, CTy d)
: value()
{
value[0] = a;
value[1] = b;
value[2] = c;
value[3] = d;
}

GLM_FUNC_QUALIFIER order::order(order_type a)
: value(a)
{}

template<typename FTy, typename CTy, typename CTr>
GLM_FUNC_QUALIFIER FTy const& get_facet(std::basic_ios<CTy, CTr>& ios)
{
// Destruction handled by locale (0 passed as default argument in the constructor)
if(!std::has_facet<FTy>(ios.getloc()))
ios.imbue(std::locale(ios.getloc(), new FTy));

Expand All @@ -122,6 +137,44 @@ namespace io
return ios;
}

template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ios<CTy, CTr>& reset(std::basic_ios<CTy, CTr>& ios)
{
// could leverage on the default constructor, but requires additional memory allocation
//ios.imbue(std::locale(ios.getloc(), new format_punct<CTy>));
//return ios;

format_punct<CTy> & fmt(const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(ios)));

fmt.formatted = true;
fmt.precision = 3;
fmt.width = 4 + 1 + fmt.precision;
fmt.separator = ',';
fmt.delim_left = '[';
fmt.delim_right = ']';
fmt.fill = ' ';
fmt.space = ' ';
fmt.newline = '\n';
fmt.firstline = '\n';
fmt.order = column_major;

return ios;
}

template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ios<CTy, CTr>& compressed(std::basic_ios<CTy, CTr>& ios)
{
format_punct<CTy> & fmt(const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(ios)));

fmt.formatted = true;
fmt.width = 0;
//fmt.space = ' ';
fmt.newline = ',';
fmt.firstline = '\0';

return ios;
}

template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, precision const& a)
{
Expand All @@ -148,6 +201,19 @@ namespace io
return os;
}

template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, filler<CTy> const& a)
{
format_punct<CTy> & fmt(const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)));

fmt.fill = a.value[0];
fmt.space = a.value[1];
fmt.newline = a.value[2];
fmt.firstline = a.value[3];

return os;
}

template<typename CTy, typename CTr>
GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, order const& a)
{
Expand All @@ -174,13 +240,13 @@ namespace detail
{
io::basic_state_saver<CTy> const bss(os);

os << std::fixed << std::right << std::setprecision(static_cast<std::streamsize>(fmt.precision)) << std::setfill(fmt.space) << fmt.delim_left;
os << std::fixed << std::right << std::setprecision(static_cast<std::streamsize>(fmt.precision)) << std::setfill(fmt.fill) << fmt.delim_left;

for(length_t i(0); i < components; ++i)
{
os << std::setw(static_cast<int>(fmt.width)) << a[i];
if(components-1 != i)
os << fmt.separator;
os << fmt.separator << fmt.space;
}

os << fmt.delim_right;
Expand Down Expand Up @@ -247,7 +313,8 @@ namespace detail

if(fmt.formatted)
{
os << fmt.newline << fmt.delim_left;
if (fmt.firstline != '\0') os << fmt.firstline;
os << fmt.delim_left;

switch(fmt.order)
{
Expand Down Expand Up @@ -390,7 +457,8 @@ namespace detail

if(fmt.formatted)
{
os << fmt.newline << fmt.delim_left;
if (fmt.firstline != '\0') os << fmt.firstline;
os << fmt.delim_left;

switch(fmt.order)
{
Expand Down