view libinterp/corefcn/dispatch.h @ 19008:80ca3b05d77c draft

New "dispatch" selects template argument from octave-value (Bug #42424, 42425) * find.cc (Ffind): This method now calls dispatch() rather than attempting to handle all matrix types on its own (findTemplated): Changed to a functor to be passed as a template template argument to dispatch() (findInfo): A struct that holds the other arguments to find (n_to_find, direction, nargout) Added unit tests for bugs 42424 and 42425 * (new file) dispatch.h (dispatch): A method for dispatching function calls to the right templated value based on an octave_value argument.
author David Spies <dnspies@gmail.com>
date Sat, 21 Jun 2014 13:13:05 -0600
parents
children
line wrap: on
line source

/*

Copyright (C) 2014 David Spies

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_dispatch_h)
#define octave_dispatch_h 1

#include <string>

#include "gripes.h"
#include "oct-obj.h"

// This function takes a templated functor as a template-template argument
// and calls it back with the matrix-type corresponding to arg.
//
// This is handy when you wish to write a templated function for dealing
// with many different matrix types and you don't want to explicitly have
// to list out all the different types your function can deal with and
// how to call it with each one.
//
// It is expected that the functor operator() takes two arguments:
//
// 1. The value being unwrapped from arg
// and
// 2. A struct containing any other information the function needs
// (ie other arguments, nargin, nargout etc.)
//
// The return value is an octave_value_list containing the return value
// for the function
//
// In addition to the above two parameters, the dispatch function itself
// also takes a string argument which is the name of the function being
// called.  This argument is only used for error-reporting
//
// It is highly recommended you always use this function as you can be
// sure dispatch will throw a compiler exception if you forget to handle
// a particular type.
//
// For an example of how to call dispatch, see the "find" function in
// libinterp/corefcn/find.cc
template<template<typename > class fun, typename Inf>
octave_value_list
dispatch (const octave_value& arg, const Inf& info, const std::string& funname)
{
  octave_value_list retval;

  if (arg.is_bool_type ())
    {
      if (arg.is_sparse_type ())
        {
          SparseBoolMatrix v = arg.sparse_bool_matrix_value ();

          if (! error_state)
            retval = fun<SparseBoolMatrix> () (v, info);
        }
      else
        {
          boolNDArray v = arg.bool_array_value ();

          if (! error_state)
            retval = fun<boolNDArray> () (v, info);
        }
    }
  else if (arg.is_integer_type ())
    {
#define DO_INT_BRANCH(INTT) \
      if (arg.is_ ## INTT ## _type ()) \
        { \
          INTT ## NDArray v = arg.INTT ## _array_value (); \
          \
          if (! error_state) \
            retval = fun<INTT ## NDArray> () (v, info);\
        } else

      DO_INT_BRANCH (int8)
      DO_INT_BRANCH (int16)
      DO_INT_BRANCH (int32)
      DO_INT_BRANCH (int64)
      DO_INT_BRANCH (uint8)
      DO_INT_BRANCH (uint16)
      DO_INT_BRANCH (uint32)
      DO_INT_BRANCH (uint64)
        panic_impossible ();
#undef DO_INT_BRANCH
    }
  else if (arg.is_sparse_type ())
    {
      if (arg.is_real_type ())
        {
          SparseMatrix v = arg.sparse_matrix_value ();

          if (! error_state)
            retval = fun<SparseMatrix> () (v, info);
        }
      else if (arg.is_complex_type ())
        {
          SparseComplexMatrix v = arg.sparse_complex_matrix_value ();

          if (! error_state)
            retval = fun<SparseComplexMatrix> () (v, info);
        }
      else
        gripe_wrong_type_arg (funname, arg);
    }
  else if (arg.is_diag_matrix ())
    {
      if (arg.is_real_type ())
        {
          DiagMatrix v = arg.diag_matrix_value ();
          if (! error_state)
            retval = fun<DiagMatrix> () (v, info);
        }
      else if (arg.is_complex_type ())
        {
          ComplexDiagMatrix v = arg.complex_diag_matrix_value ();
          if (! error_state)
            retval = fun<ComplexDiagMatrix> () (v, info);
        }
    }
  else if (arg.is_perm_matrix ())
    {
      PermMatrix v = arg.perm_matrix_value ();

      if (! error_state)
        retval = fun<PermMatrix> () (v, info);
    }
  else if (arg.is_string ())
    {
      charNDArray v = arg.char_array_value ();

      if (! error_state)
        retval = fun<charNDArray> () (v, info);
    }
  else if (arg.is_single_type ())
    {
      if (arg.is_real_type ())
        {
          FloatNDArray v = arg.float_array_value ();

          if (! error_state)
            retval = fun<FloatNDArray> () (v, info);
        }
      else if (arg.is_complex_type ())
        {
          FloatComplexNDArray v = arg.float_complex_array_value ();

          if (! error_state)
            retval = fun<FloatComplexNDArray> () (v, info);
        }
    }
  else if (arg.is_real_type ())
    {
      NDArray v = arg.array_value ();

      if (! error_state)
        retval = fun<NDArray> () (v, info);
    }
  else if (arg.is_complex_type ())
    {
      ComplexNDArray v = arg.complex_array_value ();

      if (! error_state)
        retval = fun<ComplexNDArray> () (v, info);
    }
  else
    gripe_wrong_type_arg (funname, arg);

  return retval;
}

#endif