79 #ifndef OR_TOOLS_MATH_OPT_CPP_ENUMS_H_
80 #define OR_TOOLS_MATH_OPT_CPP_ENUMS_H_
84 #include <type_traits>
86 #include "absl/strings/string_view.h"
87 #include "absl/types/span.h"
88 #include "absl/log/check.h"
131 template <
typename E>
168 template <
typename P>
188 template <
typename E>
195 template <
typename E>
200 template <
typename P>
201 std::optional<typename EnumProto<P>::Cpp>
EnumFromProto(
const P proto_value);
210 template <
typename E>
215 template <
typename E>
224 template <
typename E>
231 template <
typename E,
234 typename = std::enable_if_t<Enum<E>::kIsImplemented>>
237 if (opt_str.has_value()) {
240 out <<
"<invalid enum (" <<
static_cast<std::underlying_type_t<E>
>(
value)
250 template <
typename E,
253 typename = std::enable_if_t<Enum<E>::kIsImplemented>>
254 std::ostream&
operator<<(std::ostream& out,
const std::optional<E> opt_value) {
255 if (opt_value.has_value()) {
258 out <<
"<unspecified>";
267 template <
typename E>
273 template <
typename E>
278 template <
typename P>
279 std::optional<typename EnumProto<P>::Cpp>
EnumFromProto(
const P proto_value) {
286 template <
typename E>
289 CHECK(opt_str.has_value())
290 <<
"invalid value: " <<
static_cast<std::underlying_type_t<E>
>(
value);
294 template <
typename E>
299 template <
typename E>
323 #define MATH_OPT_DEFINE_ENUM(CppEnum, proto_unspecified_value) \
325 struct Enum<CppEnum> { \
326 static constexpr bool kIsImplemented = true; \
327 using Proto = CppEnum##Proto; \
328 static constexpr Proto kProtoUnspecifiedValue = proto_unspecified_value; \
329 static std::optional<absl::string_view> ToOptString(CppEnum value); \
330 static absl::Span<const CppEnum> AllValues(); \
334 struct EnumProto<CppEnum##Proto> { \
335 using Cpp = CppEnum; \
336 static constexpr CppEnum##Proto kMin = CppEnum##Proto##_MIN; \
337 static constexpr CppEnum##Proto kMax = CppEnum##Proto##_MAX; \
338 static constexpr ProtoEnumIsValid kIsValid = CppEnum##Proto##_IsValid; \
std::optional< E > EnumFromString(const absl::string_view str)
absl::string_view EnumToString(const E value)
bool(*)(int) ProtoEnumIsValid
std::optional< absl::string_view > EnumToOptString(const E value)
std::optional< typename EnumProto< P >::Cpp > EnumFromProto(const P proto_value)
std::ostream & operator<<(std::ostream &ostr, const IndicatorConstraint &constraint)
Enum< E >::Proto EnumToProto(const std::optional< E > value)
static std::optional< absl::string_view > ToOptString(E value)
static constexpr bool kIsImplemented
static absl::Span< const E > AllValues()
static constexpr Proto kProtoUnspecifiedValue
static constexpr ProtoEnumIsValid kIsValid