| 1 | /// @ref gtx_io |
| 2 | /// @author Jan P Springer (regnirpsj@gmail.com) |
| 3 | |
| 4 | #include <iomanip> // std::fixed, std::setfill<>, std::setprecision, std::right, std::setw |
| 5 | #include <ostream> // std::basic_ostream<> |
| 6 | #include "../gtc/matrix_access.hpp" // glm::col, glm::row |
| 7 | #include "../gtx/type_trait.hpp" // glm::type<> |
| 8 | |
| 9 | namespace glm{ |
| 10 | namespace io |
| 11 | { |
| 12 | template<typename CTy> |
| 13 | GLM_FUNC_QUALIFIER format_punct<CTy>::format_punct(size_t a) |
| 14 | : std::locale::facet(a) |
| 15 | , formatted(true) |
| 16 | , precision(3) |
| 17 | , width(1 + 4 + 1 + precision) |
| 18 | , separator(',') |
| 19 | , delim_left('[') |
| 20 | , delim_right(']') |
| 21 | , space(' ') |
| 22 | , newline('\n') |
| 23 | , order(column_major) |
| 24 | {} |
| 25 | |
| 26 | template<typename CTy> |
| 27 | GLM_FUNC_QUALIFIER format_punct<CTy>::format_punct(format_punct const& a) |
| 28 | : std::locale::facet(0) |
| 29 | , formatted(a.formatted) |
| 30 | , precision(a.precision) |
| 31 | , width(a.width) |
| 32 | , separator(a.separator) |
| 33 | , delim_left(a.delim_left) |
| 34 | , delim_right(a.delim_right) |
| 35 | , space(a.space) |
| 36 | , newline(a.newline) |
| 37 | , order(a.order) |
| 38 | {} |
| 39 | |
| 40 | template<typename CTy> std::locale::id format_punct<CTy>::id; |
| 41 | |
| 42 | template<typename CTy, typename CTr> |
| 43 | GLM_FUNC_QUALIFIER basic_state_saver<CTy, CTr>::basic_state_saver(std::basic_ios<CTy, CTr>& a) |
| 44 | : state_(a) |
| 45 | , flags_(a.flags()) |
| 46 | , precision_(a.precision()) |
| 47 | , width_(a.width()) |
| 48 | , fill_(a.fill()) |
| 49 | , locale_(a.getloc()) |
| 50 | {} |
| 51 | |
| 52 | template<typename CTy, typename CTr> |
| 53 | GLM_FUNC_QUALIFIER basic_state_saver<CTy, CTr>::~basic_state_saver() |
| 54 | { |
| 55 | state_.imbue(locale_); |
| 56 | state_.fill(fill_); |
| 57 | state_.width(width_); |
| 58 | state_.precision(precision_); |
| 59 | state_.flags(flags_); |
| 60 | } |
| 61 | |
| 62 | template<typename CTy, typename CTr> |
| 63 | GLM_FUNC_QUALIFIER basic_format_saver<CTy, CTr>::basic_format_saver(std::basic_ios<CTy, CTr>& a) |
| 64 | : bss_(a) |
| 65 | { |
| 66 | a.imbue(std::locale(a.getloc(), new format_punct<CTy>(get_facet<format_punct<CTy> >(a)))); |
| 67 | } |
| 68 | |
| 69 | template<typename CTy, typename CTr> |
| 70 | GLM_FUNC_QUALIFIER |
| 71 | basic_format_saver<CTy, CTr>::~basic_format_saver() |
| 72 | {} |
| 73 | |
| 74 | GLM_FUNC_QUALIFIER precision::precision(unsigned a) |
| 75 | : value(a) |
| 76 | {} |
| 77 | |
| 78 | GLM_FUNC_QUALIFIER width::width(unsigned a) |
| 79 | : value(a) |
| 80 | {} |
| 81 | |
| 82 | template<typename CTy> |
| 83 | GLM_FUNC_QUALIFIER delimeter<CTy>::delimeter(CTy a, CTy b, CTy c) |
| 84 | : value() |
| 85 | { |
| 86 | value[0] = a; |
| 87 | value[1] = b; |
| 88 | value[2] = c; |
| 89 | } |
| 90 | |
| 91 | GLM_FUNC_QUALIFIER order::order(order_type a) |
| 92 | : value(a) |
| 93 | {} |
| 94 | |
| 95 | template<typename FTy, typename CTy, typename CTr> |
| 96 | GLM_FUNC_QUALIFIER FTy const& get_facet(std::basic_ios<CTy, CTr>& ios) |
| 97 | { |
| 98 | if(!std::has_facet<FTy>(ios.getloc())) |
| 99 | ios.imbue(std::locale(ios.getloc(), new FTy)); |
| 100 | |
| 101 | return std::use_facet<FTy>(ios.getloc()); |
| 102 | } |
| 103 | |
| 104 | template<typename CTy, typename CTr> |
| 105 | GLM_FUNC_QUALIFIER std::basic_ios<CTy, CTr>& formatted(std::basic_ios<CTy, CTr>& ios) |
| 106 | { |
| 107 | const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(ios)).formatted = true; |
| 108 | return ios; |
| 109 | } |
| 110 | |
| 111 | template<typename CTy, typename CTr> |
| 112 | GLM_FUNC_QUALIFIER std::basic_ios<CTy, CTr>& unformatted(std::basic_ios<CTy, CTr>& ios) |
| 113 | { |
| 114 | const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(ios)).formatted = false; |
| 115 | return ios; |
| 116 | } |
| 117 | |
| 118 | template<typename CTy, typename CTr> |
| 119 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, precision const& a) |
| 120 | { |
| 121 | const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)).precision = a.value; |
| 122 | return os; |
| 123 | } |
| 124 | |
| 125 | template<typename CTy, typename CTr> |
| 126 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, width const& a) |
| 127 | { |
| 128 | const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)).width = a.value; |
| 129 | return os; |
| 130 | } |
| 131 | |
| 132 | template<typename CTy, typename CTr> |
| 133 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, delimeter<CTy> const& a) |
| 134 | { |
| 135 | format_punct<CTy> & fmt(const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os))); |
| 136 | |
| 137 | fmt.delim_left = a.value[0]; |
| 138 | fmt.delim_right = a.value[1]; |
| 139 | fmt.separator = a.value[2]; |
| 140 | |
| 141 | return os; |
| 142 | } |
| 143 | |
| 144 | template<typename CTy, typename CTr> |
| 145 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<(std::basic_ostream<CTy, CTr>& os, order const& a) |
| 146 | { |
| 147 | const_cast<format_punct<CTy>&>(get_facet<format_punct<CTy> >(os)).order = a.value; |
| 148 | return os; |
| 149 | } |
| 150 | } // namespace io |
| 151 | |
| 152 | namespace detail |
| 153 | { |
| 154 | template<typename CTy, typename CTr, typename V> |
| 155 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& |
| 156 | print_vector_on(std::basic_ostream<CTy, CTr>& os, V const& a) |
| 157 | { |
| 158 | typename std::basic_ostream<CTy, CTr>::sentry const cerberus(os); |
| 159 | |
| 160 | if(cerberus) |
| 161 | { |
| 162 | io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy> >(os)); |
| 163 | |
| 164 | length_t const& components(type<V>::components); |
| 165 | |
| 166 | if(fmt.formatted) |
| 167 | { |
| 168 | io::basic_state_saver<CTy> const bss(os); |
| 169 | |
| 170 | os << std::fixed << std::right << std::setprecision(fmt.precision) << std::setfill(fmt.space) << fmt.delim_left; |
| 171 | |
| 172 | for(length_t i(0); i < components; ++i) |
| 173 | { |
| 174 | os << std::setw(fmt.width) << a[i]; |
| 175 | if(components-1 != i) |
| 176 | os << fmt.separator; |
| 177 | } |
| 178 | |
| 179 | os << fmt.delim_right; |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | for(length_t i(0); i < components; ++i) |
| 184 | { |
| 185 | os << a[i]; |
| 186 | |
| 187 | if(components-1 != i) |
| 188 | os << fmt.space; |
| 189 | } |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | return os; |
| 194 | } |
| 195 | }//namespace detail |
| 196 | |
| 197 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 198 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, qua<T, Q> const& a) |
| 199 | { |
| 200 | return detail::print_vector_on(os, a); |
| 201 | } |
| 202 | |
| 203 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 204 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<1, T, Q> const& a) |
| 205 | { |
| 206 | return detail::print_vector_on(os, a); |
| 207 | } |
| 208 | |
| 209 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 210 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<2, T, Q> const& a) |
| 211 | { |
| 212 | return detail::print_vector_on(os, a); |
| 213 | } |
| 214 | |
| 215 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 216 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<3, T, Q> const& a) |
| 217 | { |
| 218 | return detail::print_vector_on(os, a); |
| 219 | } |
| 220 | |
| 221 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 222 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, vec<4, T, Q> const& a) |
| 223 | { |
| 224 | return detail::print_vector_on(os, a); |
| 225 | } |
| 226 | |
| 227 | namespace detail |
| 228 | { |
| 229 | template<typename CTy, typename CTr, template<length_t, length_t, typename, qualifier> class M, length_t C, length_t R, typename T, qualifier Q> |
| 230 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& print_matrix_on(std::basic_ostream<CTy, CTr>& os, M<C, R, T, Q> const& a) |
| 231 | { |
| 232 | typename std::basic_ostream<CTy,CTr>::sentry const cerberus(os); |
| 233 | |
| 234 | if(cerberus) |
| 235 | { |
| 236 | io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy> >(os)); |
| 237 | |
| 238 | length_t const& cols(type<M<C, R, T, Q> >::cols); |
| 239 | length_t const& rows(type<M<C, R, T, Q> >::rows); |
| 240 | |
| 241 | if(fmt.formatted) |
| 242 | { |
| 243 | os << fmt.newline << fmt.delim_left; |
| 244 | |
| 245 | switch(fmt.order) |
| 246 | { |
| 247 | case io::column_major: |
| 248 | { |
| 249 | for(length_t i(0); i < rows; ++i) |
| 250 | { |
| 251 | if (0 != i) |
| 252 | os << fmt.space; |
| 253 | |
| 254 | os << row(a, i); |
| 255 | |
| 256 | if(rows-1 != i) |
| 257 | os << fmt.newline; |
| 258 | } |
| 259 | } |
| 260 | break; |
| 261 | |
| 262 | case io::row_major: |
| 263 | { |
| 264 | for(length_t i(0); i < cols; ++i) |
| 265 | { |
| 266 | if(0 != i) |
| 267 | os << fmt.space; |
| 268 | |
| 269 | os << column(a, i); |
| 270 | |
| 271 | if(cols-1 != i) |
| 272 | os << fmt.newline; |
| 273 | } |
| 274 | } |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | os << fmt.delim_right; |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | switch (fmt.order) |
| 283 | { |
| 284 | case io::column_major: |
| 285 | { |
| 286 | for(length_t i(0); i < cols; ++i) |
| 287 | { |
| 288 | os << column(a, i); |
| 289 | |
| 290 | if(cols - 1 != i) |
| 291 | os << fmt.space; |
| 292 | } |
| 293 | } |
| 294 | break; |
| 295 | |
| 296 | case io::row_major: |
| 297 | { |
| 298 | for (length_t i(0); i < rows; ++i) |
| 299 | { |
| 300 | os << row(a, i); |
| 301 | |
| 302 | if (rows-1 != i) |
| 303 | os << fmt.space; |
| 304 | } |
| 305 | } |
| 306 | break; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | return os; |
| 312 | } |
| 313 | }//namespace detail |
| 314 | |
| 315 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 316 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<2, 2, T, Q> const& a) |
| 317 | { |
| 318 | return detail::print_matrix_on(os, a); |
| 319 | } |
| 320 | |
| 321 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 322 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<2, 3, T, Q> const& a) |
| 323 | { |
| 324 | return detail::print_matrix_on(os, a); |
| 325 | } |
| 326 | |
| 327 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 328 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<2, 4, T, Q> const& a) |
| 329 | { |
| 330 | return detail::print_matrix_on(os, a); |
| 331 | } |
| 332 | |
| 333 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 334 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<3, 2, T, Q> const& a) |
| 335 | { |
| 336 | return detail::print_matrix_on(os, a); |
| 337 | } |
| 338 | |
| 339 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 340 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr>& operator<<(std::basic_ostream<CTy,CTr>& os, mat<3, 3, T, Q> const& a) |
| 341 | { |
| 342 | return detail::print_matrix_on(os, a); |
| 343 | } |
| 344 | |
| 345 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 346 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<3, 4, T, Q> const& a) |
| 347 | { |
| 348 | return detail::print_matrix_on(os, a); |
| 349 | } |
| 350 | |
| 351 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 352 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<4, 2, T, Q> const& a) |
| 353 | { |
| 354 | return detail::print_matrix_on(os, a); |
| 355 | } |
| 356 | |
| 357 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 358 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<4, 3, T, Q> const& a) |
| 359 | { |
| 360 | return detail::print_matrix_on(os, a); |
| 361 | } |
| 362 | |
| 363 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 364 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy,CTr> & operator<<(std::basic_ostream<CTy,CTr>& os, mat<4, 4, T, Q> const& a) |
| 365 | { |
| 366 | return detail::print_matrix_on(os, a); |
| 367 | } |
| 368 | |
| 369 | namespace detail |
| 370 | { |
| 371 | template<typename CTy, typename CTr, template<length_t, length_t, typename, qualifier> class M, length_t C, length_t R, typename T, qualifier Q> |
| 372 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& print_matrix_pair_on(std::basic_ostream<CTy, CTr>& os, std::pair<M<C, R, T, Q> const, M<C, R, T, Q> const> const& a) |
| 373 | { |
| 374 | typename std::basic_ostream<CTy,CTr>::sentry const cerberus(os); |
| 375 | |
| 376 | if(cerberus) |
| 377 | { |
| 378 | io::format_punct<CTy> const& fmt(io::get_facet<io::format_punct<CTy> >(os)); |
| 379 | M<C, R, T, Q> const& ml(a.first); |
| 380 | M<C, R, T, Q> const& mr(a.second); |
| 381 | length_t const& cols(type<M<C, R, T, Q> >::cols); |
| 382 | length_t const& rows(type<M<C, R, T, Q> >::rows); |
| 383 | |
| 384 | if(fmt.formatted) |
| 385 | { |
| 386 | os << fmt.newline << fmt.delim_left; |
| 387 | |
| 388 | switch(fmt.order) |
| 389 | { |
| 390 | case io::column_major: |
| 391 | { |
| 392 | for(length_t i(0); i < rows; ++i) |
| 393 | { |
| 394 | if(0 != i) |
| 395 | os << fmt.space; |
| 396 | |
| 397 | os << row(ml, i) << ((rows-1 != i) ? fmt.space : fmt.delim_right) << fmt.space << ((0 != i) ? fmt.space : fmt.delim_left) << row(mr, i); |
| 398 | |
| 399 | if(rows-1 != i) |
| 400 | os << fmt.newline; |
| 401 | } |
| 402 | } |
| 403 | break; |
| 404 | case io::row_major: |
| 405 | { |
| 406 | for(length_t i(0); i < cols; ++i) |
| 407 | { |
| 408 | if(0 != i) |
| 409 | os << fmt.space; |
| 410 | |
| 411 | os << column(ml, i) << ((cols-1 != i) ? fmt.space : fmt.delim_right) << fmt.space << ((0 != i) ? fmt.space : fmt.delim_left) << column(mr, i); |
| 412 | |
| 413 | if(cols-1 != i) |
| 414 | os << fmt.newline; |
| 415 | } |
| 416 | } |
| 417 | break; |
| 418 | } |
| 419 | |
| 420 | os << fmt.delim_right; |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | os << ml << fmt.space << mr; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | return os; |
| 429 | } |
| 430 | }//namespace detail |
| 431 | |
| 432 | template<typename CTy, typename CTr, typename T, qualifier Q> |
| 433 | GLM_FUNC_QUALIFIER std::basic_ostream<CTy, CTr>& operator<<( |
| 434 | std::basic_ostream<CTy, CTr> & os, |
| 435 | std::pair<mat<4, 4, T, Q> const, |
| 436 | mat<4, 4, T, Q> const> const& a) |
| 437 | { |
| 438 | return detail::print_matrix_pair_on(os, a); |
| 439 | } |
| 440 | }//namespace glm |