[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Branching on builtin_type ()
From: |
Jordi Gutiérrez Hermoso |
Subject: |
Branching on builtin_type () |
Date: |
Mon, 5 Mar 2012 14:23:30 -0500 |
While reading Jaroslav's code, I frequently come across constructs
like this one:
switch (argx.builtin_type ())
{
case btyp_double:
retval = argx.array_value ().nth_element (n, dim);
break;
case btyp_float:
retval = argx.float_array_value ().nth_element (n, dim);
break;
case btyp_complex:
retval = argx.complex_array_value ().nth_element (n, dim);
break;
case btyp_float_complex:
retval = argx.float_complex_array_value ().nth_element (n, dim);
break;
#define MAKE_INT_BRANCH(X) \
case btyp_ ## X: \
retval = argx.X ## _array_value ().nth_element (n, dim); \
break
MAKE_INT_BRANCH (int8);
MAKE_INT_BRANCH (int16);
MAKE_INT_BRANCH (int32);
MAKE_INT_BRANCH (int64);
MAKE_INT_BRANCH (uint8);
MAKE_INT_BRANCH (uint16);
MAKE_INT_BRANCH (uint32);
MAKE_INT_BRANCH (uint64);
#undef MAKE_INT_BRANCH
default:
if (argx.is_cellstr ())
retval = argx.cellstr_value ().nth_element (n, dim);
else
gripe_wrong_type_arg ("nth_element", argx);
}
This is from data.cc:6415 @ rev ec660526ae50.
Can't this be improved? This manual polymorphism is error-prone and
ugly to read. Isn't there an abstract array type that one can convert
an octave_value into and then call virtual functions on this type?
TIA,
- Jordi G. H.
- Branching on builtin_type (),
Jordi Gutiérrez Hermoso <=