diff options
Diffstat (limited to 'xbmc/utils/Speed.cpp')
| -rw-r--r-- | xbmc/utils/Speed.cpp | 582 |
1 files changed, 582 insertions, 0 deletions
diff --git a/xbmc/utils/Speed.cpp b/xbmc/utils/Speed.cpp new file mode 100644 index 0000000..441c16c --- /dev/null +++ b/xbmc/utils/Speed.cpp | |||
| @@ -0,0 +1,582 @@ | |||
| 1 | /* | ||
| 2 | * Copyright (C) 2005-2018 Team Kodi | ||
| 3 | * This file is part of Kodi - https://kodi.tv | ||
| 4 | * | ||
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later | ||
| 6 | * See LICENSES/README.md for more information. | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "Speed.h" | ||
| 10 | |||
| 11 | #include "utils/Archive.h" | ||
| 12 | #include "utils/StringUtils.h" | ||
| 13 | |||
| 14 | #include <assert.h> | ||
| 15 | |||
| 16 | CSpeed::CSpeed() | ||
| 17 | { | ||
| 18 | m_value = 0.0; | ||
| 19 | m_valid = false; | ||
| 20 | } | ||
| 21 | |||
| 22 | CSpeed::CSpeed(const CSpeed& speed) | ||
| 23 | { | ||
| 24 | m_value = speed.m_value; | ||
| 25 | m_valid = speed.m_valid; | ||
| 26 | } | ||
| 27 | |||
| 28 | CSpeed::CSpeed(double value) | ||
| 29 | { | ||
| 30 | m_value = value; | ||
| 31 | m_valid = true; | ||
| 32 | } | ||
| 33 | |||
| 34 | bool CSpeed::operator >(const CSpeed& right) const | ||
| 35 | { | ||
| 36 | assert(IsValid()); | ||
| 37 | assert(right.IsValid()); | ||
| 38 | |||
| 39 | if (!IsValid() || !right.IsValid()) | ||
| 40 | return false; | ||
| 41 | |||
| 42 | if (this == &right) | ||
| 43 | return false; | ||
| 44 | |||
| 45 | return (m_value > right.m_value); | ||
| 46 | } | ||
| 47 | |||
| 48 | bool CSpeed::operator >=(const CSpeed& right) const | ||
| 49 | { | ||
| 50 | return operator >(right) || operator ==(right); | ||
| 51 | } | ||
| 52 | |||
| 53 | bool CSpeed::operator <(const CSpeed& right) const | ||
| 54 | { | ||
| 55 | assert(IsValid()); | ||
| 56 | assert(right.IsValid()); | ||
| 57 | |||
| 58 | if (!IsValid() || !right.IsValid()) | ||
| 59 | return false; | ||
| 60 | |||
| 61 | if (this == &right) | ||
| 62 | return false; | ||
| 63 | |||
| 64 | return (m_value < right.m_value); | ||
| 65 | } | ||
| 66 | |||
| 67 | bool CSpeed::operator <=(const CSpeed& right) const | ||
| 68 | { | ||
| 69 | return operator <(right) || operator ==(right); | ||
| 70 | } | ||
| 71 | |||
| 72 | bool CSpeed::operator ==(const CSpeed& right) const | ||
| 73 | { | ||
| 74 | assert(IsValid()); | ||
| 75 | assert(right.IsValid()); | ||
| 76 | |||
| 77 | if (!IsValid() || !right.IsValid()) | ||
| 78 | return false; | ||
| 79 | |||
| 80 | if (this == &right) | ||
| 81 | return true; | ||
| 82 | |||
| 83 | return (m_value == right.m_value); | ||
| 84 | } | ||
| 85 | |||
| 86 | bool CSpeed::operator !=(const CSpeed& right) const | ||
| 87 | { | ||
| 88 | return !operator ==(right.m_value); | ||
| 89 | } | ||
| 90 | |||
| 91 | CSpeed& CSpeed::operator =(const CSpeed& right) | ||
| 92 | { | ||
| 93 | m_valid = right.m_valid; | ||
| 94 | m_value = right.m_value; | ||
| 95 | return *this; | ||
| 96 | } | ||
| 97 | |||
| 98 | const CSpeed& CSpeed::operator +=(const CSpeed& right) | ||
| 99 | { | ||
| 100 | assert(IsValid()); | ||
| 101 | assert(right.IsValid()); | ||
| 102 | |||
| 103 | m_value += right.m_value; | ||
| 104 | return *this; | ||
| 105 | } | ||
| 106 | |||
| 107 | const CSpeed& CSpeed::operator -=(const CSpeed& right) | ||
| 108 | { | ||
| 109 | assert(IsValid()); | ||
| 110 | assert(right.IsValid()); | ||
| 111 | |||
| 112 | m_value -= right.m_value; | ||
| 113 | return *this; | ||
| 114 | } | ||
| 115 | |||
| 116 | const CSpeed& CSpeed::operator *=(const CSpeed& right) | ||
| 117 | { | ||
| 118 | assert(IsValid()); | ||
| 119 | assert(right.IsValid()); | ||
| 120 | |||
| 121 | m_value *= right.m_value; | ||
| 122 | return *this; | ||
| 123 | } | ||
| 124 | |||
| 125 | const CSpeed& CSpeed::operator /=(const CSpeed& right) | ||
| 126 | { | ||
| 127 | assert(IsValid()); | ||
| 128 | assert(right.IsValid()); | ||
| 129 | |||
| 130 | m_value /= right.m_value; | ||
| 131 | return *this; | ||
| 132 | } | ||
| 133 | |||
| 134 | CSpeed CSpeed::operator +(const CSpeed& right) const | ||
| 135 | { | ||
| 136 | assert(IsValid()); | ||
| 137 | assert(right.IsValid()); | ||
| 138 | |||
| 139 | CSpeed temp(*this); | ||
| 140 | |||
| 141 | if (!IsValid() || !right.IsValid()) | ||
| 142 | temp.SetValid(false); | ||
| 143 | else | ||
| 144 | temp.m_value += right.m_value; | ||
| 145 | |||
| 146 | return temp; | ||
| 147 | } | ||
| 148 | |||
| 149 | CSpeed CSpeed::operator -(const CSpeed& right) const | ||
| 150 | { | ||
| 151 | assert(IsValid()); | ||
| 152 | assert(right.IsValid()); | ||
| 153 | |||
| 154 | CSpeed temp(*this); | ||
| 155 | if (!IsValid() || !right.IsValid()) | ||
| 156 | temp.SetValid(false); | ||
| 157 | else | ||
| 158 | temp.m_value -= right.m_value; | ||
| 159 | |||
| 160 | return temp; | ||
| 161 | } | ||
| 162 | |||
| 163 | CSpeed CSpeed::operator *(const CSpeed& right) const | ||
| 164 | { | ||
| 165 | assert(IsValid()); | ||
| 166 | assert(right.IsValid()); | ||
| 167 | |||
| 168 | CSpeed temp(*this); | ||
| 169 | if (!IsValid() || !right.IsValid()) | ||
| 170 | temp.SetValid(false); | ||
| 171 | else | ||
| 172 | temp.m_value *= right.m_value; | ||
| 173 | return temp; | ||
| 174 | } | ||
| 175 | |||
| 176 | CSpeed CSpeed::operator /(const CSpeed& right) const | ||
| 177 | { | ||
| 178 | assert(IsValid()); | ||
| 179 | assert(right.IsValid()); | ||
| 180 | |||
| 181 | CSpeed temp(*this); | ||
| 182 | if (!IsValid() || !right.IsValid()) | ||
| 183 | temp.SetValid(false); | ||
| 184 | else | ||
| 185 | temp.m_value /= right.m_value; | ||
| 186 | return temp; | ||
| 187 | } | ||
| 188 | |||
| 189 | CSpeed& CSpeed::operator ++() | ||
| 190 | { | ||
| 191 | assert(IsValid()); | ||
| 192 | |||
| 193 | m_value++; | ||
| 194 | return *this; | ||
| 195 | } | ||
| 196 | |||
| 197 | CSpeed& CSpeed::operator --() | ||
| 198 | { | ||
| 199 | assert(IsValid()); | ||
| 200 | |||
| 201 | m_value--; | ||
| 202 | return *this; | ||
| 203 | } | ||
| 204 | |||
| 205 | CSpeed CSpeed::operator ++(int) | ||
| 206 | { | ||
| 207 | assert(IsValid()); | ||
| 208 | |||
| 209 | CSpeed temp(*this); | ||
| 210 | m_value++; | ||
| 211 | return temp; | ||
| 212 | } | ||
| 213 | |||
| 214 | CSpeed CSpeed::operator --(int) | ||
| 215 | { | ||
| 216 | assert(IsValid()); | ||
| 217 | |||
| 218 | CSpeed temp(*this); | ||
| 219 | m_value--; | ||
| 220 | return temp; | ||
| 221 | } | ||
| 222 | |||
| 223 | bool CSpeed::operator >(double right) const | ||
| 224 | { | ||
| 225 | assert(IsValid()); | ||
| 226 | |||
| 227 | if (!IsValid()) | ||
| 228 | return false; | ||
| 229 | |||
| 230 | return (m_value > right); | ||
| 231 | } | ||
| 232 | |||
| 233 | bool CSpeed::operator >=(double right) const | ||
| 234 | { | ||
| 235 | return operator >(right) || operator ==(right); | ||
| 236 | } | ||
| 237 | |||
| 238 | bool CSpeed::operator <(double right) const | ||
| 239 | { | ||
| 240 | assert(IsValid()); | ||
| 241 | |||
| 242 | if (!IsValid()) | ||
| 243 | return false; | ||
| 244 | |||
| 245 | return (m_value < right); | ||
| 246 | } | ||
| 247 | |||
| 248 | bool CSpeed::operator <=(double right) const | ||
| 249 | { | ||
| 250 | return operator <(right) || operator ==(right); | ||
| 251 | } | ||
| 252 | |||
| 253 | bool CSpeed::operator ==(double right) const | ||
| 254 | { | ||
| 255 | if (!IsValid()) | ||
| 256 | return false; | ||
| 257 | |||
| 258 | return (m_value == right); | ||
| 259 | } | ||
| 260 | |||
| 261 | bool CSpeed::operator !=(double right) const | ||
| 262 | { | ||
| 263 | return !operator ==(right); | ||
| 264 | } | ||
| 265 | |||
| 266 | const CSpeed& CSpeed::operator +=(double right) | ||
| 267 | { | ||
| 268 | assert(IsValid()); | ||
| 269 | |||
| 270 | m_value += right; | ||
| 271 | return *this; | ||
| 272 | } | ||
| 273 | |||
| 274 | const CSpeed& CSpeed::operator -=(double right) | ||
| 275 | { | ||
| 276 | assert(IsValid()); | ||
| 277 | |||
| 278 | m_value -= right; | ||
| 279 | return *this; | ||
| 280 | } | ||
| 281 | |||
| 282 | const CSpeed& CSpeed::operator *=(double right) | ||
| 283 | { | ||
| 284 | assert(IsValid()); | ||
| 285 | |||
| 286 | m_value *= right; | ||
| 287 | return *this; | ||
| 288 | } | ||
| 289 | |||
| 290 | const CSpeed& CSpeed::operator /=(double right) | ||
| 291 | { | ||
| 292 | assert(IsValid()); | ||
| 293 | |||
| 294 | m_value /= right; | ||
| 295 | return *this; | ||
| 296 | } | ||
| 297 | |||
| 298 | CSpeed CSpeed::operator +(double right) const | ||
| 299 | { | ||
| 300 | assert(IsValid()); | ||
| 301 | |||
| 302 | CSpeed temp(*this); | ||
| 303 | temp.m_value += right; | ||
| 304 | return temp; | ||
| 305 | } | ||
| 306 | |||
| 307 | CSpeed CSpeed::operator -(double right) const | ||
| 308 | { | ||
| 309 | assert(IsValid()); | ||
| 310 | |||
| 311 | CSpeed temp(*this); | ||
| 312 | temp.m_value -= right; | ||
| 313 | return temp; | ||
| 314 | } | ||
| 315 | |||
| 316 | CSpeed CSpeed::operator *(double right) const | ||
| 317 | { | ||
| 318 | assert(IsValid()); | ||
| 319 | |||
| 320 | CSpeed temp(*this); | ||
| 321 | temp.m_value *= right; | ||
| 322 | return temp; | ||
| 323 | } | ||
| 324 | |||
| 325 | CSpeed CSpeed::operator /(double right) const | ||
| 326 | { | ||
| 327 | assert(IsValid()); | ||
| 328 | |||
| 329 | CSpeed temp(*this); | ||
| 330 | temp.m_value /= right; | ||
| 331 | return temp; | ||
| 332 | } | ||
| 333 | |||
| 334 | CSpeed CSpeed::CreateFromKilometresPerHour(double value) | ||
| 335 | { | ||
| 336 | return CSpeed(value / 3.6); | ||
| 337 | } | ||
| 338 | |||
| 339 | CSpeed CSpeed::CreateFromMetresPerMinute(double value) | ||
| 340 | { | ||
| 341 | return CSpeed(value / 60.0); | ||
| 342 | } | ||
| 343 | |||
| 344 | CSpeed CSpeed::CreateFromMetresPerSecond(double value) | ||
| 345 | { | ||
| 346 | return CSpeed(value); | ||
| 347 | } | ||
| 348 | |||
| 349 | CSpeed CSpeed::CreateFromFeetPerHour(double value) | ||
| 350 | { | ||
| 351 | return CreateFromFeetPerMinute(value / 60.0); | ||
| 352 | } | ||
| 353 | |||
| 354 | CSpeed CSpeed::CreateFromFeetPerMinute(double value) | ||
| 355 | { | ||
| 356 | return CreateFromFeetPerSecond(value / 60.0); | ||
| 357 | } | ||
| 358 | |||
| 359 | CSpeed CSpeed::CreateFromFeetPerSecond(double value) | ||
| 360 | { | ||
| 361 | return CSpeed(value / 3.280839895); | ||
| 362 | } | ||
| 363 | |||
| 364 | CSpeed CSpeed::CreateFromMilesPerHour(double value) | ||
| 365 | { | ||
| 366 | return CSpeed(value / 2.236936292); | ||
| 367 | } | ||
| 368 | |||
| 369 | CSpeed CSpeed::CreateFromKnots(double value) | ||
| 370 | { | ||
| 371 | return CSpeed(value / 1.943846172); | ||
| 372 | } | ||
| 373 | |||
| 374 | CSpeed CSpeed::CreateFromBeaufort(unsigned int value) | ||
| 375 | { | ||
| 376 | if (value == 0) | ||
| 377 | return CSpeed(0.15); | ||
| 378 | if (value == 1) | ||
| 379 | return CSpeed(0.9); | ||
| 380 | if (value == 2) | ||
| 381 | return CSpeed(2.4); | ||
| 382 | if (value == 3) | ||
| 383 | return CSpeed(4.4); | ||
| 384 | if (value == 4) | ||
| 385 | return CSpeed(6.75); | ||
| 386 | if (value == 5) | ||
| 387 | return CSpeed(9.4); | ||
| 388 | if (value == 6) | ||
| 389 | return CSpeed(12.35); | ||
| 390 | if (value == 7) | ||
| 391 | return CSpeed(15.55); | ||
| 392 | if (value == 8) | ||
| 393 | return CSpeed(18.95); | ||
| 394 | if (value == 9) | ||
| 395 | return CSpeed(22.6); | ||
| 396 | if (value == 10) | ||
| 397 | return CSpeed(26.45); | ||
| 398 | if (value == 11) | ||
| 399 | return CSpeed(30.5); | ||
| 400 | |||
| 401 | return CSpeed(32.6); | ||
| 402 | } | ||
| 403 | |||
| 404 | CSpeed CSpeed::CreateFromInchPerSecond(double value) | ||
| 405 | { | ||
| 406 | return CSpeed(value / 39.37007874); | ||
| 407 | } | ||
| 408 | |||
| 409 | CSpeed CSpeed::CreateFromYardPerSecond(double value) | ||
| 410 | { | ||
| 411 | return CSpeed(value / 1.093613298); | ||
| 412 | } | ||
| 413 | |||
| 414 | CSpeed CSpeed::CreateFromFurlongPerFortnight(double value) | ||
| 415 | { | ||
| 416 | return CSpeed(value / 6012.885613871); | ||
| 417 | } | ||
| 418 | |||
| 419 | void CSpeed::Archive(CArchive& ar) | ||
| 420 | { | ||
| 421 | if (ar.IsStoring()) | ||
| 422 | { | ||
| 423 | ar << m_value; | ||
| 424 | ar << m_valid; | ||
| 425 | } | ||
| 426 | else | ||
| 427 | { | ||
| 428 | ar >> m_value; | ||
| 429 | ar >> m_valid; | ||
| 430 | } | ||
| 431 | } | ||
| 432 | |||
| 433 | bool CSpeed::IsValid() const | ||
| 434 | { | ||
| 435 | return m_valid; | ||
| 436 | } | ||
| 437 | |||
| 438 | double CSpeed::ToKilometresPerHour() const | ||
| 439 | { | ||
| 440 | return m_value * 3.6; | ||
| 441 | } | ||
| 442 | |||
| 443 | double CSpeed::ToMetresPerMinute() const | ||
| 444 | { | ||
| 445 | return m_value * 60.0; | ||
| 446 | } | ||
| 447 | |||
| 448 | double CSpeed::ToMetresPerSecond() const | ||
| 449 | { | ||
| 450 | return m_value; | ||
| 451 | } | ||
| 452 | |||
| 453 | double CSpeed::ToFeetPerHour() const | ||
| 454 | { | ||
| 455 | return ToFeetPerMinute() * 60.0; | ||
| 456 | } | ||
| 457 | |||
| 458 | double CSpeed::ToFeetPerMinute() const | ||
| 459 | { | ||
| 460 | return ToFeetPerSecond() * 60.0; | ||
| 461 | } | ||
| 462 | |||
| 463 | double CSpeed::ToFeetPerSecond() const | ||
| 464 | { | ||
| 465 | return m_value * 3.280839895; | ||
| 466 | } | ||
| 467 | |||
| 468 | double CSpeed::ToMilesPerHour() const | ||
| 469 | { | ||
| 470 | return m_value * 2.236936292; | ||
| 471 | } | ||
| 472 | |||
| 473 | double CSpeed::ToKnots() const | ||
| 474 | { | ||
| 475 | return m_value * 1.943846172; | ||
| 476 | } | ||
| 477 | |||
| 478 | double CSpeed::ToBeaufort() const | ||
| 479 | { | ||
| 480 | if (m_value < 0.3) | ||
| 481 | return 0; | ||
| 482 | if (m_value >= 0.3 && m_value < 1.5) | ||
| 483 | return 1; | ||
| 484 | if (m_value >= 1.5 && m_value < 3.3) | ||
| 485 | return 2; | ||
| 486 | if (m_value >= 3.3 && m_value < 5.5) | ||
| 487 | return 3; | ||
| 488 | if (m_value >= 5.5 && m_value < 8.0) | ||
| 489 | return 4; | ||
| 490 | if (m_value >= 8.0 && m_value < 10.8) | ||
| 491 | return 5; | ||
| 492 | if (m_value >= 10.8 && m_value < 13.9) | ||
| 493 | return 6; | ||
| 494 | if (m_value >= 13.9 && m_value < 17.2) | ||
| 495 | return 7; | ||
| 496 | if (m_value >= 17.2 && m_value < 20.7) | ||
| 497 | return 8; | ||
| 498 | if (m_value >= 20.7 && m_value < 24.5) | ||
| 499 | return 9; | ||
| 500 | if (m_value >= 24.5 && m_value < 28.4) | ||
| 501 | return 10; | ||
| 502 | if (m_value >= 28.4 && m_value < 32.6) | ||
| 503 | return 11; | ||
| 504 | |||
| 505 | return 12; | ||
| 506 | } | ||
| 507 | |||
| 508 | double CSpeed::ToInchPerSecond() const | ||
| 509 | { | ||
| 510 | return m_value * 39.37007874; | ||
| 511 | } | ||
| 512 | |||
| 513 | double CSpeed::ToYardPerSecond() const | ||
| 514 | { | ||
| 515 | return m_value * 1.093613298; | ||
| 516 | } | ||
| 517 | |||
| 518 | double CSpeed::ToFurlongPerFortnight() const | ||
| 519 | { | ||
| 520 | return m_value * 6012.885613871; | ||
| 521 | } | ||
| 522 | |||
| 523 | double CSpeed::To(Unit speedUnit) const | ||
| 524 | { | ||
| 525 | if (!IsValid()) | ||
| 526 | return 0; | ||
| 527 | |||
| 528 | double value = 0.0; | ||
| 529 | |||
| 530 | switch (speedUnit) | ||
| 531 | { | ||
| 532 | case UnitKilometresPerHour: | ||
| 533 | value = ToKilometresPerHour(); | ||
| 534 | break; | ||
| 535 | case UnitMetresPerMinute: | ||
| 536 | value = ToMetresPerMinute(); | ||
| 537 | break; | ||
| 538 | case UnitMetresPerSecond: | ||
| 539 | value = ToMetresPerSecond(); | ||
| 540 | break; | ||
| 541 | case UnitFeetPerHour: | ||
| 542 | value = ToFeetPerHour(); | ||
| 543 | break; | ||
| 544 | case UnitFeetPerMinute: | ||
| 545 | value = ToFeetPerMinute(); | ||
| 546 | break; | ||
| 547 | case UnitFeetPerSecond: | ||
| 548 | value = ToFeetPerSecond(); | ||
| 549 | break; | ||
| 550 | case UnitMilesPerHour: | ||
| 551 | value = ToMilesPerHour(); | ||
| 552 | break; | ||
| 553 | case UnitKnots: | ||
| 554 | value = ToKnots(); | ||
| 555 | break; | ||
| 556 | case UnitBeaufort: | ||
| 557 | value = ToBeaufort(); | ||
| 558 | break; | ||
| 559 | case UnitInchPerSecond: | ||
| 560 | value = ToInchPerSecond(); | ||
| 561 | break; | ||
| 562 | case UnitYardPerSecond: | ||
| 563 | value = ToYardPerSecond(); | ||
| 564 | break; | ||
| 565 | case UnitFurlongPerFortnight: | ||
| 566 | value = ToFurlongPerFortnight(); | ||
| 567 | break; | ||
| 568 | default: | ||
| 569 | assert(false); | ||
| 570 | break; | ||
| 571 | } | ||
| 572 | return value; | ||
| 573 | } | ||
| 574 | |||
| 575 | // Returns temperature as localized string | ||
| 576 | std::string CSpeed::ToString(Unit speedUnit) const | ||
| 577 | { | ||
| 578 | if (!IsValid()) | ||
| 579 | return ""; | ||
| 580 | |||
| 581 | return StringUtils::Format("%2.0f", To(speedUnit)); | ||
| 582 | } | ||
