145 #ifndef OR_TOOLS_BASE_INT_TYPE_H_
146 #define OR_TOOLS_BASE_INT_TYPE_H_
150 #include <functional>
153 #include <type_traits>
155 #include "absl/base/port.h"
156 #include "absl/strings/string_view.h"
161 template <
typename IntTypeName,
typename _ValueType>
167 #define DEFINE_INT_TYPE(int_type_name, value_type) \
168 struct int_type_name##_tag_ { \
169 static constexpr absl::string_view TypeName() { return #int_type_name; } \
171 typedef ::gtl::IntType<int_type_name##_tag_, value_type> int_type_name;
183 template <
typename IntTypeName,
typename _ValueType>
190 return IntTypeName::TypeName();
197 return static_cast<size_t>(arg.
value());
217 template <
typename ValType>
219 return static_cast<ValType
>(value_);
242 constexpr
bool operator!()
const {
return value_ == 0; }
250 #define INT_TYPE_ASSIGNMENT_OP(op) \
251 ThisType& operator op(const ThisType& arg_value) { \
252 value_ op arg_value.value(); \
255 ThisType& operator op(ValueType arg_value) { \
256 value_ op arg_value; \
266 #undef INT_TYPE_ASSIGNMENT_OP
278 invalid_integer_type_for_id_type_);
284 template <
typename IntTypeName,
typename ValueType>
287 return os << arg.
value();
297 #define INT_TYPE_ARITHMETIC_OP(op) \
298 template <typename IntTypeName, typename ValueType> \
299 constexpr IntType<IntTypeName, ValueType> operator op( \
300 IntType<IntTypeName, ValueType> id_1, \
301 IntType<IntTypeName, ValueType> id_2) { \
302 return IntType<IntTypeName, ValueType>(id_1.value() op id_2.value()); \
304 template <typename IntTypeName, typename ValueType> \
305 constexpr IntType<IntTypeName, ValueType> operator op( \
306 IntType<IntTypeName, ValueType> id, \
307 typename IntType<IntTypeName, ValueType>::ValueType arg_val) { \
308 return IntType<IntTypeName, ValueType>(id.value() op arg_val); \
310 template <typename IntTypeName, typename ValueType> \
311 constexpr IntType<IntTypeName, ValueType> operator op( \
312 typename IntType<IntTypeName, ValueType>::ValueType arg_val, \
313 IntType<IntTypeName, ValueType> id) { \
314 return IntType<IntTypeName, ValueType>(arg_val op id.value()); \
323 #undef INT_TYPE_ARITHMETIC_OP
331 #define INT_TYPE_COMPARISON_OP(op) \
332 template <typename IntTypeName, typename ValueType> \
333 static inline constexpr bool operator op( \
334 IntType<IntTypeName, ValueType> id_1, \
335 IntType<IntTypeName, ValueType> id_2) { \
336 return id_1.value() op id_2.value(); \
338 template <typename IntTypeName, typename ValueType> \
339 static inline constexpr bool operator op( \
340 IntType<IntTypeName, ValueType> id, \
341 typename IntType<IntTypeName, ValueType>::ValueType val) { \
342 return id.value() op val; \
344 template <typename IntTypeName, typename ValueType> \
345 static inline constexpr bool operator op( \
346 typename IntType<IntTypeName, ValueType>::ValueType val, \
347 IntType<IntTypeName, ValueType> id) { \
348 return val op id.value(); \
356 #undef INT_TYPE_COMPARISON_OP
362 template <
typename IntTypeName,
typename ValueType>
363 struct hash<
gtl::IntType<IntTypeName, ValueType> >
INT_TYPE_ASSIGNMENT_OP(+=)
INT_TYPE_ASSIGNMENT_OP(>>=)
IntType< IntTypeName, ValueType > ThisType
INT_TYPE_ASSIGNMENT_OP(%=)
INT_TYPE_ASSIGNMENT_OP(-=)
constexpr IntType(ValueType value)
const ThisType operator--(int v)
constexpr const ThisType operator+() const
constexpr const ThisType operator~() const
const ThisType operator++(int v)
INT_TYPE_ASSIGNMENT_OP * INT_TYPE_ASSIGNMENT_OP(/=);INT_TYPE_ASSIGNMENT_OP(<<=
constexpr const ThisType operator-() const
constexpr ValType value() const
constexpr bool operator!() const
ThisType & operator=(ValueType arg_value)
constexpr ValueType value() const
static constexpr absl::string_view TypeName()
INT_TYPE_ARITHMETIC_OP(+)
class gtl::IntType ABSL_ATTRIBUTE_PACKED
INT_TYPE_COMPARISON_OP(==)
std::ostream & operator<<(std::ostream &os, IntType< IntTypeName, ValueType > arg)
size_t operator()(const IntType &arg) const