File: converter.qbk

package info (click to toggle)
boost1.35 1.35.0-5
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 203,856 kB
  • ctags: 337,867
  • sloc: cpp: 938,683; xml: 56,847; ansic: 41,589; python: 18,999; sh: 11,566; makefile: 664; perl: 494; yacc: 456; asm: 353; csh: 6
file content (297 lines) | stat: -rw-r--r-- 10,243 bytes parent folder | download
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
[/
    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)
]

[#numeric_conversion_converter]

[section converter<> function object]

[section Synopsis]

    namespace boost { namespace numeric {


        template<class T,
                 class S,
                 class Traits,          = conversion_traits<T,S>
                 class OverflowHandler  = def_overflow_handler,
                 class Float2IntRounder = Trunc< typename Traits::source_type >,
                 class RawConverter     = raw_converter<Traits>,
                 class UserRangeChecker = UseInternalRangeChecker
                >
        struct converter
        {
            typedef Traits traits ;

            typedef typename Traits::source_type   source_type   ;
            typedef typename Traits::argument_type argument_type ;
            typedef typename Traits::result_type   result_type   ;

            static result_type convert ( argument_type s ) ;

            result_type operator() ( argument_type s ) const ;

            // Internal member functions:

            static range_check_result out_of_range      ( argument_type s ) ;
            static void               validate_range    ( argument_type s ) ;
            static result_type        low_level_convert ( argument_type s ) ;
            static source_type        nearbyint         ( argument_type s ) ;

        } ;

    } } // namespace numeric, boost


`boost::numeric::converter<>` is a __SGI_UNARY_FUNCTION__ encapsulating
the code to perform a numeric conversion with the direction and
properties specified by the Traits template parameter. It can optionally
take some [link numeric_coversion_converter_policies policies] which can be used to customize its behavior. The
`Traits` parameter is not a policy but the parameter that defines
the conversion.

[endsect]

[section Template parameters]

[table
[[            ][           ]]
[[`T`][
The [link numeric_conversion_definitions_numeric_types Numeric Type] 
which is the ['Target] of the conversion.
]]
[[`S`][
The [link numeric_conversion_definitions_numeric_types Numeric Type] 
which is the ['Source] of the conversion.
]]
[[`Traits`][
This must be a conversion traits class with the interface of
[link numeric_conversion_traits `boost::numeric::conversion_traits`]
]]
[[`OverflowHandler`][
[*Stateless Policy] called to administrate the result of the range checking.

It is a [*Function Object] which receives the result of `out_of_range()`
and is called inside the `validate_range()` static member function exposed
by the converter.
]]
[[`Float2IntRounder`][
[*Stateless Policy] which specifies the rounding mode used for float to
integral conversions.

It supplies the `nearbyint()` static member function exposed by the converter.
]]
[[`RawConverter`][
[*Stateless Policy] which is used to perform the actual conversion.

It supplies the `low_level_convert()` static member function exposed
by the converter.
]]
[[`UserRangeChecker`][
['Special and Optional] [*Stateless Policy] which can be used to override
the internal range checking logic.

If given, supplies alternative code for the `out_of_range()` and
`validate_range()` static member functions exposed by the converter.
]]
]

[endsect]

[section Member functions]

[: `static result_type converter<>::convert ( argument_type s ) ; // throw
`]

This static member function converts an rvalue of type `source_type` to
an rvalue of type `target_type`.

If the conversion requires it, it performs a range checking before the conversion
and passes the result of the check to the overflow handler policy (the default
policy throws an exception if out-of-range is detected)

The implementation of this function is actually built from the policies and is
basically as follows:

    result_type converter<>::convert ( argument_type s )
    {
        validate_range(s); // Implemented by the internal range checking logic
                           // (which also calls the OverflowHandler policy)
                           // or externally supplied by the UserRangeChecker policy.

        s = nearbyint(s); // Externally supplied by the Float2IntRounder policy.
                          // NOTE: This is actually called only for float to int conversions.

        return low_level_convert(s); // Externally supplied by the RawConverter policy.
    }

`converter<>::operator() const` just calls `convert()`

__SPACE__

[: `static range_check_result numeric_converter<>::out_of_range ( argument_type s ) ;`]

This [link numeric_conversion_converter_internal internal] static member function 
determines if the value `s` can be
represented by the target type without overflow.

It does not determine if the conversion is ['exact]; that is, it does not detect
['inexact] conversions, only ['out-of-range] conversions (see the
[link numeric_conversion_definitions_roundoff Definitions] for further details).

The return value is of enum type 
[link numeric_conversion_converter_policies_range_check_result `boost::numeric::range_check_result`]

The actual code for the range checking logic is optimized for the combined
properties of the source and target types. For example, a non-subranged
conversion (i.e: `int`->`float`), requires no range checking, so `out_of_range()`
returns `cInRange` directly. See the following 
[link numeric_conversion_converter_range_checking_logic table] for more details.

If the user supplied a
[link numeric_conversion_policy_user_range_checker UserRangeChecker] policy, 
is this policy which implements this function, so the implementation is user 
defined, although it is expected to perform the same conceptual check and 
return the appropriate result.

__SPACE__

[: `static void numeric_converter<>::validate_range ( argument_type s ) ; // no throw
`]

This [link numeric_conversion_converter_internal internal] static member function 
calls out_of_range(s), and passes the
result to the [link numeric_conversion_policy_overflow_handler OverflowHandler] 
policy class.

For those Target/Source combinations which don't require range checking, this
is an empty inline function.

If the user supplied a 
[link numeric_conversion_policy_user_range_checker UserRangeChecker] policy, 
is this policy which implements this function, so the implementation is user 
defined, although it is expected to perform the same action as the default. 
In particular, it is expected to pass the result of the check to the overflow handler.

__SPACE__

[: `static result_type numeric_converter<>::low_level_convert ( argument_type s ) ;` ]

This [link numeric_conversion_converter_internal internal] static member function 
performs the actual conversion.

This function is externally supplied by the 
[link numeric_conversion_policy_raw_converter RawConverter] policy class.

__SPACE__

[: `static source_type converter<>::nearbyint ( argument_type s ) ;`]

This [link numeric_conversion_converter_internal internal] static member function, 
which is [_only used] for
`float` to `int` conversions, returns an ['integer] value of ['[_floating-point
type]] according to some rounding direction.

This function is externally supplied by the 
[link numeric_conversion_policy_float_to_int_rounder Float2IntRounder] policy class
which encapsulates the specific rounding mode.

__SPACE__

[#numeric_conversion_converter_internal]

[heading Internal Member Functions]

These static member functions build the actual conversion code used by `convert()`.
The user does not have to call these if calling `convert()`, since `convert()` calls
them infernally, but they can be called separately for specific needs.

[endsect]

[#numeric_conversion_converter_range_checking_logic]

[section Range Checking Logic]

The following table summarizes the internal range checking logic performed for
each combination of the properties of Source and Target.

LowestT/HighestT denotes the highest and lowest values of the Target type, respectively.

`S(n)` is short for `static_cast<S>(n)` (`S` denotes the Source type).

`NONE` indicates that for this case there is no range checking.

[pre
[^
int_to_int    |--> sig_to_sig     |--> subranged     |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) )
              |                   |--> not subranged |--> NONE
              |
              |--> unsig_to_unsig |--> subranged     |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) )
              |                   |--> not subranged |--> NONE
              |
              |--> sig_to_unsig   |--> pos subranged     |--> ( s >= S(0) ) && ( s <= S(HighestT) )
              |                   |--> not pos subranged |--> ( s >= S(0) )
              |
              |--> unsig_to_sig   |--> subranged     |--> ( s <= S(HighestT) )
              |                   |--> not subranged |--> NONE
]
[^
int_to_float   |--> NONE
]
[^
float_to_int   |--> round_to_zero         |--> ( s >  S(LowestT)-S(1)   ) && ( s <  S(HighestT)+S(1)   )
               |--> round_to_even_nearest |--> ( s >= S(LowestT)-S(0.5) ) && ( s <  S(HighestT)+S(0.5) )
               |--> round_to_infinity     |--> ( s >  S(LowestT)-S(1)   ) && ( s <= S(HighestT)        )
               |--> round_to_neg_infinity |--> ( s >= S(LowestT)        ) && ( s <  S(HighestT)+S(1)   )
]
[^
float_to_float |--> subranged     |--> ( s >= S(LowestT) ) && ( s <= S(HighestT) )
               |--> not subranged |--> NONE
]
]



[endsect]

[section Examples]

    #include <cassert>
    #include <boost/numeric/conversion/converter.hpp>

    int main() {

        typedef boost::numeric::converter<int,double> Double2Int ;

        int x = Double2Int::convert(2.0);
        assert ( x == 2 );

        int y = Double2Int()(3.14); // As a function object.
        assert ( y == 3 ) ; // The default rounding is trunc.

        try
        {
            double m = boost::numeric::bounds<double>::highest();
            int z = Double2Int::convert(m); // By default throws positive_overflow()
        }
        catch ( boost::numeric::positive_overflow const& )
        {
        }

        return 0;
    }

[endsect]

[endsect]