Merge pull request #26166 from ksooo/improve-plugin-ctx-menus
[xbmc.git] / xbmc / utils / Speed.cpp
blobd1326c7f90320f6a8dbbfa4a15687a84b0e549d5
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "Speed.h"
11 #include "utils/Archive.h"
12 #include "utils/StringUtils.h"
14 #include <assert.h>
16 CSpeed::CSpeed()
18 m_value = 0.0;
19 m_valid = false;
22 CSpeed::CSpeed(const CSpeed& speed)
24 m_value = speed.m_value;
25 m_valid = speed.m_valid;
28 CSpeed::CSpeed(double value)
30 m_value = value;
31 m_valid = true;
34 bool CSpeed::operator >(const CSpeed& right) const
36 assert(IsValid());
37 assert(right.IsValid());
39 if (!IsValid() || !right.IsValid())
40 return false;
42 if (this == &right)
43 return false;
45 return (m_value > right.m_value);
48 bool CSpeed::operator >=(const CSpeed& right) const
50 return operator >(right) || operator ==(right);
53 bool CSpeed::operator <(const CSpeed& right) const
55 assert(IsValid());
56 assert(right.IsValid());
58 if (!IsValid() || !right.IsValid())
59 return false;
61 if (this == &right)
62 return false;
64 return (m_value < right.m_value);
67 bool CSpeed::operator <=(const CSpeed& right) const
69 return operator <(right) || operator ==(right);
72 bool CSpeed::operator ==(const CSpeed& right) const
74 assert(IsValid());
75 assert(right.IsValid());
77 if (!IsValid() || !right.IsValid())
78 return false;
80 if (this == &right)
81 return true;
83 return (m_value == right.m_value);
86 bool CSpeed::operator !=(const CSpeed& right) const
88 return !operator ==(right.m_value);
91 CSpeed& CSpeed::operator =(const CSpeed& right)
93 m_valid = right.m_valid;
94 m_value = right.m_value;
95 return *this;
98 const CSpeed& CSpeed::operator +=(const CSpeed& right)
100 assert(IsValid());
101 assert(right.IsValid());
103 m_value += right.m_value;
104 return *this;
107 const CSpeed& CSpeed::operator -=(const CSpeed& right)
109 assert(IsValid());
110 assert(right.IsValid());
112 m_value -= right.m_value;
113 return *this;
116 const CSpeed& CSpeed::operator *=(const CSpeed& right)
118 assert(IsValid());
119 assert(right.IsValid());
121 m_value *= right.m_value;
122 return *this;
125 const CSpeed& CSpeed::operator /=(const CSpeed& right)
127 assert(IsValid());
128 assert(right.IsValid());
130 m_value /= right.m_value;
131 return *this;
134 CSpeed CSpeed::operator +(const CSpeed& right) const
136 assert(IsValid());
137 assert(right.IsValid());
139 CSpeed temp(*this);
141 if (!IsValid() || !right.IsValid())
142 temp.SetValid(false);
143 else
144 temp.m_value += right.m_value;
146 return temp;
149 CSpeed CSpeed::operator -(const CSpeed& right) const
151 assert(IsValid());
152 assert(right.IsValid());
154 CSpeed temp(*this);
155 if (!IsValid() || !right.IsValid())
156 temp.SetValid(false);
157 else
158 temp.m_value -= right.m_value;
160 return temp;
163 CSpeed CSpeed::operator *(const CSpeed& right) const
165 assert(IsValid());
166 assert(right.IsValid());
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;
176 CSpeed CSpeed::operator /(const CSpeed& right) const
178 assert(IsValid());
179 assert(right.IsValid());
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;
189 CSpeed& CSpeed::operator ++()
191 assert(IsValid());
193 m_value++;
194 return *this;
197 CSpeed& CSpeed::operator --()
199 assert(IsValid());
201 m_value--;
202 return *this;
205 CSpeed CSpeed::operator ++(int)
207 assert(IsValid());
209 CSpeed temp(*this);
210 m_value++;
211 return temp;
214 CSpeed CSpeed::operator --(int)
216 assert(IsValid());
218 CSpeed temp(*this);
219 m_value--;
220 return temp;
223 bool CSpeed::operator >(double right) const
225 assert(IsValid());
227 if (!IsValid())
228 return false;
230 return (m_value > right);
233 bool CSpeed::operator >=(double right) const
235 return operator >(right) || operator ==(right);
238 bool CSpeed::operator <(double right) const
240 assert(IsValid());
242 if (!IsValid())
243 return false;
245 return (m_value < right);
248 bool CSpeed::operator <=(double right) const
250 return operator <(right) || operator ==(right);
253 bool CSpeed::operator ==(double right) const
255 if (!IsValid())
256 return false;
258 return (m_value == right);
261 bool CSpeed::operator !=(double right) const
263 return !operator ==(right);
266 const CSpeed& CSpeed::operator +=(double right)
268 assert(IsValid());
270 m_value += right;
271 return *this;
274 const CSpeed& CSpeed::operator -=(double right)
276 assert(IsValid());
278 m_value -= right;
279 return *this;
282 const CSpeed& CSpeed::operator *=(double right)
284 assert(IsValid());
286 m_value *= right;
287 return *this;
290 const CSpeed& CSpeed::operator /=(double right)
292 assert(IsValid());
294 m_value /= right;
295 return *this;
298 CSpeed CSpeed::operator +(double right) const
300 assert(IsValid());
302 CSpeed temp(*this);
303 temp.m_value += right;
304 return temp;
307 CSpeed CSpeed::operator -(double right) const
309 assert(IsValid());
311 CSpeed temp(*this);
312 temp.m_value -= right;
313 return temp;
316 CSpeed CSpeed::operator *(double right) const
318 assert(IsValid());
320 CSpeed temp(*this);
321 temp.m_value *= right;
322 return temp;
325 CSpeed CSpeed::operator /(double right) const
327 assert(IsValid());
329 CSpeed temp(*this);
330 temp.m_value /= right;
331 return temp;
334 CSpeed CSpeed::CreateFromKilometresPerHour(double value)
336 return CSpeed(value / 3.6);
339 CSpeed CSpeed::CreateFromMetresPerMinute(double value)
341 return CSpeed(value / 60.0);
344 CSpeed CSpeed::CreateFromMetresPerSecond(double value)
346 return CSpeed(value);
349 CSpeed CSpeed::CreateFromFeetPerHour(double value)
351 return CreateFromFeetPerMinute(value / 60.0);
354 CSpeed CSpeed::CreateFromFeetPerMinute(double value)
356 return CreateFromFeetPerSecond(value / 60.0);
359 CSpeed CSpeed::CreateFromFeetPerSecond(double value)
361 return CSpeed(value / 3.280839895);
364 CSpeed CSpeed::CreateFromMilesPerHour(double value)
366 return CSpeed(value / 2.236936292);
369 CSpeed CSpeed::CreateFromKnots(double value)
371 return CSpeed(value / 1.943846172);
374 CSpeed CSpeed::CreateFromBeaufort(unsigned int value)
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);
401 return CSpeed(32.6);
404 CSpeed CSpeed::CreateFromInchPerSecond(double value)
406 return CSpeed(value / 39.37007874);
409 CSpeed CSpeed::CreateFromYardPerSecond(double value)
411 return CSpeed(value / 1.093613298);
414 CSpeed CSpeed::CreateFromFurlongPerFortnight(double value)
416 return CSpeed(value / 6012.885613871);
419 void CSpeed::Archive(CArchive& ar)
421 if (ar.IsStoring())
423 ar << m_value;
424 ar << m_valid;
426 else
428 ar >> m_value;
429 ar >> m_valid;
433 bool CSpeed::IsValid() const
435 return m_valid;
438 double CSpeed::ToKilometresPerHour() const
440 return m_value * 3.6;
443 double CSpeed::ToMetresPerMinute() const
445 return m_value * 60.0;
448 double CSpeed::ToMetresPerSecond() const
450 return m_value;
453 double CSpeed::ToFeetPerHour() const
455 return ToFeetPerMinute() * 60.0;
458 double CSpeed::ToFeetPerMinute() const
460 return ToFeetPerSecond() * 60.0;
463 double CSpeed::ToFeetPerSecond() const
465 return m_value * 3.280839895;
468 double CSpeed::ToMilesPerHour() const
470 return m_value * 2.236936292;
473 double CSpeed::ToKnots() const
475 return m_value * 1.943846172;
478 double CSpeed::ToBeaufort() const
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;
505 return 12;
508 double CSpeed::ToInchPerSecond() const
510 return m_value * 39.37007874;
513 double CSpeed::ToYardPerSecond() const
515 return m_value * 1.093613298;
518 double CSpeed::ToFurlongPerFortnight() const
520 return m_value * 6012.885613871;
523 double CSpeed::To(Unit speedUnit) const
525 if (!IsValid())
526 return 0;
528 double value = 0.0;
530 switch (speedUnit)
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;
572 return value;
575 // Returns temperature as localized string
576 std::string CSpeed::ToString(Unit speedUnit) const
578 if (!IsValid())
579 return "";
581 return StringUtils::Format("{:2.0f}", To(speedUnit));