File: converter_policies.qbk

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (311 lines) | stat: -rw-r--r-- 9,448 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
[/
    Boost.Optional

    Copyright (c) 2003-2007 Fernando Luis Cacciola Carballal

    Distributed under the Boost Software License, Version 1.0.
    (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
]

[section Numeric Converter Policy Classes]


[section enum range_check_result]

    namespace boost { namespace numeric {

        enum range_check_result
        {
            cInRange     ,
            cNegOverflow ,
            cPosOverflow
        } ;

    } }

Defines the values returned by `boost::numeric::converter<>::out_of_range()`

[endsect]

[section Policy OverflowHandler]

This ['stateless] non-template policy class must be a ['function object] and is
called to administrate the result of the range checking. It can throw an exception
if overflow has been detected by the range checking as indicated by its argument.
If it throws, is is recommended that it be `std::bad_cast` or derived.

It must have the following interface (it does not has to be a template class):

    struct YourOverflowHandlerPolicy
    {
        void operator() ( boost::range_check_result ) ; // throw bad_cast or derived
    } ;

It is called with the result of the converter's `out_of_range()` inside `validate_range()`.

These are the two overflow handler classes provided by the library:

    namespace boost { namespace numeric {

        struct def_overflow_handler
        {
            void operator() ( range_check_result r ) // throw bad_numeric_conversion derived
            {
                if ( r == cNegOverflow )
                    throw negative_overflow() ;
                else if ( r == cPosOverflow )
                    throw positive_overflow() ;
            }
        } ;

        struct silent_overflow_handler
        {
            void operator() ( range_check_result ) // no-throw
            {}
        } ;

    } }

And these are the Exception Classes thrown by the default overflow handler
[link numeric_conversion_policy_overflow_handler_important_note (see IMPORTANT note)]

    namespace boost { namespace numeric {

        ``[#numeric_conversion_bad_numeric_cast]``
        class bad_numeric_cast : public std::bad_cast
        {
            public:
            virtual const char *what() const // throw()
            {
                return "bad numeric conversion: overflow";
            }

        };

        ``[#numeric_conversion_negative_overflow]``
        class negative_overflow : public bad_numeric_cast
        {
            public:
            virtual const char *what() const // throw()
            {
                return "bad numeric conversion: negative overflow";
            }
        };

        ``[#numeric_conversion_possitive_overflow]``
        class positive_overflow : public bad_numeric_cast
        {
            public:
            virtual const char *what() const // throw()
            {
                return "bad numeric conversion: positive overflow";
            }
        };


    } }

[#numeric_conversion_policy_overflow_handler_important_note]

[important [*RELEASE NOTE for 1.33]
Previous to boost version 1.33, the exception class `bad_numeric_cast` was
named `bad_numeric_conversion`. However, in 1.33, the old function
`numeric_cast<>` from `boost/cast.hpp` was completly replaced by the
new `numeric_cast<>` in `boost/numeric/conversion/cast.hpp`
(and `boost/cast.hpp` is including `boost/numeric/conversion/cast.hpp` now).
That old function which existed in boost for quite some time used the
`bad_numeric_cast` as its exception type so I decided to avoid backward
compatibility problems by adopting it (guessing that the user base for
the old code is wider than for the new code).
]

[endsect]

[section Policy Float2IntRounder]

This ['stateless] template policy class specifies the rounding mode used
for [_float to integral] conversions. It supplies the `nearbyint()`
static member function exposed by the converter, which means that it
[_publicly inherits from this policy].

The policy must have the following interface:

    template<class S>
    struct YourFloat2IntRounderPolicy
    {
        typedef S               source_type ;
        typedef {S or S const&} argument_type ;

        static source_type nearbyint ( argument_type s ) { ... }

        typedef mpl::integral_c<std::float_round_style,std::round_...> round_style ;

    } ;

These are the rounder classes provided by the library (only the specific parts are shown,
see the general policy form above)

[note
These classes are not intended to be general purpose rounding functions
but specific policies for `converter<>`. This is why they are not function objects.
]

    namespace boost { namespace numeric {


        template<class S>
        struct Trunc
        {
            static source_type nearbyint ( argument_type s )
            {
                using std::floor ;
                using std::ceil  ;

                return s >= static_cast<S>(0) ? floor(s) : ceil(s) ;
            }

            typedef mpl::integral_c<std::float_round_style,std::round_toward_zero> round_style ;
        } ;


        template<class S>
        struct RoundEven
        {
            static source_type nearbyint ( argument_type s )
            {
                return impl-defined-value ;
            }

            typedef mpl::integral_c<std::float_round_style,std::round_to_nearest> round_style ;
        } ;


        template<class S>
        struct Ceil
        {
            static source_type nearbyint ( argument_type s )
            {
                using std::ceil ;
                return ceil(s) ;
            }

            typedef mpl::integral_c<std::float_round_style,std::round_toward_infinity> round_style ;
        } ;


        template<class S>
        struct Floor
        {
            static source_type nearbyint ( argument_type s )
            {
                using std::floor ;
                return floor(s) ;
            }
            typedef mpl::integral_c<std::float_round_style,std::round_toward_neg_infinity> round_style ;
        } ;

    } } // namespace numeric, namespace boost

[heading Math Functions used by the rounder policies]

The rounder policies supplied by this header use math functions `floor()` and `ceil()`.
The standard versions of these functions are introduced in context by a using directive,
so in normal conditions, the standard functions will be used.

However, if there are other visible corresponding overloads an ambiguity could arise.
In this case, the user can supply her own rounder policy which could, for instance,
use a fully qualified call.

This technique allows the default rounder policies to be used directly with
user defined types. The user only requires that suitable overloads of `floor()` and `ceil()`
be visible. See also [link boost_numericconversion.type_requirements_and_user_defined_types_support User Defined Numeric Types]
support.

[endsect]

[section Policy RawConverter]

This ['stateless] template policy class is used to perform the 
actual conversion from Source to Target. It supplies the 
`low_level_convert()` static member function exposed by the 
converter, which means that it publicly inherits from this policy.

The policy must have the following interface:

    template<class Traits>
    struct YourRawConverterPolicy
    {
        typedef typename Traits::result_type   result_type   ;
        typedef typename Traits::argument_type argument_type ;

        static result_type low_level_convert ( argument_type s ) { return <impl defined> ; }
    } ;


This policy is mostly provided as a hook for user defined types which don't support `static_cast<>` conversions to some types

This is the only raw converter policy class provided by the library:

    namespace boost { namespace numeric {

        template<class Traits>
        struct raw_numeric_converter
        {
            typedef typename Traits::result_type   result_type   ;
            typedef typename Traits::argument_type argument_type ;

            static result_type low_level_convert ( argument_type s )
            {
                return static_cast<result_type>(s) ; 
            }
        } ;

    } }

[endsect]

[section Policy UserRangeChecker]

This ['stateless] template policy class is used [_only if supplied] 
to [*override] the internal range checking logic.

It supplies the `validate_range()` static member function 
exposed by the converter, which means that it publicly inherits 
from this policy.

The policy must have the following interface:

    template<class Traits>
    struct YourRangeCheckerPolicy
    {
        typedef typename Traits::argument_type argument_type ;

        // Determines if the value 's' fits in the range of the Target type.
        static range_check_result out_of_range ( argument_type s ) ;

        // Checks whether the value 's' is out_of_range()
        // and passes the result of the check to the OverflowHandler policy.
        static void validate_range ( argument_type s )
        {
            OverflowHandler()( out_of_range(s) ) ;
        }
    } ;


This policy is [*only] provided as a hook for user defined types which require
range checking (which is disabled by default when a UDT is involved).
The library provides a class: `UseInternalRangeChecker{}`; which is a ['fake]
`RangeChecker` policy used to signal the converter to use its internal
range checking implementation.

[endsect]

[endsect]