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
34 uint8_t get_value() const { return month_value_; }
35
41 constexpr explicit Month(std::uint8_t month_value) : month_value_{month_value} {}
42
48 constexpr auto operator++() -> Month &
49 {
50 constexpr auto jan_value{1U};
51 constexpr auto dec_value{12U};
52
53 month_value_ = (month_value_ + 1) % (dec_value + 1);
54
55 if (month_value_ == 0) {
56 month_value_ = jan_value;
57 }
58
59 return *this;
60 }
61
69 constexpr auto operator++(int /* dummy parameter */) -> Month
70 {
71 Month previous{*this};
72 ++(*this);
73
74 return previous;
75 }
76
82 constexpr auto operator--() -> Month &
83 {
84 constexpr auto dec_value{12U};
85
86 month_value_ = (month_value_ - 1 % (dec_value + 1));
87
88 if (month_value_ == 0) {
89 month_value_ = dec_value;
90 }
91
92 return *this;
93 }
94
102 constexpr auto operator--(int /* dummy parameter */) -> Month
103 {
104 Month previous{*this};
105 --(*this);
106
107 return previous;
108 }
109
113 constexpr explicit operator unsigned() const { return static_cast<unsigned>(month_value_); }
114
120 [[nodiscard]] constexpr auto ok() const -> bool
121 {
122 constexpr auto jan_value{1U};
123 constexpr auto dec_value{12U};
124
125 return month_value_ >= jan_value && month_value_ <= dec_value;
126 }
127
136 friend constexpr auto operator==(const Month & x, const Month & y) -> bool
137 {
138 return x.month_value_ == y.month_value_;
139 }
140
149 friend constexpr auto operator!=(const Month & x, const Month & y) -> bool { return !(x == y); }
150
159 friend constexpr auto operator<(const Month & x, const Month & y) -> bool
160 {
161 return x.month_value_ < y.month_value_;
162 }
163
172 friend constexpr auto operator<=(const Month & x, const Month & y) -> bool
173 {
174 return x < y || x == y;
175 }
176
185 friend constexpr auto operator>(const Month & x, const Month & y) -> bool
186 {
187 return x.month_value_ > y.month_value_;
188 }
189
198 friend constexpr auto operator>=(const Month & x, const Month & y) -> bool
199 {
200 return x > y || x == y;
201 }
202
214 template <typename CharT, typename Traits>
215 friend auto operator<<(std::basic_ostream<CharT, Traits> & os, const Month & m)
216 -> std::basic_ostream<CharT, Traits> &
217 {
218 static constexpr std::array abbreviations{"Jan.", "Feb.", "Mar.", "Apr.", "May", "Jun.",
219 "Jul.", "Aug.", "Sep.", "Oct.", "Nov.", "Dec."};
220 if (!m.ok()) {
221 return os << static_cast<unsigned>(m) << " is not a valid month";
222 }
223
224 return os << abbreviations.at(static_cast<unsigned>(m) - 1);
225 }
226
227private:
228 std::uint8_t month_value_;
229};
230
231inline constexpr Month January{1};
232inline constexpr Month February{2};
233inline constexpr Month March{3};
234inline constexpr Month April{4};
235inline constexpr Month May{5};
236inline constexpr Month June{6};
237inline constexpr Month July{7};
238inline constexpr Month August{8};
239inline constexpr Month September{9};
240inline constexpr Month October{10};
241inline constexpr Month November{11};
242inline constexpr Month December{12};
243
244} // namespace carma_cooperative_perception
245
246#endif // CARMA_COOPERATIVE_PERCEPTION__MONTH_HPP_
constexpr auto operator--(int) -> Month
Post-decrement operator overload.
Definition: month.hpp:102
constexpr auto operator++(int) -> Month
Post-increment operator overload.
Definition: month.hpp:69
friend constexpr auto operator==(const Month &x, const Month &y) -> bool
Compare exact equality between two Month instances.
Definition: month.hpp:136
constexpr auto operator++() -> Month &
Pre-increment operator overload.
Definition: month.hpp:48
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:172
friend constexpr auto operator!=(const Month &x, const Month &y) -> bool
Compare exact inequality between two Month instances.
Definition: month.hpp:149
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:215
constexpr auto operator--() -> Month &
Pre-decrement operator overload.
Definition: month.hpp:82
constexpr auto ok() const -> bool
Checks if Month instance's value is within valid Gregorian calendar range.
Definition: month.hpp:120
friend constexpr auto operator<(const Month &x, const Month &y) -> bool
Check if one Month instance is less than another.
Definition: month.hpp:159
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:198
friend constexpr auto operator>(const Month &x, const Month &y) -> bool
Check if one Month instance is greater than another.
Definition: month.hpp:185
constexpr Month(std::uint8_t month_value)
Create a Month instance with the specified value.
Definition: month.hpp:41
constexpr Month September
Definition: month.hpp:239
constexpr Month June
Definition: month.hpp:236
constexpr Month April
Definition: month.hpp:234
constexpr Month January
Definition: month.hpp:231
constexpr Month May
Definition: month.hpp:235
constexpr Month July
Definition: month.hpp:237
constexpr Month December
Definition: month.hpp:242
constexpr Month February
Definition: month.hpp:232
constexpr Month October
Definition: month.hpp:240
constexpr Month March
Definition: month.hpp:233
constexpr Month November
Definition: month.hpp:241
constexpr Month August
Definition: month.hpp:238