Library of Assembled Shared Sources
num_cast.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2025 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_NUM_CAST_H
44#define LASS_GUARDIAN_OF_INCLUSION_NUM_NUM_CAST_H
45
46#include "num_common.h"
47
48namespace lass
49{
50namespace num
51{
52
53class BadNumCast: public util::ExceptionMixin<BadNumCast>
54{
55public:
56 BadNumCast(std::string msg, std::string loc): util::ExceptionMixin<BadNumCast>(std::move(msg), std::move(loc)) {}
57 ~BadNumCast() noexcept {}
58};
59
60namespace impl
61{
62
63template <typename Out, typename In, bool signedOut, bool signedIn> struct NumCaster;
64
65template <typename Out, typename In>
66struct NumCaster<Out, In, true, true>
67{
68 // Out is signed, In is signed
69
70 static Out cast(In in)
71 {
72 static_assert(num::NumTraits<In>::isSigned == true);
73 static_assert(num::NumTraits<Out>::isSigned == true);
74
75 using TCommon = std::common_type_t<In, Out, int>;
76 static_assert(num::NumTraits<TCommon>::isSigned == true);
77
78 constexpr TCommon min = num::NumTraits<Out>::min;
79 constexpr TCommon max = num::NumTraits<Out>::max;
80 if (static_cast<TCommon>(in) < min)
81 {
82 LASS_THROW_EX(BadNumCast, "not a " << num::NumTraits<Out>::name()
83 << ": underflow: " << static_cast<TCommon>(in) << " < " << min);
84 }
85 if (static_cast<TCommon>(in) > max)
86 {
87 LASS_THROW_EX(BadNumCast, "not a " << num::NumTraits<Out>::name()
88 << ": overflow: " << static_cast<TCommon>(in) << " > " << max);
89 }
90 return static_cast<Out>(in);
91 }
92};
93
94template <typename Out, typename In>
95struct NumCaster<Out, In, false, true>
96{
97 // Out is unsigned, In is signed
98
99 static Out cast(In in)
100 {
101 static_assert(num::NumTraits<In>::isSigned == true);
102 static_assert(num::NumTraits<Out>::isSigned == false);
103 using TUnsignedIn = typename num::NumTraits<In>::unsignedType;
104
105 using TCommon = std::common_type_t<TUnsignedIn, Out, unsigned>;
106 static_assert(num::NumTraits<TCommon>::isSigned == false);
107
108 constexpr TCommon max = num::NumTraits<Out>::max;
109 if (in < 0)
110 {
111 using TSigned = std::common_type_t<In, int>;
112 LASS_THROW_EX(BadNumCast, "not a " << num::NumTraits<Out>::name()
113 << ": negative: " << static_cast<TSigned>(in));
114 }
115 if (static_cast<TCommon>(in) > max)
116 {
117 LASS_THROW_EX(BadNumCast, "not a " << num::NumTraits<Out>::name()
118 << ": overflow: " << static_cast<TCommon>(in) << " > " << max);
119 }
120 return static_cast<Out>(in);
121 }
122};
123
124template <typename Out, typename In, bool signedOut>
125struct NumCaster<Out, In, signedOut, false>
126{
127 // In is unsigned, Out is signed or unsigned
128
129 static Out cast(In in)
130 {
131 static_assert(num::NumTraits<In>::isSigned == false);
132 using TUnsignedOut = typename num::NumTraits<Out>::unsignedType;
133
134 using TCommon = std::common_type_t<In, TUnsignedOut, unsigned>;
135 static_assert(num::NumTraits<TCommon>::isSigned == false);
136
137 constexpr TCommon max = num::NumTraits<Out>::max;
138 if (static_cast<TCommon>(in) > max)
139 {
140 LASS_THROW_EX(BadNumCast, "not a " << num::NumTraits<Out>::name()
141 << ": overflow: " << static_cast<TCommon>(in) << " > " << max);
142 }
143 return static_cast<Out>(in);
144 }
145};
146
147template <typename Out>
148struct NumCaster<Out, Out, true, true>
149{
150 static Out cast(Out in) { return in; }
151};
152
153template <typename Out>
154struct NumCaster<Out, Out, false, false>
155{
156 static Out cast(Out in) { return in; }
157};
158
159}
160
161template <typename Out, typename In>
162Out numCast(In in)
163{
164 return impl::NumCaster<Out, In, NumTraits<Out>::isSigned, NumTraits<In>::isSigned>::cast(in);
165}
166
167}
168}
169
170#endif
171
172// EOF
numeric types and traits.
Definition basic_ops.h:70
ColorRGBA in(const ColorRGBA &a, const ColorRGBA &b)
part of a inside b.
Library for Assembled Shared Sources.
Definition config.h:53