|
| 1 | +/* |
| 2 | + * MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2024 NUClear Contributors |
| 5 | + * |
| 6 | + * This file is part of the NUClear codebase. |
| 7 | + * See https://github.com/Fastcode/NUClear for further info. |
| 8 | + * |
| 9 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated |
| 10 | + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the |
| 11 | + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to |
| 12 | + * permit persons to whom the Software is furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the |
| 15 | + * Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE |
| 18 | + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 19 | + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 20 | + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 | + */ |
| 22 | + |
| 23 | +#ifndef NUCLEAR_PRIORITY_LEVEL_HPP |
| 24 | +#define NUCLEAR_PRIORITY_LEVEL_HPP |
| 25 | + |
| 26 | +#include <cstdint> |
| 27 | +#include <ostream> |
| 28 | +#include <string> |
| 29 | + |
| 30 | +namespace NUClear { |
| 31 | + |
| 32 | +class PriorityLevel { |
| 33 | +public: |
| 34 | + enum Value : uint8_t { |
| 35 | + UNKNOWN = 0, |
| 36 | + IDLE = 1, |
| 37 | + LOWEST = 2, |
| 38 | + LOW = 3, |
| 39 | + NORMAL = 4, |
| 40 | + HIGH = 5, |
| 41 | + HIGHEST = 6, |
| 42 | + REALTIME = 7 |
| 43 | + }; |
| 44 | + |
| 45 | + /** |
| 46 | + * Construct a PriorityLevel from a Value |
| 47 | + * |
| 48 | + * @param value The value to construct the PriorityLevel from |
| 49 | + */ |
| 50 | + constexpr PriorityLevel(const Value& value = Value::NORMAL) noexcept : value(value) {} |
| 51 | + |
| 52 | + /** |
| 53 | + * Construct a PriorityLevel from a string |
| 54 | + * |
| 55 | + * @param level The string to construct the PriorityLevel from |
| 56 | + */ |
| 57 | + PriorityLevel(const std::string& level) |
| 58 | + : value(level == "IDLE" ? Value::IDLE |
| 59 | + : level == "LOWEST" ? Value::LOWEST |
| 60 | + : level == "LOW" ? Value::LOW |
| 61 | + : level == "NORMAL" ? Value::NORMAL |
| 62 | + : level == "HIGH" ? Value::HIGH |
| 63 | + : level == "HIGHEST" ? Value::HIGHEST |
| 64 | + : level == "REALTIME" ? Value::REALTIME |
| 65 | + : Value::NORMAL) {} |
| 66 | + |
| 67 | + /** |
| 68 | + * A call operator which will return the value of the PriorityLevel |
| 69 | + * This can be useful in situations where the implicit conversion operators are ambiguous. |
| 70 | + * |
| 71 | + * @return The value of the PriorityLevel |
| 72 | + */ |
| 73 | + constexpr Value operator()() const { |
| 74 | + return value; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * A conversion operator which will return the value of the PriorityLevel |
| 79 | + * |
| 80 | + * @return The value of the PriorityLevel |
| 81 | + */ |
| 82 | + constexpr operator Value() const { |
| 83 | + return value; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * A conversion operator which will return the string representation of the PriorityLevel |
| 88 | + * |
| 89 | + * @return The string representation of the PriorityLevel |
| 90 | + */ |
| 91 | + operator std::string() const { |
| 92 | + return value == Value::IDLE ? "IDLE" |
| 93 | + : value == Value::LOWEST ? "LOWEST" |
| 94 | + : value == Value::LOW ? "LOW" |
| 95 | + : value == Value::NORMAL ? "NORMAL" |
| 96 | + : value == Value::HIGH ? "HIGH" |
| 97 | + : value == Value::HIGHEST ? "HIGHEST" |
| 98 | + : value == Value::REALTIME ? "REALTIME" |
| 99 | + : "UNKNOWN"; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Stream the PriorityLevel to an ostream, it will output the string representation of the PriorityLevel |
| 104 | + * |
| 105 | + * @param os The ostream to output to |
| 106 | + * @param level The PriorityLevel to output |
| 107 | + * |
| 108 | + * @return The ostream that was passed in |
| 109 | + */ |
| 110 | + friend std::ostream& operator<<(std::ostream& os, const PriorityLevel& level) { |
| 111 | + return os << static_cast<std::string>(level); |
| 112 | + } |
| 113 | + |
| 114 | + // Operators to compare PriorityLevel values and PriorityLevel to Value |
| 115 | + // clang-format off |
| 116 | + friend constexpr bool operator<(const PriorityLevel& lhs, const PriorityLevel& rhs) { return lhs.value < rhs.value; } |
| 117 | + friend constexpr bool operator>(const PriorityLevel& lhs, const PriorityLevel& rhs) { return lhs.value > rhs.value; } |
| 118 | + friend constexpr bool operator<=(const PriorityLevel& lhs, const PriorityLevel& rhs) { return lhs.value <= rhs.value; } |
| 119 | + friend constexpr bool operator>=(const PriorityLevel& lhs, const PriorityLevel& rhs) { return lhs.value >= rhs.value; } |
| 120 | + friend constexpr bool operator==(const PriorityLevel& lhs, const PriorityLevel& rhs) { return lhs.value == rhs.value; } |
| 121 | + friend constexpr bool operator!=(const PriorityLevel& lhs, const PriorityLevel& rhs) { return lhs.value != rhs.value; } |
| 122 | + |
| 123 | + friend constexpr bool operator<(const PriorityLevel& lhs, const Value& rhs) { return lhs.value < rhs; } |
| 124 | + friend constexpr bool operator>(const PriorityLevel& lhs, const Value& rhs) { return lhs.value > rhs; } |
| 125 | + friend constexpr bool operator<=(const PriorityLevel& lhs, const Value& rhs) { return lhs.value <= rhs; } |
| 126 | + friend constexpr bool operator>=(const PriorityLevel& lhs, const Value& rhs) { return lhs.value >= rhs; } |
| 127 | + friend constexpr bool operator==(const PriorityLevel& lhs, const Value& rhs) { return lhs.value == rhs; } |
| 128 | + friend constexpr bool operator!=(const PriorityLevel& lhs, const Value& rhs) { return lhs.value != rhs; } |
| 129 | + friend constexpr bool operator<(const Value& lhs, const PriorityLevel& rhs) { return lhs < rhs.value; } |
| 130 | + friend constexpr bool operator>(const Value& lhs, const PriorityLevel& rhs) { return lhs > rhs.value; } |
| 131 | + friend constexpr bool operator<=(const Value& lhs, const PriorityLevel& rhs) { return lhs <= rhs.value; } |
| 132 | + friend constexpr bool operator>=(const Value& lhs, const PriorityLevel& rhs) { return lhs >= rhs.value; } |
| 133 | + friend constexpr bool operator==(const Value& lhs, const PriorityLevel& rhs) { return lhs == rhs.value; } |
| 134 | + friend constexpr bool operator!=(const Value& lhs, const PriorityLevel& rhs) { return lhs != rhs.value; } |
| 135 | + |
| 136 | + friend bool operator<(const PriorityLevel& lhs, const std::string& rhs) { return static_cast<std::string>(lhs) < rhs; } |
| 137 | + friend bool operator>(const PriorityLevel& lhs, const std::string& rhs) { return static_cast<std::string>(lhs) > rhs; } |
| 138 | + friend bool operator<=(const PriorityLevel& lhs, const std::string& rhs) { return static_cast<std::string>(lhs) <= rhs; } |
| 139 | + friend bool operator>=(const PriorityLevel& lhs, const std::string& rhs) { return static_cast<std::string>(lhs) >= rhs; } |
| 140 | + friend bool operator==(const PriorityLevel& lhs, const std::string& rhs) { return static_cast<std::string>(lhs) == rhs; } |
| 141 | + friend bool operator!=(const PriorityLevel& lhs, const std::string& rhs) { return static_cast<std::string>(lhs) != rhs; } |
| 142 | + friend bool operator<(const std::string& lhs, const PriorityLevel& rhs) { return lhs < static_cast<std::string>(rhs); } |
| 143 | + friend bool operator>(const std::string& lhs, const PriorityLevel& rhs) { return lhs > static_cast<std::string>(rhs); } |
| 144 | + friend bool operator<=(const std::string& lhs, const PriorityLevel& rhs) { return lhs <= static_cast<std::string>(rhs); } |
| 145 | + friend bool operator>=(const std::string& lhs, const PriorityLevel& rhs) { return lhs >= static_cast<std::string>(rhs); } |
| 146 | + friend bool operator==(const std::string& lhs, const PriorityLevel& rhs) { return lhs == static_cast<std::string>(rhs); } |
| 147 | + friend bool operator!=(const std::string& lhs, const PriorityLevel& rhs) { return lhs != static_cast<std::string>(rhs); } |
| 148 | + // clang-format on |
| 149 | + |
| 150 | +private: |
| 151 | + /// The stored enum value |
| 152 | + Value value; |
| 153 | +}; |
| 154 | + |
| 155 | +} // namespace NUClear |
| 156 | + |
| 157 | +#endif // NUCLEAR_PRIORITY_LEVEL_HPP |
0 commit comments