Mercurial > hg > octave-terminal
changeset 10419:afe44ee90cbd
implement generic macro magic for repeated decls
author | Jaroslav Hajek <highegg@gmail.com> |
---|---|
date | Wed, 17 Mar 2010 10:30:36 +0100 |
parents | 6c19d6fcd7e5 |
children | 3373fdc0b14a |
files | liboctave/ChangeLog liboctave/Makefile.am liboctave/dim-vector.h liboctave/lo-macros.h |
diffstat | 4 files changed, 120 insertions(+), 47 deletions(-) [+] |
line wrap: on
line diff
--- a/liboctave/ChangeLog +++ b/liboctave/ChangeLog @@ -1,3 +1,9 @@ +2010-03-17 Jaroslav Hajek <highegg@gmail.com> + + * lo-macros.h: New source. + * dim-vector.h: Use the macro magic to define constructors + generically. + 2010-03-17 Jaroslav Hajek <highegg@gmail.com> * dim-vector.h: Provide constructors up to order 7.
--- a/liboctave/Makefile.am +++ b/liboctave/Makefile.am @@ -202,6 +202,7 @@ idx-vector.h \ kpse-xfns.h \ lo-ieee.h \ + lo-macros.h \ lo-mappers.h \ lo-math.h \ lo-specfun.h \
--- a/liboctave/dim-vector.h +++ b/liboctave/dim-vector.h @@ -31,6 +31,7 @@ #include <string> #include "lo-error.h" +#include "lo-macros.h" // Rationale: This implementation is more tricky than Array, but the // big plus is that dim_vector requires only one allocation instead of @@ -154,54 +155,24 @@ rep[1] = c; rep[2] = p; } - - dim_vector (octave_idx_type r, octave_idx_type c, octave_idx_type p, - octave_idx_type q) - : rep (newrep (4)) - { - rep[0] = r; - rep[1] = c; - rep[2] = p; - rep[3] = q; - } - - dim_vector (octave_idx_type r, octave_idx_type c, octave_idx_type p, - octave_idx_type q, octave_idx_type s) - : rep (newrep (5)) - { - rep[0] = r; - rep[1] = c; - rep[2] = p; - rep[3] = q; - rep[4] = s; + +#define ASSIGN_REP(i) rep[i] = d ## i; +#define DIM_VECTOR_CTOR(N) \ + dim_vector (OCT_MAKE_DECL_LIST(octave_idx_type, d, N)) \ + : rep (newrep (N)) \ + { \ + OCT_ITERATE_MACRO(ASSIGN_REP, N) \ } - - dim_vector (octave_idx_type r, octave_idx_type c, octave_idx_type p, - octave_idx_type q, octave_idx_type s, octave_idx_type t) - : rep (newrep (6)) - { - rep[0] = r; - rep[1] = c; - rep[2] = p; - rep[3] = q; - rep[4] = s; - rep[5] = t; - } - - dim_vector (octave_idx_type r, octave_idx_type c, octave_idx_type p, - octave_idx_type q, octave_idx_type s, octave_idx_type t, - octave_idx_type u) - : rep (newrep (7)) - { - rep[0] = r; - rep[1] = c; - rep[2] = p; - rep[3] = q; - rep[4] = s; - rep[5] = t; - rep[6] = u; - } - + + // Add more if needed. + DIM_VECTOR_CTOR(4) + DIM_VECTOR_CTOR(5) + DIM_VECTOR_CTOR(6) + DIM_VECTOR_CTOR(7) + +#undef ASSIGN_REP +#undef DIM_VECTOR_CTOR + dim_vector (const octave_idx_type *vec, size_t vec_size) : rep (newrep (vec_size)) {
new file mode 100644 --- /dev/null +++ b/liboctave/lo-macros.h @@ -0,0 +1,95 @@ +/* + +Copyright (C) 2010 VZLU Prague + +This file is part of Octave. + +Octave is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3 of the License, or (at your +option) any later version. + +Octave is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +for more details. + +You should have received a copy of the GNU General Public License +along with Octave; see the file COPYING. If not, see +<http://www.gnu.org/licenses/>. + +*/ + +#if !defined (octave_liboctave_macros_h) +#define octave_liboctave_macros_h 1 + +// Core macros. Iteration is supported up to count 10. + +#define OCT_ITERATE_PARAM_MACRO0(MACRO, PARAM) +#define OCT_ITERATE_PARAM_MACRO1(MACRO, PARAM) \ +MACRO(0, PARAM) +#define OCT_ITERATE_PARAM_MACRO2(MACRO, PARAM) \ +MACRO(0, PARAM) MACRO(1, PARAM) +#define OCT_ITERATE_PARAM_MACRO3(MACRO, PARAM) \ +MACRO(0, PARAM) MACRO(1, PARAM) MACRO(2, PARAM) +#define OCT_ITERATE_PARAM_MACRO4(MACRO, PARAM) \ +MACRO(0, PARAM) MACRO(1, PARAM) MACRO(2, PARAM) MACRO(3, PARAM) +#define OCT_ITERATE_PARAM_MACRO5(MACRO, PARAM) \ +MACRO(0, PARAM) MACRO(1, PARAM) MACRO(2, PARAM) MACRO(3, PARAM) MACRO(4, PARAM) +#define OCT_ITERATE_PARAM_MACRO6(MACRO, PARAM) \ +OCT_ITERATE_PARAM_MACRO5(MACRO, PARAM) MACRO(5, PARAM) +#define OCT_ITERATE_PARAM_MACRO7(MACRO, PARAM) \ +OCT_ITERATE_PARAM_MACRO5(MACRO, PARAM) MACRO(5, PARAM) MACRO(6, PARAM) +#define OCT_ITERATE_PARAM_MACRO8(MACRO, PARAM) \ +OCT_ITERATE_PARAM_MACRO5(MACRO, PARAM) MACRO(5, PARAM) MACRO(6, PARAM) MACRO(7, PARAM) +#define OCT_ITERATE_PARAM_MACRO9(MACRO, PARAM) \ +OCT_ITERATE_PARAM_MACRO8(MACRO, PARAM) MACRO(8, PARAM) +#define OCT_ITERATE_PARAM_MACRO10(MACRO, PARAM) \ +OCT_ITERATE_PARAM_MACRO8(MACRO, PARAM) MACRO(8, PARAM) MACRO(9, PARAM) + +// expands to MACRO(0, PARAM) MACRO(1, PARAM) ... MACRO(NUM-1, PARAM) +#define OCT_ITERATE_PARAM_MACRO(MACRO, PARAM, NUM) OCT_ITERATE_PARAM_MACRO##NUM(MACRO,PARAM) + +#define OCT_IF_PARAM0(MACRO, PARAM) +#define OCT_IF_PARAM1(MACRO, PARAM) MACRO(PARAM) +#define OCT_IF_PARAM2(MACRO, PARAM) MACRO(PARAM) +#define OCT_IF_PARAM3(MACRO, PARAM) MACRO(PARAM) +#define OCT_IF_PARAM4(MACRO, PARAM) MACRO(PARAM) +#define OCT_IF_PARAM5(MACRO, PARAM) MACRO(PARAM) +#define OCT_IF_PARAM6(MACRO, PARAM) MACRO(PARAM) +#define OCT_IF_PARAM7(MACRO, PARAM) MACRO(PARAM) +#define OCT_IF_PARAM8(MACRO, PARAM) MACRO(PARAM) +#define OCT_IF_PARAM9(MACRO, PARAM) MACRO(PARAM) + +// expands to MACRO(PARAM) if NUM > 0 +#define OCT_IF_PARAM(NUM, MACRO, PARAM) OCT_IF_PARAM##NUM(MACRO, PARAM) + +// concatenation and reverse concat +#define OCT_CONCAT2(ARG1, ARG2) ARG1 ## ARG2 +#define OCT_CONCAT2_REVERSE(ARG1, ARG2) ARG2 ## ARG1 + +#define OCT_IF_HELPER(X) X + +// expands to X if NUM > 0 +#define OCT_IF(NUM, X) OCT_IF_PARAM(NUM, OCT_IF_HELPER, X) + +#define OCT_ITERATE_MACRO_HELPER(NUM, MACRO) MACRO(NUM) + +// expands to MACRO(0) ... MACRO(NUM-1) +#define OCT_ITERATE_MACRO(MACRO, NUM) OCT_ITERATE_PARAM_MACRO(OCT_ITERATE_MACRO_HELPER, MACRO, NUM) + +#define OCT_MAKE_LIST_HELPER1(ARG) ARG, + +// expands to MACRO(0), MACRO(1), ..., MACRO(NUM-1) +#define OCT_MAKE_LIST_HELPER(NUM, MACRO) OCT_IF_PARAM(NUM,OCT_MAKE_LIST_HELPER1,) MACRO(NUM) + +#define OCT_MAKE_LIST(MACRO, NUM) OCT_ITERATE_PARAM_MACRO(OCT_MAKE_LIST_HELPER, MACRO, NUM) + +#define OCT_MAKE_DECL_LIST_HELPER(NUM, PREFIX) \ + OCT_IF_PARAM(NUM,OCT_MAKE_LIST_HELPER1,) OCT_CONCAT2(PREFIX, NUM) + +// expands to TYPE PREFIX0, TYPE PREFIX1, ..., TYPE PREFIX ## (NUM-1) +#define OCT_MAKE_DECL_LIST(TYPE, PREFIX, NUM) \ + OCT_ITERATE_PARAM_MACRO(OCT_MAKE_DECL_LIST_HELPER, TYPE PREFIX, NUM) + +#endif