LibreOffice Module o3tl (master) 1
strong_int.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#ifndef INCLUDED_O3TL_STRONG_INT_HXX
21#define INCLUDED_O3TL_STRONG_INT_HXX
22
23#include <sal/config.h>
24#include <limits>
25#include <cassert>
26#include <type_traits>
27
28namespace o3tl
29{
30
31#if !defined __COVERITY__
32
33namespace detail {
34
35template<typename T1, typename T2> constexpr
36typename std::enable_if<
37 std::is_signed<T1>::value && std::is_signed<T2>::value, bool>::type
38isInRange(T2 value) {
39 return value >= std::numeric_limits<T1>::min()
40 && value <= std::numeric_limits<T1>::max();
41}
42
43template<typename T1, typename T2> constexpr
44typename std::enable_if<
45 std::is_signed<T1>::value && std::is_unsigned<T2>::value, bool>::type
46isInRange(T2 value) {
47 return value
48 <= static_cast<typename std::make_unsigned<T1>::type>(
49 std::numeric_limits<T1>::max());
50}
51
52template<typename T1, typename T2> constexpr
53typename std::enable_if<
54 std::is_unsigned<T1>::value && std::is_signed<T2>::value, bool>::type
55isInRange(T2 value) {
56 return value >= 0
57 && (static_cast<typename std::make_unsigned<T2>::type>(value)
58 <= std::numeric_limits<T1>::max());
59}
60
61template<typename T1, typename T2> constexpr
62typename std::enable_if<
63 std::is_unsigned<T1>::value && std::is_unsigned<T2>::value, bool>::type
64isInRange(T2 value) {
65 return value <= std::numeric_limits<T1>::max();
66}
67
68}
69
70#endif
71
82template <typename UNDERLYING_TYPE, typename PHANTOM_TYPE>
84{
85public:
86 template<typename T> explicit constexpr strong_int(
87 T value,
88 typename std::enable_if<std::is_integral<T>::value, int>::type = 0):
90 {
91#if !defined __COVERITY__
92 // catch attempts to pass in out-of-range values early
93 assert(detail::isInRange<UNDERLYING_TYPE>(value)
94 && "out of range");
95#endif
96 }
98
99 explicit constexpr operator UNDERLYING_TYPE() const { return m_value; }
100 explicit operator bool() const { return m_value != 0; }
101 UNDERLYING_TYPE get() const { return m_value; }
102
103 bool operator<(strong_int const & other) const { return m_value < other.m_value; }
104 bool operator<=(strong_int const & other) const { return m_value <= other.m_value; }
105 bool operator>(strong_int const & other) const { return m_value > other.m_value; }
106 bool operator>=(strong_int const & other) const { return m_value >= other.m_value; }
107 bool operator==(strong_int const & other) const { return m_value == other.m_value; }
108 bool operator!=(strong_int const & other) const { return m_value != other.m_value; }
109 strong_int& operator++() { ++m_value; return *this; }
110 strong_int operator++(int) { UNDERLYING_TYPE nOldValue = m_value; ++m_value; return strong_int(nOldValue); }
111 strong_int& operator--() { --m_value; return *this; }
112 strong_int operator--(int) { UNDERLYING_TYPE nOldValue = m_value; --m_value; return strong_int(nOldValue); }
113 strong_int& operator+=(strong_int const & other) { m_value += other.m_value; return *this; }
114 strong_int& operator-=(strong_int const & other) { m_value -= other.m_value; return *this; }
115 strong_int& operator%=(strong_int const & other) { m_value %= other.m_value; return *this; }
116 strong_int& operator*=(strong_int const & other) { m_value *= other.m_value; return *this; }
117 strong_int& operator/=(strong_int const & other) { m_value /= other.m_value; return *this; }
118 [[nodiscard]]
119 strong_int operator%(strong_int const & other) const { return strong_int(m_value % other.m_value); }
120 [[nodiscard]]
122 [[nodiscard]]
123 strong_int operator*(strong_int const & other) const { return strong_int(m_value * other.m_value); }
124 [[nodiscard]]
125 strong_int operator/(strong_int const & other) const { return strong_int(m_value / other.m_value); }
126
127 bool anyOf(strong_int v) const {
128 return *this == v;
129 }
130
131 template<typename... Args>
132 bool anyOf(strong_int first, Args... args) const {
133 return *this == first || anyOf(args...);
134 }
135
136private:
137 UNDERLYING_TYPE m_value;
138};
139
140template <typename UT, typename PT>
142{
143 return strong_int<UT,PT>(lhs.get() + rhs.get());
144}
145
146template <typename UT, typename PT>
148{
149 return strong_int<UT,PT>(lhs.get() - rhs.get());
150}
151
152}; // namespace o3tl
153
154#endif /* INCLUDED_O3TL_STRONG_INT_HXX */
155
156/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Any value
float v
constexpr OUStringLiteral first
constexpr std::enable_if< std::is_signed< T1 >::value &&std::is_signed< T2 >::value, bool >::type isInRange(T2 value)
Definition: strong_int.hxx:38
strong_int< UT, PT > operator-(strong_int< UT, PT > const &lhs, strong_int< UT, PT > const &rhs)
Definition: strong_int.hxx:147
strong_int< UT, PT > operator+(strong_int< UT, PT > const &lhs, strong_int< UT, PT > const &rhs)
Definition: strong_int.hxx:141
args
Wrap up an integer type so that we prevent accidental conversion to other integer types.
Definition: strong_int.hxx:84
bool anyOf(strong_int first, Args... args) const
Definition: strong_int.hxx:132
bool operator==(strong_int const &other) const
Definition: strong_int.hxx:107
strong_int operator/(strong_int const &other) const
Definition: strong_int.hxx:125
strong_int & operator--()
Definition: strong_int.hxx:111
strong_int operator*(strong_int const &other) const
Definition: strong_int.hxx:123
strong_int & operator+=(strong_int const &other)
Definition: strong_int.hxx:113
strong_int operator%(strong_int const &other) const
Definition: strong_int.hxx:119
bool operator>=(strong_int const &other) const
Definition: strong_int.hxx:106
strong_int & operator/=(strong_int const &other)
Definition: strong_int.hxx:117
UNDERLYING_TYPE get() const
Definition: strong_int.hxx:101
bool operator!=(strong_int const &other) const
Definition: strong_int.hxx:108
bool operator<(strong_int const &other) const
Definition: strong_int.hxx:103
constexpr strong_int(T value, typename std::enable_if< std::is_integral< T >::value, int >::type=0)
Definition: strong_int.hxx:86
strong_int & operator%=(strong_int const &other)
Definition: strong_int.hxx:115
strong_int operator-() const
Definition: strong_int.hxx:121
strong_int & operator++()
Definition: strong_int.hxx:109
strong_int & operator*=(strong_int const &other)
Definition: strong_int.hxx:116
bool operator>(strong_int const &other) const
Definition: strong_int.hxx:105
bool anyOf(strong_int v) const
Definition: strong_int.hxx:127
strong_int operator--(int)
Definition: strong_int.hxx:112
strong_int operator++(int)
Definition: strong_int.hxx:110
UNDERLYING_TYPE m_value
Definition: strong_int.hxx:137
strong_int & operator-=(strong_int const &other)
Definition: strong_int.hxx:114
bool operator<=(strong_int const &other) const
Definition: strong_int.hxx:104