tdf#143148 Use pragma once instead of include guards
[LibreOffice.git] / include / o3tl / strong_int.hxx
blobc0df5f75efc82c9c9b4b4cc46a6af265990485ab
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_O3TL_STRONG_INT_HXX
21 #define INCLUDED_O3TL_STRONG_INT_HXX
23 #include <sal/config.h>
24 #include <limits>
25 #include <cassert>
26 #include <type_traits>
27 #include <compare>
28 #include <config_options.h>
30 namespace o3tl
33 #if !defined __COVERITY__
35 namespace detail {
37 template<typename T1, typename T2> constexpr
38 typename std::enable_if<
39 std::is_signed<T1>::value && std::is_signed<T2>::value, bool>::type
40 isInRange(T2 value) {
41 return value >= std::numeric_limits<T1>::min()
42 && value <= std::numeric_limits<T1>::max();
45 template<typename T1, typename T2> constexpr
46 typename std::enable_if<
47 std::is_signed<T1>::value && std::is_unsigned<T2>::value, bool>::type
48 isInRange(T2 value) {
49 return value
50 <= static_cast<typename std::make_unsigned<T1>::type>(
51 std::numeric_limits<T1>::max());
54 template<typename T1, typename T2> constexpr
55 typename std::enable_if<
56 std::is_unsigned<T1>::value && std::is_signed<T2>::value, bool>::type
57 isInRange(T2 value) {
58 return value >= 0
59 && (static_cast<typename std::make_unsigned<T2>::type>(value)
60 <= std::numeric_limits<T1>::max());
63 template<typename T1, typename T2> constexpr
64 typename std::enable_if<
65 std::is_unsigned<T1>::value && std::is_unsigned<T2>::value, bool>::type
66 isInRange(T2 value) {
67 return value <= std::numeric_limits<T1>::max();
72 #endif
74 ///
75 /// Wrap up an integer type so that we prevent accidental conversion to other integer types.
76 ///
77 /// e.g.
78 /// typedef o3tl::strong_int<unsigned, struct MyIntTag> MyInt;
79 ///
80 /// \param UNDERLYING_TYPE the underlying scalar type
81 /// \param PHANTOM_TYPE a type tag, used to distinguish this instantiation of the template
82 /// from other instantiations with the same UNDERLYING_TYPE.
83 ///
84 template <typename UNDERLYING_TYPE, typename PHANTOM_TYPE>
85 struct strong_int
87 public:
88 // when compiling LO on macOS, debug builds will display a linking error where, see
89 // <https://lists.freedesktop.org/archives/libreoffice/2024-February/091564.html>, "Our Clang
90 // --enable-pch setup is known broken":
91 #if defined MACOSX && defined __clang__ && __clang_major__ == 16 && ENABLE_PCH
92 explicit constexpr strong_int(unsigned long long value) : m_value(value) {}
93 explicit constexpr strong_int(unsigned long value) : m_value(value) {}
94 explicit constexpr strong_int(long value) : m_value(value) {}
95 explicit constexpr strong_int(int value) : m_value(value) {}
96 explicit constexpr strong_int(unsigned int value) : m_value(value) {}
97 #else
98 template<typename T> explicit constexpr strong_int(
99 T value,
100 typename std::enable_if<std::is_integral<T>::value, int>::type = 0):
101 m_value(value)
103 #if !defined __COVERITY__
104 // catch attempts to pass in out-of-range values early
105 assert(detail::isInRange<UNDERLYING_TYPE>(value)
106 && "out of range");
107 #endif
109 #endif
110 strong_int() : m_value(0) {}
112 explicit constexpr operator UNDERLYING_TYPE() const { return m_value; }
113 explicit operator bool() const { return m_value != 0; }
114 UNDERLYING_TYPE get() const { return m_value; }
116 auto operator<=>(strong_int const & other) const = default;
118 strong_int& operator++() { ++m_value; return *this; }
119 strong_int operator++(int) { UNDERLYING_TYPE nOldValue = m_value; ++m_value; return strong_int(nOldValue); }
120 strong_int& operator--() { --m_value; return *this; }
121 strong_int operator--(int) { UNDERLYING_TYPE nOldValue = m_value; --m_value; return strong_int(nOldValue); }
122 strong_int& operator+=(strong_int const & other) { m_value += other.m_value; return *this; }
123 strong_int& operator-=(strong_int const & other) { m_value -= other.m_value; return *this; }
124 strong_int& operator%=(strong_int const & other) { m_value %= other.m_value; return *this; }
125 strong_int& operator*=(strong_int const & other) { m_value *= other.m_value; return *this; }
126 strong_int& operator/=(strong_int const & other) { m_value /= other.m_value; return *this; }
127 [[nodiscard]]
128 strong_int operator%(strong_int const & other) const { return strong_int(m_value % other.m_value); }
129 [[nodiscard]]
130 strong_int operator-() const { return strong_int(-m_value); }
131 [[nodiscard]]
132 strong_int operator*(strong_int const & other) const { return strong_int(m_value * other.m_value); }
133 [[nodiscard]]
134 strong_int operator/(strong_int const & other) const { return strong_int(m_value / other.m_value); }
136 bool anyOf(strong_int v) const {
137 return *this == v;
140 template<typename... Args>
141 bool anyOf(strong_int first, Args... args) const {
142 return *this == first || anyOf(args...);
145 private:
146 UNDERLYING_TYPE m_value;
149 template <typename UT, typename PT>
150 strong_int<UT,PT> operator+(strong_int<UT,PT> const & lhs, strong_int<UT,PT> const & rhs)
152 return strong_int<UT,PT>(lhs.get() + rhs.get());
155 template <typename UT, typename PT>
156 strong_int<UT,PT> operator-(strong_int<UT,PT> const & lhs, strong_int<UT,PT> const & rhs)
158 return strong_int<UT,PT>(lhs.get() - rhs.get());
161 }; // namespace o3tl
163 #endif /* INCLUDED_O3TL_STRONG_INT_HXX */
165 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */