Mercurial > octave-nkf
comparison src/ov.cc @ 7997:2b8952e133c9
implement checked conversions between integers
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 30 Jul 2008 15:20:13 +0200 |
parents | 7ca2735d74c2 |
children | f7f2d867c523 |
comparison
equal
deleted
inserted
replaced
7996:6a7db240b3a3 | 7997:2b8952e133c9 |
---|---|
1344 return retval.reshape (make_vector_dims (retval.dims (), | 1344 return retval.reshape (make_vector_dims (retval.dims (), |
1345 force_vector_conversion, | 1345 force_vector_conversion, |
1346 type_name (), "real vector")); | 1346 type_name (), "real vector")); |
1347 } | 1347 } |
1348 | 1348 |
1349 template <class T> | |
1350 static Array<int> | |
1351 convert_to_int_array (const Array<octave_int<T> >& A) | |
1352 { | |
1353 Array<int> O (A.dims ()); | |
1354 octave_idx_type n = A.numel (); | |
1355 | |
1356 octave_int<int>::clear_trunc_flag (); | |
1357 for (octave_idx_type i = 0; i < n; i++) | |
1358 O.xelem (i) = octave_int<int> (A.xelem (i)); | |
1359 if (octave_int<int>::get_trunc_flag ()) | |
1360 { | |
1361 gripe_truncated_conversion (octave_int<T>::type_name (), "int"); | |
1362 octave_int<int>::clear_trunc_flag (); | |
1363 } | |
1364 | |
1365 return O; | |
1366 } | |
1367 | |
1349 Array<int> | 1368 Array<int> |
1350 octave_value::int_vector_value (bool force_string_conv, bool require_int, | 1369 octave_value::int_vector_value (bool force_string_conv, bool require_int, |
1351 bool force_vector_conversion) const | 1370 bool force_vector_conversion) const |
1352 { | 1371 { |
1353 Array<int> retval; | 1372 Array<int> retval; |
1354 | 1373 |
1355 if (is_integer_type ()) | 1374 if (is_integer_type ()) |
1356 { | 1375 { |
1357 // query for the first type that is wide enough | 1376 if (is_int32_type ()) |
1358 #if SIZEOF_INT == 2 | 1377 retval = convert_to_int_array (int32_array_value ()); |
1359 retval = int16_array_value (); | 1378 else if (is_int64_type ()) |
1360 #elif SIZEOF_INT == 4 | 1379 retval = convert_to_int_array (int64_array_value ()); |
1361 retval = int32_array_value (); | 1380 else if (is_int16_type ()) |
1362 #else | 1381 retval = convert_to_int_array (int16_array_value ()); |
1363 retval = int64_array_value (); | 1382 else if (is_int8_type ()) |
1364 #endif | 1383 retval = convert_to_int_array (int8_array_value ()); |
1384 else if (is_uint32_type ()) | |
1385 retval = convert_to_int_array (uint32_array_value ()); | |
1386 else if (is_uint64_type ()) | |
1387 retval = convert_to_int_array (uint64_array_value ()); | |
1388 else if (is_uint16_type ()) | |
1389 retval = convert_to_int_array (uint16_array_value ()); | |
1390 else if (is_uint8_type ()) | |
1391 retval = convert_to_int_array (uint8_array_value ()); | |
1392 else | |
1393 retval = array_value (force_string_conv); | |
1365 } | 1394 } |
1366 else | 1395 else |
1367 { | 1396 { |
1368 const NDArray a = array_value (force_string_conv); | 1397 const NDArray a = array_value (force_string_conv); |
1369 if (! error_state) | 1398 if (! error_state) |