Merge pull request #4594 from FernetMenta/paplayer
[xbmc.git] / xbmc / Temperature.cpp
blob240af9dd06e37d0cccef145eb400c76ab04a4333
1 /*
2 * Copyright (C) 2005-2013 Team XBMC
3 * http://xbmc.org
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "LangInfo.h"
22 #include "guilib/LocalizeStrings.h"
23 #include "Temperature.h"
24 #include "utils/StringUtils.h"
26 CTemperature::CTemperature()
28 m_value=0.0f;
29 m_state=invalid;
32 CTemperature::CTemperature(const CTemperature& temperature)
34 m_value=temperature.m_value;
35 m_state=temperature.m_state;
38 CTemperature::CTemperature(double value)
40 m_value=value;
41 m_state=valid;
44 bool CTemperature::operator >(const CTemperature& right) const
46 ASSERT(IsValid());
47 ASSERT(right.IsValid());
49 if (!IsValid() || !right.IsValid())
50 return false;
52 if (this==&right)
53 return false;
55 return (m_value>right.m_value);
58 bool CTemperature::operator >=(const CTemperature& right) const
60 return operator >(right) || operator ==(right);
63 bool CTemperature::operator <(const CTemperature& right) const
65 ASSERT(IsValid());
66 ASSERT(right.IsValid());
68 if (!IsValid() || !right.IsValid())
69 return false;
71 if (this==&right)
72 return false;
74 return (m_value<right.m_value);
77 bool CTemperature::operator <=(const CTemperature& right) const
79 return operator <(right) || operator ==(right);
82 bool CTemperature::operator ==(const CTemperature& right) const
84 ASSERT(IsValid());
85 ASSERT(right.IsValid());
87 if (!IsValid() || !right.IsValid())
88 return false;
90 if (this==&right)
91 return true;
93 return (m_value==right.m_value);
96 bool CTemperature::operator !=(const CTemperature& right) const
98 return !operator ==(right.m_value);
101 const CTemperature& CTemperature::operator =(const CTemperature& right)
103 m_state=right.m_state;
104 m_value=right.m_value;
105 return *this;
108 const CTemperature& CTemperature::operator +=(const CTemperature& right)
110 ASSERT(IsValid());
111 ASSERT(right.IsValid());
113 m_value+=right.m_value;
114 return *this;
117 const CTemperature& CTemperature::operator -=(const CTemperature& right)
119 ASSERT(IsValid());
120 ASSERT(right.IsValid());
122 m_value-=right.m_value;
123 return *this;
126 const CTemperature& CTemperature::operator *=(const CTemperature& right)
128 ASSERT(IsValid());
129 ASSERT(right.IsValid());
131 m_value*=right.m_value;
132 return *this;
135 const CTemperature& CTemperature::operator /=(const CTemperature& right)
137 ASSERT(IsValid());
138 ASSERT(right.IsValid());
140 m_value/=right.m_value;
141 return *this;
144 CTemperature CTemperature::operator +(const CTemperature& right) const
146 ASSERT(IsValid());
147 ASSERT(right.IsValid());
149 CTemperature temp(*this);
151 if (!IsValid() || !right.IsValid())
152 temp.SetState(invalid);
153 else
154 temp.m_value+=right.m_value;
156 return temp;
159 CTemperature CTemperature::operator -(const CTemperature& right) const
161 ASSERT(IsValid());
162 ASSERT(right.IsValid());
164 CTemperature temp(*this);
165 if (!IsValid() || !right.IsValid())
166 temp.SetState(invalid);
167 else
168 temp.m_value-=right.m_value;
170 return temp;
173 CTemperature CTemperature::operator *(const CTemperature& right) const
175 ASSERT(IsValid());
176 ASSERT(right.IsValid());
178 CTemperature temp(*this);
179 if (!IsValid() || !right.IsValid())
180 temp.SetState(invalid);
181 else
182 temp.m_value*=right.m_value;
183 return temp;
186 CTemperature CTemperature::operator /(const CTemperature& right) const
188 ASSERT(IsValid());
189 ASSERT(right.IsValid());
191 CTemperature temp(*this);
192 if (!IsValid() || !right.IsValid())
193 temp.SetState(invalid);
194 else
195 temp.m_value/=right.m_value;
196 return temp;
199 CTemperature& CTemperature::operator ++()
201 ASSERT(IsValid());
203 m_value++;
204 return *this;
207 CTemperature& CTemperature::operator --()
209 ASSERT(IsValid());
211 m_value--;
212 return *this;
215 CTemperature CTemperature::operator ++(int)
217 ASSERT(IsValid());
219 CTemperature temp(*this);
220 m_value++;
221 return temp;
224 CTemperature CTemperature::operator --(int)
226 ASSERT(IsValid());
228 CTemperature temp(*this);
229 m_value--;
230 return temp;
233 bool CTemperature::operator >(double right) const
235 ASSERT(IsValid());
237 if (!IsValid())
238 return false;
240 return (m_value>right);
243 bool CTemperature::operator >=(double right) const
245 return operator >(right) || operator ==(right);
248 bool CTemperature::operator <(double right) const
250 ASSERT(IsValid());
252 if (!IsValid())
253 return false;
255 return (m_value<right);
258 bool CTemperature::operator <=(double right) const
260 return operator <(right) || operator ==(right);
263 bool CTemperature::operator ==(double right) const
265 if (!IsValid())
266 return false;
268 return (m_value==right);
271 bool CTemperature::operator !=(double right) const
273 return !operator ==(right);
276 const CTemperature& CTemperature::operator +=(double right)
278 ASSERT(IsValid());
280 m_value+=right;
281 return *this;
284 const CTemperature& CTemperature::operator -=(double right)
286 ASSERT(IsValid());
288 m_value-=right;
289 return *this;
292 const CTemperature& CTemperature::operator *=(double right)
294 ASSERT(IsValid());
296 m_value*=right;
297 return *this;
300 const CTemperature& CTemperature::operator /=(double right)
302 ASSERT(IsValid());
304 m_value/=right;
305 return *this;
308 CTemperature CTemperature::operator +(double right) const
310 ASSERT(IsValid());
312 CTemperature temp(*this);
313 temp.m_value+=right;
314 return temp;
317 CTemperature CTemperature::operator -(double right) const
319 ASSERT(IsValid());
321 CTemperature temp(*this);
322 temp.m_value-=right;
323 return temp;
326 CTemperature CTemperature::operator *(double right) const
328 ASSERT(IsValid());
330 CTemperature temp(*this);
331 temp.m_value*=right;
332 return temp;
335 CTemperature CTemperature::operator /(double right) const
337 ASSERT(IsValid());
339 CTemperature temp(*this);
340 temp.m_value/=right;
341 return temp;
344 CTemperature CTemperature::CreateFromFahrenheit(double value)
346 return CTemperature(value);
349 CTemperature CTemperature::CreateFromReaumur(double value)
351 return CTemperature(value*2.25f+32.0f);
354 CTemperature CTemperature::CreateFromRankine(double value)
356 return CTemperature(value-459.67f);
359 CTemperature CTemperature::CreateFromRomer(double value)
361 return CTemperature((value-7.5f)*24.0f/7.0f+32.0f);
364 CTemperature CTemperature::CreateFromDelisle(double value)
366 CTemperature temp(212.0f - value * 1.2f);
367 return temp;
370 CTemperature CTemperature::CreateFromNewton(double value)
372 return CTemperature(value*60.0f/11.0f+32.0f);
375 CTemperature CTemperature::CreateFromCelsius(double value)
377 return CTemperature(value*1.8f+32.0f);
380 void CTemperature::Archive(CArchive& ar)
382 if (ar.IsStoring())
384 ar<<m_value;
385 ar<<(int)m_state;
387 else
389 ar>>m_value;
390 int state;
391 ar>>(int&)state;
392 m_state = CTemperature::STATE(state);
396 void CTemperature::SetState(CTemperature::STATE state)
398 m_state=state;
401 bool CTemperature::IsValid() const
403 return (m_state==valid);
406 double CTemperature::ToFahrenheit() const
408 return m_value;
411 double CTemperature::ToKelvin() const
413 return (m_value+459.67F)/1.8f;
416 double CTemperature::ToCelsius() const
418 return (m_value-32.0f)/1.8f;
421 double CTemperature::ToReaumur() const
423 return (m_value-32.0f)/2.25f;
426 double CTemperature::ToRankine() const
428 return m_value+459.67f;
431 double CTemperature::ToRomer() const
433 return (m_value-32.0f)*7.0f/24.0f+7.5f;
436 double CTemperature::ToDelisle() const
438 return (212.f-m_value)*5.0f/6.0f;
441 double CTemperature::ToNewton() const
443 return (m_value-32.0f)*11.0f/60.0f;
446 double CTemperature::ToLocale() const
448 if (!IsValid())
449 return 0;
450 double value = 0.0;
452 switch(g_langInfo.GetTempUnit())
454 case CLangInfo::TEMP_UNIT_FAHRENHEIT:
455 value=ToFahrenheit();
456 break;
457 case CLangInfo::TEMP_UNIT_KELVIN:
458 value=ToKelvin();
459 break;
460 case CLangInfo::TEMP_UNIT_CELSIUS:
461 value=ToCelsius();
462 break;
463 case CLangInfo::TEMP_UNIT_REAUMUR:
464 value=ToReaumur();
465 break;
466 case CLangInfo::TEMP_UNIT_RANKINE:
467 value=ToRankine();
468 break;
469 case CLangInfo::TEMP_UNIT_ROMER:
470 value=ToRomer();
471 break;
472 case CLangInfo::TEMP_UNIT_DELISLE:
473 value=ToDelisle();
474 break;
475 case CLangInfo::TEMP_UNIT_NEWTON:
476 value=ToNewton();
477 break;
478 default:
479 ASSERT(false);
480 break;
482 return value;
485 // Returns temperature as localized string
486 std::string CTemperature::ToString() const
488 if (!IsValid())
489 return g_localizeStrings.Get(13205); // "Unknown"
491 return StringUtils::Format("%2.0f%s", ToLocale(), g_langInfo.GetTempUnitString().c_str());