Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
enums.h File Reference
#include <optional>
#include <ostream>
#include <type_traits>
#include "absl/log/check.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"

Go to the source code of this file.

Classes

struct  operations_research::math_opt::Enum< E >
 
struct  operations_research::math_opt::EnumProto< P >
 

Namespaces

namespace  operations_research
 In SWIG mode, we don't want anything besides these top-level includes.
 
namespace  operations_research::math_opt
 An object oriented wrapper for quadratic constraints in ModelStorage.
 

Macros

#define MATH_OPT_DEFINE_ENUM(CppEnum, proto_unspecified_value)
 

Typedefs

using operations_research::math_opt::ProtoEnumIsValid = bool (*)(int)
 

Functions

template<typename E >
Enum< E >::Proto operations_research::math_opt::EnumToProto (std::optional< E > value)
 
template<typename E >
Enum< E >::Proto operations_research::math_opt::EnumToProto (E value)
 
template<typename P >
std::optional< typename EnumProto< P >::Cpp > operations_research::math_opt::EnumFromProto (P proto_value)
 
template<typename E >
absl::string_view operations_research::math_opt::EnumToString (E value)
 
template<typename E >
std::optional< absl::string_view > operations_research::math_opt::EnumToOptString (E value)
 
template<typename E >
std::optional< E > operations_research::math_opt::EnumFromString (absl::string_view str)
 
template<typename E , typename = std::enable_if_t<Enum<E>::kIsImplemented>>
std::ostream & operations_research::math_opt::operator<< (std::ostream &out, const E value)
 
template<typename E , typename = std::enable_if_t<Enum<E>::kIsImplemented>>
std::ostream & operations_research::math_opt::operator<< (std::ostream &out, const std::optional< E > opt_value)
 

Macro Definition Documentation

◆ MATH_OPT_DEFINE_ENUM

#define MATH_OPT_DEFINE_ENUM ( CppEnum,
proto_unspecified_value )
Value:
template <> \
struct Enum<CppEnum> { \
static constexpr bool kIsImplemented = true; \
using Proto = CppEnum##Proto; \
static constexpr Proto kProtoUnspecifiedValue = proto_unspecified_value; \
static std::optional<absl::string_view> ToOptString(CppEnum value); \
static absl::Span<const CppEnum> AllValues(); \
}; \
\
template <> \
struct EnumProto<CppEnum##Proto> { \
using Cpp = CppEnum; \
static constexpr CppEnum##Proto kMin = CppEnum##Proto##_MIN; \
static constexpr CppEnum##Proto kMax = CppEnum##Proto##_MAX; \
static constexpr ProtoEnumIsValid kIsValid = CppEnum##Proto##_IsValid; \
} /* missing semicolon to force adding it at the invocation site */
int64_t value

Macros that defines the templates specializations for Enum and EnumProto.

The CppEnum parameter is the name of the C++ enum class which values are the Proto enum values. The C++ enum must contain a value for each value of the Proto enum but the UNSPECIFIED one. The proto_unspecified_value is the UNSPECIFIED one.

It leaves two functions to be implemented in the .cc file:

absl::string_view Enum<CppEnum>::ToOptString(CppEnum value) { absl::Span<const CppEnum> Enum<CppEnum>::AllValues();

See the comment at the top of this file for an example. See the comment on Enum struct for the functions that can then be used on enums.

Definition at line 325 of file enums.h.