Carma-platform v4.2.0
CARMA Platform is built on robot operating system (ROS) and utilizes open source software (OSS) that enables Cooperative Driving Automation (CDA) features to allow Automated Driving Systems to interact and cooperate with infrastructure and other vehicles through communication.
month.hpp
Go to the documentation of this file.
1// Copyright 2023 Leidos
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef CARMA_COOPERATIVE_PERCEPTION__MONTH_HPP_
16#define CARMA_COOPERATIVE_PERCEPTION__MONTH_HPP_
17
24#include <array>
25#include <ostream>
26
28{
29class Month
30{
31public:
32 Month() = default;
33
39 constexpr explicit Month(std::uint8_t month_value) : month_value_{month_value} {}
40
46 constexpr auto operator++() -> Month &
47 {
48 constexpr auto jan_value{1U};
49 constexpr auto dec_value{12U};
50
51 month_value_ = (month_value_ + 1) % (dec_value + 1);
52
53 if (month_value_ == 0) {
54 month_value_ = jan_value;
55 }
56
57 return *this;
58 }
59
67 constexpr auto operator++(int /* dummy parameter */) -> Month
68 {
69 Month previous{*this};
70 ++(*this);
71
72 return previous;
73 }
74
80 constexpr auto operator--() -> Month &
81 {
82 constexpr auto dec_value{12U};
83
84 month_value_ = (month_value_ - 1 % (dec_value + 1));
85
86 if (month_value_ == 0) {
87 month_value_ = dec_value;
88 }
89
90 return *this;
91 }
92
100 constexpr auto operator--(int /* dummy parameter */) -> Month
101 {
102 Month previous{*this};
103 --(*this);
104
105 return previous;
106 }
107
111 constexpr explicit operator unsigned() const { return static_cast<unsigned>(month_value_); }
112
118 [[nodiscard]] constexpr auto ok() const -> bool
119 {
120 constexpr auto jan_value{1U};
121 constexpr auto dec_value{12U};
122
123 return month_value_ >= jan_value && month_value_ <= dec_value;
124 }
125
134 friend constexpr auto operator==(const Month & x, const Month & y) -> bool
135 {
136 return x.month_value_ == y.month_value_;
137 }
138
147 friend constexpr auto operator!=(const Month & x, const Month & y) -> bool { return !(x == y); }
148
157 friend constexpr auto operator<(const Month & x, const Month & y) -> bool
158 {
159 return x.month_value_ < y.month_value_;
160 }
161
170 friend constexpr auto operator<=(const Month & x, const Month & y) -> bool
171 {
172 return x < y || x == y;
173 }
174
183 friend constexpr auto operator>(const Month & x, const Month & y) -> bool
184 {
185 return x.month_value_ > y.month_value_;
186 }
187
196 friend constexpr auto operator>=(const Month & x, const Month & y) -> bool
197 {
198 return x > y || x == y;
199 }
200
212 template <typename CharT, typename Traits>
213 friend auto operator<<(std::basic_ostream<CharT, Traits> & os, const Month & m)
214 -> std::basic_ostream<CharT, Traits> &
215 {
216 static constexpr std::array abbreviations{"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.",
217 "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."};
218 if (!m.ok()) {
219 return os << static_cast<unsigned>(m) << " is not a valid month";
220 }
221
222 return os << abbreviations.at(static_cast<unsigned>(m) - 1);
223 }
224
225private:
226 std::uint8_t month_value_;
227};
228
229inline constexpr Month January{1};
230inline constexpr Month February{2};
231inline constexpr Month March{3};
232inline constexpr Month April{4};
233inline constexpr Month May{5};
234inline constexpr Month June{6};
235inline constexpr Month July{7};
236inline constexpr Month August{8};
237inline constexpr Month September{9};
238inline constexpr Month October{10};
239inline constexpr Month November{11};
240inline constexpr Month December{12};
241
242} // namespace carma_cooperative_perception
243
244#endif // CARMA_COOPERATIVE_PERCEPTION__MONTH_HPP_
constexpr auto operator--(int) -> Month
Post-decrement operator overload.
Definition: month.hpp:100
constexpr auto operator++(int) -> Month
Post-increment operator overload.
Definition: month.hpp:67
friend constexpr auto operator==(const Month &x, const Month &y) -> bool
Compare exact equality between two Month instances.
Definition: month.hpp:134
constexpr auto operator++() -> Month &
Pre-increment operator overload.
Definition: month.hpp:46
friend constexpr auto operator<=(const Month &x, const Month &y) -> bool
Check if one Month instance is less than or equal to another.
Definition: month.hpp:170
friend constexpr auto operator!=(const Month &x, const Month &y) -> bool
Compare exact inequality between two Month instances.
Definition: month.hpp:147
friend auto operator<<(std::basic_ostream< CharT, Traits > &os, const Month &m) -> std::basic_ostream< CharT, Traits > &
Output a string representation of Month instance to an output stream.
Definition: month.hpp:213
constexpr auto operator--() -> Month &
Pre-decrement operator overload.
Definition: month.hpp:80
constexpr auto ok() const -> bool
Checks if Month instance's value is within valid Gregorian calendar range.
Definition: month.hpp:118
friend constexpr auto operator<(const Month &x, const Month &y) -> bool
Check if one Month instance is less than another.
Definition: month.hpp:157
friend constexpr auto operator>=(const Month &x, const Month &y) -> bool
Check if one Month instance is greater than or equal to another.
Definition: month.hpp:196
friend constexpr auto operator>(const Month &x, const Month &y) -> bool
Check if one Month instance is greater than another.
Definition: month.hpp:183
constexpr Month(std::uint8_t month_value)
Create a Month instance with the specified value.
Definition: month.hpp:39
constexpr Month September
Definition: month.hpp:237
constexpr Month June
Definition: month.hpp:234
constexpr Month April
Definition: month.hpp:232
constexpr Month January
Definition: month.hpp:229
constexpr Month May
Definition: month.hpp:233
constexpr Month July
Definition: month.hpp:235
constexpr Month December
Definition: month.hpp:240
constexpr Month February
Definition: month.hpp:230
constexpr Month October
Definition: month.hpp:238
constexpr Month March
Definition: month.hpp:231
constexpr Month November
Definition: month.hpp:239
constexpr Month August
Definition: month.hpp:236