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/strings/string_view.h"
160 template <
typename IntTypeName,
typename _ValueType>
166 #define DEFINE_INT_TYPE(int_type_name, value_type) \
167 struct int_type_name##_tag_ { \
168 static constexpr absl::string_view TypeName() { return #int_type_name; } \
170 typedef ::gtl::IntType<int_type_name##_tag_, value_type> int_type_name;
182 template <
typename IntTypeName,
typename _ValueType>
189 return IntTypeName::TypeName();
195 return static_cast<size_t>(arg.
value());
215 template <
typename ValType>
217 return static_cast<ValType
>(value_);
240 constexpr
bool operator!()
const {
return value_ == 0; }
248 #define INT_TYPE_ASSIGNMENT_OP(op) \
249 ThisType& operator op(const ThisType& arg_value) { \
250 value_ op arg_value.value(); \
253 ThisType& operator op(ValueType arg_value) { \
254 value_ op arg_value; \
264 #undef INT_TYPE_ASSIGNMENT_OP
276 invalid_integer_type_for_id_type_);
282 template <
typename IntTypeName,
typename ValueType>
285 return os << arg.
value();
295 #define INT_TYPE_ARITHMETIC_OP(op) \
296 template <typename IntTypeName, typename ValueType> \
297 constexpr IntType<IntTypeName, ValueType> operator op( \
298 IntType<IntTypeName, ValueType> id_1, \
299 IntType<IntTypeName, ValueType> id_2) { \
300 return IntType<IntTypeName, ValueType>(id_1.value() op id_2.value()); \
302 template <typename IntTypeName, typename ValueType> \
303 constexpr IntType<IntTypeName, ValueType> operator op( \
304 IntType<IntTypeName, ValueType> id, \
305 typename IntType<IntTypeName, ValueType>::ValueType arg_val) { \
306 return IntType<IntTypeName, ValueType>(id.value() op arg_val); \
308 template <typename IntTypeName, typename ValueType> \
309 constexpr IntType<IntTypeName, ValueType> operator op( \
310 typename IntType<IntTypeName, ValueType>::ValueType arg_val, \
311 IntType<IntTypeName, ValueType> id) { \
312 return IntType<IntTypeName, ValueType>(arg_val op id.value()); \
321 #undef INT_TYPE_ARITHMETIC_OP
329 #define INT_TYPE_COMPARISON_OP(op) \
330 template <typename IntTypeName, typename ValueType> \
331 static inline constexpr bool operator op( \
332 IntType<IntTypeName, ValueType> id_1, \
333 IntType<IntTypeName, ValueType> id_2) { \
334 return id_1.value() op id_2.value(); \
336 template <typename IntTypeName, typename ValueType> \
337 static inline constexpr bool operator op( \
338 IntType<IntTypeName, ValueType> id, \
339 typename IntType<IntTypeName, ValueType>::ValueType val) { \
340 return id.value() op val; \
342 template <typename IntTypeName, typename ValueType> \
343 static inline constexpr bool operator op( \
344 typename IntType<IntTypeName, ValueType>::ValueType val, \
345 IntType<IntTypeName, ValueType> id) { \
346 return val op id.value(); \
354 #undef INT_TYPE_COMPARISON_OP
360 template <
typename IntTypeName,
typename ValueType>
361 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