| 1 | // ratio_io |
| 2 | // |
| 3 | // (C) Copyright Howard Hinnant |
| 4 | // (C) Copyright 2010 Vicente J. Botet Escriba |
| 5 | // Use, modification and distribution are subject to the Boost Software License, |
| 6 | // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at |
| 7 | // http://www.boost.org/LICENSE_1_0.txt). |
| 8 | // |
| 9 | // This code was adapted by Vicente from Howard Hinnant's experimental work |
| 10 | // on chrono i/o under lvm/libc++ to Boost |
| 11 | |
| 12 | #ifndef BOOST_RATIO_RATIO_IO_HPP |
| 13 | #define BOOST_RATIO_RATIO_IO_HPP |
| 14 | |
| 15 | /* |
| 16 | |
| 17 | ratio_io synopsis |
| 18 | |
| 19 | #include <ratio> |
| 20 | #include <string> |
| 21 | |
| 22 | namespace boost |
| 23 | { |
| 24 | |
| 25 | template <class Ratio, class CharT> |
| 26 | struct ratio_string |
| 27 | { |
| 28 | static basic_string<CharT> prefix(); |
| 29 | static basic_string<CharT> symbol(); |
| 30 | }; |
| 31 | |
| 32 | } // boost |
| 33 | |
| 34 | */ |
| 35 | |
| 36 | #include <boost/ratio/ratio.hpp> |
| 37 | #include <string> |
| 38 | #include <sstream> |
| 39 | |
| 40 | namespace boost { |
| 41 | |
| 42 | template <class Ratio, class CharT> |
| 43 | struct ratio_string |
| 44 | { |
| 45 | static std::basic_string<CharT> symbol() {return prefix();} |
| 46 | static std::basic_string<CharT> prefix(); |
| 47 | }; |
| 48 | |
| 49 | template <class Ratio, class CharT> |
| 50 | std::basic_string<CharT> |
| 51 | ratio_string<Ratio, CharT>::prefix() |
| 52 | { |
| 53 | std::basic_ostringstream<CharT> os; |
| 54 | os << CharT('[') << Ratio::num << CharT('/') |
| 55 | << Ratio::den << CharT(']'); |
| 56 | return os.str(); |
| 57 | } |
| 58 | |
| 59 | // atto |
| 60 | |
| 61 | template <> |
| 62 | struct ratio_string<atto, char> |
| 63 | { |
| 64 | static std::string symbol() {return std::string(1, 'a');} |
| 65 | static std::string prefix() {return std::string("atto" );} |
| 66 | }; |
| 67 | |
| 68 | template <> |
| 69 | struct ratio_string<atto, char16_t> |
| 70 | { |
| 71 | static std::u16string symbol() {return std::u16string(1, u'a');} |
| 72 | static std::u16string prefix() {return std::u16string(u"atto" );} |
| 73 | }; |
| 74 | |
| 75 | template <> |
| 76 | struct ratio_string<atto, char32_t> |
| 77 | { |
| 78 | static std::u32string symbol() {return std::u32string(1, U'a');} |
| 79 | static std::u32string prefix() {return std::u32string(U"atto" );} |
| 80 | }; |
| 81 | |
| 82 | template <> |
| 83 | struct ratio_string<atto, wchar_t> |
| 84 | { |
| 85 | static std::wstring symbol() {return std::wstring(1, L'a');} |
| 86 | static std::wstring prefix() {return std::wstring(L"atto" );} |
| 87 | }; |
| 88 | |
| 89 | // femto |
| 90 | |
| 91 | template <> |
| 92 | struct ratio_string<femto, char> |
| 93 | { |
| 94 | static std::string symbol() {return std::string(1, 'f');} |
| 95 | static std::string prefix() {return std::string("femto" );} |
| 96 | }; |
| 97 | |
| 98 | template <> |
| 99 | struct ratio_string<femto, char16_t> |
| 100 | { |
| 101 | static std::u16string symbol() {return std::u16string(1, u'f');} |
| 102 | static std::u16string prefix() {return std::u16string(u"femto" );} |
| 103 | }; |
| 104 | |
| 105 | template <> |
| 106 | struct ratio_string<femto, char32_t> |
| 107 | { |
| 108 | static std::u32string symbol() {return std::u32string(1, U'f');} |
| 109 | static std::u32string prefix() {return std::u32string(U"femto" );} |
| 110 | }; |
| 111 | |
| 112 | template <> |
| 113 | struct ratio_string<femto, wchar_t> |
| 114 | { |
| 115 | static std::wstring symbol() {return std::wstring(1, L'f');} |
| 116 | static std::wstring prefix() {return std::wstring(L"femto" );} |
| 117 | }; |
| 118 | |
| 119 | // pico |
| 120 | |
| 121 | template <> |
| 122 | struct ratio_string<pico, char> |
| 123 | { |
| 124 | static std::string symbol() {return std::string(1, 'p');} |
| 125 | static std::string prefix() {return std::string("pico" );} |
| 126 | }; |
| 127 | |
| 128 | template <> |
| 129 | struct ratio_string<pico, char16_t> |
| 130 | { |
| 131 | static std::u16string symbol() {return std::u16string(1, u'p');} |
| 132 | static std::u16string prefix() {return std::u16string(u"pico" );} |
| 133 | }; |
| 134 | |
| 135 | template <> |
| 136 | struct ratio_string<pico, char32_t> |
| 137 | { |
| 138 | static std::u32string symbol() {return std::u32string(1, U'p');} |
| 139 | static std::u32string prefix() {return std::u32string(U"pico" );} |
| 140 | }; |
| 141 | |
| 142 | template <> |
| 143 | struct ratio_string<pico, wchar_t> |
| 144 | { |
| 145 | static std::wstring symbol() {return std::wstring(1, L'p');} |
| 146 | static std::wstring prefix() {return std::wstring(L"pico" );} |
| 147 | }; |
| 148 | |
| 149 | // nano |
| 150 | |
| 151 | template <> |
| 152 | struct ratio_string<nano, char> |
| 153 | { |
| 154 | static std::string symbol() {return std::string(1, 'n');} |
| 155 | static std::string prefix() {return std::string("nano" );} |
| 156 | }; |
| 157 | |
| 158 | template <> |
| 159 | struct ratio_string<nano, char16_t> |
| 160 | { |
| 161 | static std::u16string symbol() {return std::u16string(1, u'n');} |
| 162 | static std::u16string prefix() {return std::u16string(u"nano" );} |
| 163 | }; |
| 164 | |
| 165 | template <> |
| 166 | struct ratio_string<nano, char32_t> |
| 167 | { |
| 168 | static std::u32string symbol() {return std::u32string(1, U'n');} |
| 169 | static std::u32string prefix() {return std::u32string(U"nano" );} |
| 170 | }; |
| 171 | |
| 172 | template <> |
| 173 | struct ratio_string<nano, wchar_t> |
| 174 | { |
| 175 | static std::wstring symbol() {return std::wstring(1, L'n');} |
| 176 | static std::wstring prefix() {return std::wstring(L"nano" );} |
| 177 | }; |
| 178 | |
| 179 | // micro |
| 180 | |
| 181 | template <> |
| 182 | struct ratio_string<micro, char> |
| 183 | { |
| 184 | static std::string symbol() {return std::string("\xC2\xB5" );} |
| 185 | static std::string prefix() {return std::string("micro" );} |
| 186 | }; |
| 187 | |
| 188 | template <> |
| 189 | struct ratio_string<micro, char16_t> |
| 190 | { |
| 191 | static std::u16string symbol() {return std::u16string(1, u'\xB5');} |
| 192 | static std::u16string prefix() {return std::u16string(u"micro" );} |
| 193 | }; |
| 194 | |
| 195 | template <> |
| 196 | struct ratio_string<micro, char32_t> |
| 197 | { |
| 198 | static std::u32string symbol() {return std::u32string(1, U'\xB5');} |
| 199 | static std::u32string prefix() {return std::u32string(U"micro" );} |
| 200 | }; |
| 201 | |
| 202 | template <> |
| 203 | struct ratio_string<micro, wchar_t> |
| 204 | { |
| 205 | static std::wstring symbol() {return std::wstring(1, L'\xB5');} |
| 206 | static std::wstring prefix() {return std::wstring(L"micro" );} |
| 207 | }; |
| 208 | |
| 209 | // milli |
| 210 | |
| 211 | template <> |
| 212 | struct ratio_string<milli, char> |
| 213 | { |
| 214 | static std::string symbol() {return std::string(1, 'm');} |
| 215 | static std::string prefix() {return std::string("milli" );} |
| 216 | }; |
| 217 | |
| 218 | template <> |
| 219 | struct ratio_string<milli, char16_t> |
| 220 | { |
| 221 | static std::u16string symbol() {return std::u16string(1, u'm');} |
| 222 | static std::u16string prefix() {return std::u16string(u"milli" );} |
| 223 | }; |
| 224 | |
| 225 | template <> |
| 226 | struct ratio_string<milli, char32_t> |
| 227 | { |
| 228 | static std::u32string symbol() {return std::u32string(1, U'm');} |
| 229 | static std::u32string prefix() {return std::u32string(U"milli" );} |
| 230 | }; |
| 231 | |
| 232 | template <> |
| 233 | struct ratio_string<milli, wchar_t> |
| 234 | { |
| 235 | static std::wstring symbol() {return std::wstring(1, L'm');} |
| 236 | static std::wstring prefix() {return std::wstring(L"milli" );} |
| 237 | }; |
| 238 | |
| 239 | // centi |
| 240 | |
| 241 | template <> |
| 242 | struct ratio_string<centi, char> |
| 243 | { |
| 244 | static std::string symbol() {return std::string(1, 'c');} |
| 245 | static std::string prefix() {return std::string("centi" );} |
| 246 | }; |
| 247 | |
| 248 | template <> |
| 249 | struct ratio_string<centi, char16_t> |
| 250 | { |
| 251 | static std::u16string symbol() {return std::u16string(1, u'c');} |
| 252 | static std::u16string prefix() {return std::u16string(u"centi" );} |
| 253 | }; |
| 254 | |
| 255 | template <> |
| 256 | struct ratio_string<centi, char32_t> |
| 257 | { |
| 258 | static std::u32string symbol() {return std::u32string(1, U'c');} |
| 259 | static std::u32string prefix() {return std::u32string(U"centi" );} |
| 260 | }; |
| 261 | |
| 262 | template <> |
| 263 | struct ratio_string<centi, wchar_t> |
| 264 | { |
| 265 | static std::wstring symbol() {return std::wstring(1, L'c');} |
| 266 | static std::wstring prefix() {return std::wstring(L"centi" );} |
| 267 | }; |
| 268 | |
| 269 | // deci |
| 270 | |
| 271 | template <> |
| 272 | struct ratio_string<deci, char> |
| 273 | { |
| 274 | static std::string symbol() {return std::string(1, 'd');} |
| 275 | static std::string prefix() {return std::string("deci" );} |
| 276 | }; |
| 277 | |
| 278 | template <> |
| 279 | struct ratio_string<deci, char16_t> |
| 280 | { |
| 281 | static std::u16string symbol() {return std::u16string(1, u'd');} |
| 282 | static std::u16string prefix() {return std::u16string(u"deci" );} |
| 283 | }; |
| 284 | |
| 285 | template <> |
| 286 | struct ratio_string<deci, char32_t> |
| 287 | { |
| 288 | static std::u32string symbol() {return std::u32string(1, U'd');} |
| 289 | static std::u32string prefix() {return std::u32string(U"deci" );} |
| 290 | }; |
| 291 | |
| 292 | template <> |
| 293 | struct ratio_string<deci, wchar_t> |
| 294 | { |
| 295 | static std::wstring symbol() {return std::wstring(1, L'd');} |
| 296 | static std::wstring prefix() {return std::wstring(L"deci" );} |
| 297 | }; |
| 298 | |
| 299 | // unit |
| 300 | |
| 301 | // deca |
| 302 | |
| 303 | template <> |
| 304 | struct ratio_string<deca, char> |
| 305 | { |
| 306 | static std::string symbol() {return std::string("da" );} |
| 307 | static std::string prefix() {return std::string("deca" );} |
| 308 | }; |
| 309 | |
| 310 | template <> |
| 311 | struct ratio_string<deca, char16_t> |
| 312 | { |
| 313 | static std::u16string symbol() {return std::u16string(u"da" );} |
| 314 | static std::u16string prefix() {return std::u16string(u"deca" );} |
| 315 | }; |
| 316 | |
| 317 | template <> |
| 318 | struct ratio_string<deca, char32_t> |
| 319 | { |
| 320 | static std::u32string symbol() {return std::u32string(U"da" );} |
| 321 | static std::u32string prefix() {return std::u32string(U"deca" );} |
| 322 | }; |
| 323 | |
| 324 | template <> |
| 325 | struct ratio_string<deca, wchar_t> |
| 326 | { |
| 327 | static std::wstring symbol() {return std::wstring(L"da" );} |
| 328 | static std::wstring prefix() {return std::wstring(L"deca" );} |
| 329 | }; |
| 330 | |
| 331 | // hecto |
| 332 | |
| 333 | template <> |
| 334 | struct ratio_string<hecto, char> |
| 335 | { |
| 336 | static std::string symbol() {return std::string(1, 'h');} |
| 337 | static std::string prefix() {return std::string("hecto" );} |
| 338 | }; |
| 339 | |
| 340 | template <> |
| 341 | struct ratio_string<hecto, char16_t> |
| 342 | { |
| 343 | static std::u16string symbol() {return std::u16string(1, u'h');} |
| 344 | static std::u16string prefix() {return std::u16string(u"hecto" );} |
| 345 | }; |
| 346 | |
| 347 | template <> |
| 348 | struct ratio_string<hecto, char32_t> |
| 349 | { |
| 350 | static std::u32string symbol() {return std::u32string(1, U'h');} |
| 351 | static std::u32string prefix() {return std::u32string(U"hecto" );} |
| 352 | }; |
| 353 | |
| 354 | template <> |
| 355 | struct ratio_string<hecto, wchar_t> |
| 356 | { |
| 357 | static std::wstring symbol() {return std::wstring(1, L'h');} |
| 358 | static std::wstring prefix() {return std::wstring(L"hecto" );} |
| 359 | }; |
| 360 | |
| 361 | // kilo |
| 362 | |
| 363 | template <> |
| 364 | struct ratio_string<kilo, char> |
| 365 | { |
| 366 | static std::string symbol() {return std::string(1, 'k');} |
| 367 | static std::string prefix() {return std::string("kilo" );} |
| 368 | }; |
| 369 | |
| 370 | template <> |
| 371 | struct ratio_string<kilo, char16_t> |
| 372 | { |
| 373 | static std::u16string symbol() {return std::u16string(1, u'k');} |
| 374 | static std::u16string prefix() {return std::u16string(u"kilo" );} |
| 375 | }; |
| 376 | |
| 377 | template <> |
| 378 | struct ratio_string<kilo, char32_t> |
| 379 | { |
| 380 | static std::u32string symbol() {return std::u32string(1, U'k');} |
| 381 | static std::u32string prefix() {return std::u32string(U"kilo" );} |
| 382 | }; |
| 383 | |
| 384 | template <> |
| 385 | struct ratio_string<kilo, wchar_t> |
| 386 | { |
| 387 | static std::wstring symbol() {return std::wstring(1, L'k');} |
| 388 | static std::wstring prefix() {return std::wstring(L"kilo" );} |
| 389 | }; |
| 390 | |
| 391 | // mega |
| 392 | |
| 393 | template <> |
| 394 | struct ratio_string<mega, char> |
| 395 | { |
| 396 | static std::string symbol() {return std::string(1, 'M');} |
| 397 | static std::string prefix() {return std::string("mega" );} |
| 398 | }; |
| 399 | |
| 400 | template <> |
| 401 | struct ratio_string<mega, char16_t> |
| 402 | { |
| 403 | static std::u16string symbol() {return std::u16string(1, u'M');} |
| 404 | static std::u16string prefix() {return std::u16string(u"mega" );} |
| 405 | }; |
| 406 | |
| 407 | template <> |
| 408 | struct ratio_string<mega, char32_t> |
| 409 | { |
| 410 | static std::u32string symbol() {return std::u32string(1, U'M');} |
| 411 | static std::u32string prefix() {return std::u32string(U"mega" );} |
| 412 | }; |
| 413 | |
| 414 | template <> |
| 415 | struct ratio_string<mega, wchar_t> |
| 416 | { |
| 417 | static std::wstring symbol() {return std::wstring(1, L'M');} |
| 418 | static std::wstring prefix() {return std::wstring(L"mega" );} |
| 419 | }; |
| 420 | |
| 421 | // giga |
| 422 | |
| 423 | template <> |
| 424 | struct ratio_string<giga, char> |
| 425 | { |
| 426 | static std::string symbol() {return std::string(1, 'G');} |
| 427 | static std::string prefix() {return std::string("giga" );} |
| 428 | }; |
| 429 | |
| 430 | template <> |
| 431 | struct ratio_string<giga, char16_t> |
| 432 | { |
| 433 | static std::u16string symbol() {return std::u16string(1, u'G');} |
| 434 | static std::u16string prefix() {return std::u16string(u"giga" );} |
| 435 | }; |
| 436 | |
| 437 | template <> |
| 438 | struct ratio_string<giga, char32_t> |
| 439 | { |
| 440 | static std::u32string symbol() {return std::u32string(1, U'G');} |
| 441 | static std::u32string prefix() {return std::u32string(U"giga" );} |
| 442 | }; |
| 443 | |
| 444 | template <> |
| 445 | struct ratio_string<giga, wchar_t> |
| 446 | { |
| 447 | static std::wstring symbol() {return std::wstring(1, L'G');} |
| 448 | static std::wstring prefix() {return std::wstring(L"giga" );} |
| 449 | }; |
| 450 | |
| 451 | // tera |
| 452 | |
| 453 | template <> |
| 454 | struct ratio_string<tera, char> |
| 455 | { |
| 456 | static std::string symbol() {return std::string(1, 'T');} |
| 457 | static std::string prefix() {return std::string("tera" );} |
| 458 | }; |
| 459 | |
| 460 | template <> |
| 461 | struct ratio_string<tera, char16_t> |
| 462 | { |
| 463 | static std::u16string symbol() {return std::u16string(1, u'T');} |
| 464 | static std::u16string prefix() {return std::u16string(u"tera" );} |
| 465 | }; |
| 466 | |
| 467 | template <> |
| 468 | struct ratio_string<tera, char32_t> |
| 469 | { |
| 470 | static std::u32string symbol() {return std::u32string(1, U'T');} |
| 471 | static std::u32string prefix() {return std::u32string(U"tera" );} |
| 472 | }; |
| 473 | |
| 474 | template <> |
| 475 | struct ratio_string<tera, wchar_t> |
| 476 | { |
| 477 | static std::wstring symbol() {return std::wstring(1, L'T');} |
| 478 | static std::wstring prefix() {return std::wstring(L"tera" );} |
| 479 | }; |
| 480 | |
| 481 | // peta |
| 482 | |
| 483 | template <> |
| 484 | struct ratio_string<peta, char> |
| 485 | { |
| 486 | static std::string symbol() {return std::string(1, 'P');} |
| 487 | static std::string prefix() {return std::string("peta" );} |
| 488 | }; |
| 489 | |
| 490 | template <> |
| 491 | struct ratio_string<peta, char16_t> |
| 492 | { |
| 493 | static std::u16string symbol() {return std::u16string(1, u'P');} |
| 494 | static std::u16string prefix() {return std::u16string(u"peta" );} |
| 495 | }; |
| 496 | |
| 497 | template <> |
| 498 | struct ratio_string<peta, char32_t> |
| 499 | { |
| 500 | static std::u32string symbol() {return std::u32string(1, U'P');} |
| 501 | static std::u32string prefix() {return std::u32string(U"peta" );} |
| 502 | }; |
| 503 | |
| 504 | template <> |
| 505 | struct ratio_string<peta, wchar_t> |
| 506 | { |
| 507 | static std::wstring symbol() {return std::wstring(1, L'P');} |
| 508 | static std::wstring prefix() {return std::wstring(L"peta" );} |
| 509 | }; |
| 510 | |
| 511 | // exa |
| 512 | |
| 513 | template <> |
| 514 | struct ratio_string<exa, char> |
| 515 | { |
| 516 | static std::string symbol() {return std::string(1, 'E');} |
| 517 | static std::string prefix() {return std::string("exa" );} |
| 518 | }; |
| 519 | |
| 520 | template <> |
| 521 | struct ratio_string<exa, char16_t> |
| 522 | { |
| 523 | static std::u16string symbol() {return std::u16string(1, u'E');} |
| 524 | static std::u16string prefix() {return std::u16string(u"exa" );} |
| 525 | }; |
| 526 | |
| 527 | template <> |
| 528 | struct ratio_string<exa, char32_t> |
| 529 | { |
| 530 | static std::u32string symbol() {return std::u32string(1, U'E');} |
| 531 | static std::u32string prefix() {return std::u32string(U"exa" );} |
| 532 | }; |
| 533 | |
| 534 | template <> |
| 535 | struct ratio_string<exa, wchar_t> |
| 536 | { |
| 537 | static std::wstring symbol() {return std::wstring(1, L'E');} |
| 538 | static std::wstring prefix() {return std::wstring(L"exa" );} |
| 539 | }; |
| 540 | |
| 541 | } |
| 542 | |
| 543 | #endif // BOOST_RATIO_RATIO_IO_HPP |
| 544 | |