ubuntu-location-service  0.0.2
heading.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_HEADING_H_
19 #define LOCATION_SERVICE_COM_UBUNTU_LOCATION_HEADING_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 Heading
35 {
36  typedef units::PlaneAngle Unit;
37  typedef units::Quantity<Unit> Quantity;
38 
39  static const Quantity& min()
40  {
41  static const auto instance = Heading::Quantity::from_value(0.);
42  return instance;
43  }
44  static const Quantity& max()
45  {
46  static const auto instance = Heading::Quantity::from_value(360.);
47  return instance;
48  }
49 
50  Heading(const Quantity& value = Quantity()) : value(value)
51  {
52  if (value < min())
53  throw std::out_of_range("");
54  if (value > max())
55  throw std::out_of_range("");
56  }
57 
58  bool operator==(const Heading& rhs) const
59  {
60  return value == rhs.value;
61  }
62 
63  bool operator!=(const Heading& rhs) const
64  {
65  return value != rhs.value;
66  }
67 
68  Quantity value;
69 };
70 
71 inline std::ostream& operator<<(std::ostream& out, const Heading& heading)
72 {
73  out << "Heading(" << heading.value << ")";
74  return out;
75 }
76 
77 template<>
79 {
80  static AccuracyLevel classify(const Heading& h)
81  {
82  static const auto half = 0.5 * Heading::max();
83  if(h.value > half)
84  return AccuracyLevel::worst;
85 
86  if(h.value < half)
87  return AccuracyLevel::best;
88 
89  return AccuracyLevel::worst;
90  }
91 
92  static Accuracy<Heading> best()
93  {
94  return Accuracy<Heading>{Heading{Heading::min()}};
95  }
96 
97  static Accuracy<Heading> worst()
98  {
99  return Accuracy<Heading>{Heading{Heading::max()}};
100  }
101 };
102 }
103 }
104 }
105 
106 #endif // LOCATION_SERVICE_COM_UBUNTU_LOCATION_HEADING_H_