145 #ifndef OR_TOOLS_BASE_STRONG_INT_H_
146 #define OR_TOOLS_BASE_STRONG_INT_H_
150 #include <functional>
153 #include <type_traits>
155 #include "absl/base/port.h"
156 #include "absl/strings/string_view.h"
161 template <
typename StrongIntName,
typename _ValueType>
167 #define DEFINE_STRONG_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 ::util_intops::StrongInt<int_type_name##_tag_, value_type> \
184 template <
typename StrongIntName,
typename _ValueType>
191 return StrongIntName::TypeName();
198 return static_cast<size_t>(arg.
value());
218 template <
typename ValType>
220 return static_cast<ValType
>(value_);
243 constexpr
bool operator!()
const {
return value_ == 0; }
251 #define STRONG_INT_TYPE_ASSIGNMENT_OP(op) \
252 ThisType& operator op(const ThisType& arg_value) { \
253 value_ op arg_value.value(); \
256 ThisType& operator op(ValueType arg_value) { \
257 value_ op arg_value; \
267 #undef STRONG_INT_TYPE_ASSIGNMENT_OP
279 invalid_integer_type_for_id_type_);
285 template <
typename StrongIntName,
typename ValueType>
288 return os << arg.
value();
299 #define STRONG_INT_TYPE_ARITHMETIC_OP(op) \
300 template <typename StrongIntName, typename ValueType> \
301 constexpr StrongInt<StrongIntName, ValueType> operator op( \
302 StrongInt<StrongIntName, ValueType> id_1, \
303 StrongInt<StrongIntName, ValueType> id_2) { \
304 return StrongInt<StrongIntName, ValueType>(id_1.value() op id_2.value()); \
306 template <typename StrongIntName, typename ValueType> \
307 constexpr StrongInt<StrongIntName, ValueType> operator op( \
308 StrongInt<StrongIntName, ValueType> id, \
309 typename StrongInt<StrongIntName, ValueType>::ValueType arg_val) { \
310 return StrongInt<StrongIntName, ValueType>(id.value() op arg_val); \
312 template <typename StrongIntName, typename ValueType> \
313 constexpr StrongInt<StrongIntName, ValueType> operator op( \
314 typename StrongInt<StrongIntName, ValueType>::ValueType arg_val, \
315 StrongInt<StrongIntName, ValueType> id) { \
316 return StrongInt<StrongIntName, ValueType>(arg_val op id.value()); \
325 #undef STRONG_INT_TYPE_ARITHMETIC_OP
333 #define STRONG_INT_TYPE_COMPARISON_OP(op) \
334 template <typename StrongIntName, typename ValueType> \
335 static inline constexpr bool operator op( \
336 StrongInt<StrongIntName, ValueType> id_1, \
337 StrongInt<StrongIntName, ValueType> id_2) { \
338 return id_1.value() op id_2.value(); \
340 template <typename StrongIntName, typename ValueType> \
341 static inline constexpr bool operator op( \
342 StrongInt<StrongIntName, ValueType> id, \
343 typename StrongInt<StrongIntName, ValueType>::ValueType val) { \
344 return id.value() op val; \
346 template <typename StrongIntName, typename ValueType> \
347 static inline constexpr bool operator op( \
348 typename StrongInt<StrongIntName, ValueType>::ValueType val, \
349 StrongInt<StrongIntName, ValueType> id) { \
350 return val op id.value(); \
358 #undef STRONG_INT_TYPE_COMPARISON_OP
362 template <
typename IntType>
376 return current_ != other.current_;
379 return current_ == other.current_;
405 const StrongIntRangeIterator begin_;
406 const StrongIntRangeIterator end_;
409 template <
typename IntType>
414 template <
typename IntType>
422 template <
typename StrongIntName,
typename ValueType>
STRONG_INT_TYPE_ASSIGNMENT_OP(%=)
STRONG_INT_TYPE_ASSIGNMENT_OP(>>=)
STRONG_INT_TYPE_ASSIGNMENT_OP(-=)
STRONG_INT_TYPE_ASSIGNMENT_OP(+=)
const ThisType operator--(int v)
constexpr const ThisType operator+() const
constexpr const ThisType operator~() const
const ThisType operator++(int v)
constexpr const ThisType operator-() const
constexpr ValType value() const
constexpr bool operator!() const
STRONG_INT_TYPE_ASSIGNMENT_OP * STRONG_INT_TYPE_ASSIGNMENT_OP(/=);STRONG_INT_TYPE_ASSIGNMENT_OP(<<=
StrongInt< StrongIntName, ValueType > ThisType
ThisType & operator=(ValueType arg_value)
constexpr StrongInt(ValueType value)
constexpr ValueType value() const
static constexpr absl::string_view TypeName()
StrongIntRangeIterator operator++(int)
bool operator!=(const StrongIntRangeIterator &other) const
StrongIntRangeIterator & operator++()
bool operator==(const StrongIntRangeIterator &other) const
StrongIntRangeIterator(IntType initial)
std::input_iterator_tag iterator_category
value_type operator*() const
const IntType & reference
pointer operator->() const
StrongIntRangeIterator begin() const
StrongIntRangeIterator end() const
StrongIntRange(IntType end)
StrongIntRange(IntType begin, IntType end)
STRONG_INT_TYPE_ARITHMETIC_OP(+)
class util_intops::StrongInt ABSL_ATTRIBUTE_PACKED
STRONG_INT_TYPE_COMPARISON_OP(==)
std::ostream & operator<<(std::ostream &os, StrongInt< StrongIntName, ValueType > arg)
StrongIntRange< IntType > MakeStrongIntRange(IntType end)
std::optional< int64_t > end
size_t operator()(const StrongInt &arg) const