Merge pull request #26220 from 78andyp/blurayfixes
[xbmc.git] / xbmc / utils / Temperature.cpp
blob15aad3ab51e4f40e58ab74d2ba88ee583b516e30
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 "Temperature.h"
11 #include "utils/Archive.h"
12 #include "utils/StringUtils.h"
14 #include <assert.h>
16 CTemperature::CTemperature()
18 m_value = 0.0;
19 m_valid=false;
22 CTemperature::CTemperature(const CTemperature& temperature)
24 m_value=temperature.m_value;
25 m_valid=temperature.m_valid;
28 CTemperature::CTemperature(double value)
30 m_value=value;
31 m_valid=true;
34 bool CTemperature::operator >(const CTemperature& 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 CTemperature::operator >=(const CTemperature& right) const
50 return operator >(right) || operator ==(right);
53 bool CTemperature::operator <(const CTemperature& 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 CTemperature::operator <=(const CTemperature& right) const
69 return operator <(right) || operator ==(right);
72 bool CTemperature::operator ==(const CTemperature& 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 CTemperature::operator !=(const CTemperature& right) const
88 return !operator ==(right.m_value);
91 CTemperature& CTemperature::operator =(const CTemperature& right)
93 m_valid=right.m_valid;
94 m_value=right.m_value;
95 return *this;
98 const CTemperature& CTemperature::operator +=(const CTemperature& right)
100 assert(IsValid());
101 assert(right.IsValid());
103 m_value+=right.m_value;
104 return *this;
107 const CTemperature& CTemperature::operator -=(const CTemperature& right)
109 assert(IsValid());
110 assert(right.IsValid());
112 m_value-=right.m_value;
113 return *this;
116 const CTemperature& CTemperature::operator *=(const CTemperature& right)
118 assert(IsValid());
119 assert(right.IsValid());
121 m_value*=right.m_value;
122 return *this;
125 const CTemperature& CTemperature::operator /=(const CTemperature& right)
127 assert(IsValid());
128 assert(right.IsValid());
130 m_value/=right.m_value;
131 return *this;
134 CTemperature CTemperature::operator +(const CTemperature& right) const
136 assert(IsValid());
137 assert(right.IsValid());
139 CTemperature temp(*this);
141 if (!IsValid() || !right.IsValid())
142 temp.SetValid(false);
143 else
144 temp.m_value+=right.m_value;
146 return temp;
149 CTemperature CTemperature::operator -(const CTemperature& right) const
151 assert(IsValid());
152 assert(right.IsValid());
154 CTemperature temp(*this);
155 if (!IsValid() || !right.IsValid())
156 temp.SetValid(false);
157 else
158 temp.m_value-=right.m_value;
160 return temp;
163 CTemperature CTemperature::operator *(const CTemperature& right) const
165 assert(IsValid());
166 assert(right.IsValid());
168 CTemperature temp(*this);
169 if (!IsValid() || !right.IsValid())
170 temp.SetValid(false);
171 else
172 temp.m_value*=right.m_value;
173 return temp;
176 CTemperature CTemperature::operator /(const CTemperature& right) const
178 assert(IsValid());
179 assert(right.IsValid());
181 CTemperature temp(*this);
182 if (!IsValid() || !right.IsValid())
183 temp.SetValid(false);
184 else
185 temp.m_value/=right.m_value;
186 return temp;
189 CTemperature& CTemperature::operator ++()
191 assert(IsValid());
193 m_value++;
194 return *this;
197 CTemperature& CTemperature::operator --()
199 assert(IsValid());
201 m_value--;
202 return *this;
205 CTemperature CTemperature::operator ++(int)
207 assert(IsValid());
209 CTemperature temp(*this);
210 m_value++;
211 return temp;
214 CTemperature CTemperature::operator --(int)
216 assert(IsValid());
218 CTemperature temp(*this);
219 m_value--;
220 return temp;
223 bool CTemperature::operator >(double right) const
225 assert(IsValid());
227 if (!IsValid())
228 return false;
230 return (m_value>right);
233 bool CTemperature::operator >=(double right) const
235 return operator >(right) || operator ==(right);
238 bool CTemperature::operator <(double right) const
240 assert(IsValid());
242 if (!IsValid())
243 return false;
245 return (m_value<right);
248 bool CTemperature::operator <=(double right) const
250 return operator <(right) || operator ==(right);
253 bool CTemperature::operator ==(double right) const
255 if (!IsValid())
256 return false;
258 return (m_value==right);
261 bool CTemperature::operator !=(double right) const
263 return !operator ==(right);
266 const CTemperature& CTemperature::operator +=(double right)
268 assert(IsValid());
270 m_value+=right;
271 return *this;
274 const CTemperature& CTemperature::operator -=(double right)
276 assert(IsValid());
278 m_value-=right;
279 return *this;
282 const CTemperature& CTemperature::operator *=(double right)
284 assert(IsValid());
286 m_value*=right;
287 return *this;
290 const CTemperature& CTemperature::operator /=(double right)
292 assert(IsValid());
294 m_value/=right;
295 return *this;
298 CTemperature CTemperature::operator +(double right) const
300 assert(IsValid());
302 CTemperature temp(*this);
303 temp.m_value+=right;
304 return temp;
307 CTemperature CTemperature::operator -(double right) const
309 assert(IsValid());
311 CTemperature temp(*this);
312 temp.m_value-=right;
313 return temp;
316 CTemperature CTemperature::operator *(double right) const
318 assert(IsValid());
320 CTemperature temp(*this);
321 temp.m_value*=right;
322 return temp;
325 CTemperature CTemperature::operator /(double right) const
327 assert(IsValid());
329 CTemperature temp(*this);
330 temp.m_value/=right;
331 return temp;
334 CTemperature CTemperature::CreateFromFahrenheit(double value)
336 return CTemperature(value);
339 CTemperature CTemperature::CreateFromReaumur(double value)
341 return CTemperature(value * 2.25 + 32.0);
344 CTemperature CTemperature::CreateFromRankine(double value)
346 return CTemperature(value - 459.67);
349 CTemperature CTemperature::CreateFromRomer(double value)
351 return CTemperature((value - 7.5) * 24.0 / 7.0 + 32.0);
354 CTemperature CTemperature::CreateFromDelisle(double value)
356 CTemperature temp(212.0 - value * 1.2);
357 return temp;
360 CTemperature CTemperature::CreateFromNewton(double value)
362 return CTemperature(value * 60.0 / 11.0 + 32.0);
365 CTemperature CTemperature::CreateFromCelsius(double value)
367 return CTemperature(value * 1.8 + 32.0);
370 CTemperature CTemperature::CreateFromKelvin(double value)
372 return CTemperature((value - 273.15) * 1.8 + 32.0);
375 void CTemperature::Archive(CArchive& ar)
377 if (ar.IsStoring())
379 ar<<m_value;
380 ar<<m_valid;
382 else
384 ar>>m_value;
385 ar>>m_valid;
389 bool CTemperature::IsValid() const
391 return m_valid;
394 double CTemperature::ToFahrenheit() const
396 return m_value;
399 double CTemperature::ToKelvin() const
401 return (m_value + 459.67) / 1.8;
404 double CTemperature::ToCelsius() const
406 return (m_value - 32.0) / 1.8;
409 double CTemperature::ToReaumur() const
411 return (m_value - 32.0) / 2.25;
414 double CTemperature::ToRankine() const
416 return m_value + 459.67;
419 double CTemperature::ToRomer() const
421 return (m_value - 32.0) * 7.0 / 24.0 + 7.5;
424 double CTemperature::ToDelisle() const
426 return (212.0 - m_value) * 5.0 / 6.0;
429 double CTemperature::ToNewton() const
431 return (m_value - 32.0) * 11.0 / 60.0;
434 double CTemperature::To(Unit temperatureUnit) const
436 if (!IsValid())
437 return 0;
439 double value = 0.0;
441 switch (temperatureUnit)
443 case UnitFahrenheit:
444 value=ToFahrenheit();
445 break;
446 case UnitKelvin:
447 value=ToKelvin();
448 break;
449 case UnitCelsius:
450 value=ToCelsius();
451 break;
452 case UnitReaumur:
453 value=ToReaumur();
454 break;
455 case UnitRankine:
456 value=ToRankine();
457 break;
458 case UnitRomer:
459 value=ToRomer();
460 break;
461 case UnitDelisle:
462 value=ToDelisle();
463 break;
464 case UnitNewton:
465 value=ToNewton();
466 break;
467 default:
468 assert(false);
469 break;
471 return value;
474 // Returns temperature as localized string
475 std::string CTemperature::ToString(Unit temperatureUnit) const
477 if (!IsValid())
478 return "";
480 return StringUtils::Format("{:2.0f}", To(temperatureUnit));