ubuntu-location-service  0.0.2
velocity.h
1 /*
2  * Copyright © 2012-2013 Canonical Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU Lesser General Public License version 3,
6  * as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Authored by: Thomas Voß <thomas.voss@canonical.com>
17  */
18 #ifndef LOCATION_SERVICE_COM_UBUNTU_LOCATION_VELOCITY_H_
19 #define LOCATION_SERVICE_COM_UBUNTU_LOCATION_VELOCITY_H_
20 
21 #include "com/ubuntu/location/accuracy.h"
22 #include "com/ubuntu/location/units/units.h"
23 
24 #include <limits>
25 #include <ostream>
26 #include <stdexcept>
27 
28 namespace com
29 {
30 namespace ubuntu
31 {
32 namespace location
33 {
34 struct Velocity
35 {
36  typedef units::Velocity Unit;
37  typedef units::Quantity<Unit> Quantity;
38 
39  static inline const Quantity& min()
40  {
41  static const Quantity instance = Quantity::from_value(0.);
42  return instance;
43  }
44 
45  static inline const Quantity max()
46  {
47  static const Quantity instance = Quantity::from_value(std::numeric_limits<double>::max());
48  return instance;
49  }
50 
51  Velocity(const Quantity& value = Quantity()) : value(value)
52  {
53  if (value < Velocity::min())
54  throw std::out_of_range("");
55  if (value > Velocity::max())
56  throw std::out_of_range("");
57  }
58 
59  inline bool operator==(const Velocity& rhs) const
60  {
61  return value == rhs.value;
62  }
63 
64  inline bool operator!=(const Velocity& rhs) const
65  {
66  return value != rhs.value;
67  }
68 
69  Quantity value;
70 };
71 
72 inline std::ostream& operator<<(std::ostream& out, const Velocity& velocity)
73 {
74  out << "Velocity(" << velocity.value << ")";
75  return out;
76 }
77 
78 template<>
80 {
81  static AccuracyLevel classify(const Velocity& h)
82  {
83  if (h.value > (1.f * units::MetersPerSecond))
84  return AccuracyLevel::worst;
85 
86  if (h.value <= (1.f * units::MetersPerSecond))
87  return AccuracyLevel::best;
88 
89  return AccuracyLevel::worst;
90  }
91 
92  static Accuracy<Velocity> best()
93  {
94  return Accuracy<Velocity>{Velocity{Velocity::min()}};
95  }
96 
97  static Accuracy<Velocity> worst()
98  {
99  return Accuracy<Velocity>{Velocity{2*units::MetersPerSecond}};
100  }
101 };
102 }
103 }
104 }
105 
106 #endif // LOCATION_SERVICE_COM_UBUNTU_LOCATION_VELOCITY_H_