op-class.cc

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2007-2012 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #ifdef HAVE_CONFIG_H
00024 #include <config.h>
00025 #endif
00026 
00027 #include "oct-time.h"
00028 
00029 #include "gripes.h"
00030 #include "load-path.h"
00031 #include "oct-obj.h"
00032 #include "ov.h"
00033 #include "ov-class.h"
00034 #include "ov-typeinfo.h"
00035 #include "ops.h"
00036 #include "symtab.h"
00037 #include "parse.h"
00038 
00039 // class ops.
00040 
00041 #define DEF_CLASS_UNOP(name) \
00042   static octave_value \
00043   oct_unop_ ## name (const octave_value& a) \
00044   { \
00045     octave_value retval; \
00046  \
00047     std::string class_name = a.class_name (); \
00048  \
00049     octave_value meth = symbol_table::find_method (#name, class_name); \
00050  \
00051     if (meth.is_defined ()) \
00052       { \
00053         octave_value_list args; \
00054  \
00055         args(0) = a; \
00056  \
00057         octave_value_list tmp = feval (meth.function_value (), args, 1); \
00058  \
00059         if (tmp.length () > 0) \
00060           retval = tmp(0); \
00061       } \
00062     else \
00063       error ("%s method not defined for %s class", \
00064              #name, class_name.c_str ()); \
00065  \
00066     return retval; \
00067   }
00068 
00069 DEF_CLASS_UNOP (not)
00070 DEF_CLASS_UNOP (uplus)
00071 DEF_CLASS_UNOP (uminus)
00072 DEF_CLASS_UNOP (transpose)
00073 DEF_CLASS_UNOP (ctranspose)
00074 
00075 // FIXME -- we need to handle precedence in the binop function.
00076 
00077 #define DEF_CLASS_BINOP(name) \
00078   static octave_value \
00079   oct_binop_ ## name (const octave_value& a1, const octave_value& a2) \
00080   { \
00081     octave_value retval; \
00082  \
00083     std::string dispatch_type \
00084       = a1.is_object () ? a1.class_name () : a2.class_name (); \
00085  \
00086     octave_value meth = symbol_table::find_method (#name, dispatch_type); \
00087  \
00088     if (meth.is_defined ()) \
00089       { \
00090         octave_value_list args; \
00091  \
00092         args(1) = a2; \
00093         args(0) = a1; \
00094  \
00095         octave_value_list tmp = feval (meth.function_value (), args, 1); \
00096  \
00097         if (tmp.length () > 0) \
00098           retval = tmp(0); \
00099       } \
00100     else \
00101       error ("%s method not defined for %s class", \
00102              #name, dispatch_type.c_str ()); \
00103  \
00104     return retval; \
00105   }
00106 
00107 DEF_CLASS_BINOP (plus)
00108 DEF_CLASS_BINOP (minus)
00109 DEF_CLASS_BINOP (mtimes)
00110 DEF_CLASS_BINOP (mrdivide)
00111 DEF_CLASS_BINOP (mpower)
00112 DEF_CLASS_BINOP (mldivide)
00113 DEF_CLASS_BINOP (lt)
00114 DEF_CLASS_BINOP (le)
00115 DEF_CLASS_BINOP (eq)
00116 DEF_CLASS_BINOP (ge)
00117 DEF_CLASS_BINOP (gt)
00118 DEF_CLASS_BINOP (ne)
00119 DEF_CLASS_BINOP (times)
00120 DEF_CLASS_BINOP (rdivide)
00121 DEF_CLASS_BINOP (power)
00122 DEF_CLASS_BINOP (ldivide)
00123 DEF_CLASS_BINOP (and)
00124 DEF_CLASS_BINOP (or)
00125 
00126 #define INSTALL_CLASS_UNOP(op, f) \
00127   octave_value_typeinfo::register_unary_class_op \
00128     (octave_value::op, oct_unop_ ## f)
00129 
00130 #define INSTALL_CLASS_BINOP(op, f) \
00131   octave_value_typeinfo::register_binary_class_op \
00132     (octave_value::op, oct_binop_ ## f)
00133 
00134 void
00135 install_class_ops (void)
00136 {
00137   INSTALL_CLASS_UNOP (op_not, not);
00138   INSTALL_CLASS_UNOP (op_uplus, uplus);
00139   INSTALL_CLASS_UNOP (op_uminus, uminus);
00140   INSTALL_CLASS_UNOP (op_transpose, transpose);
00141   INSTALL_CLASS_UNOP (op_hermitian, ctranspose);
00142 
00143   INSTALL_CLASS_BINOP (op_add, plus);
00144   INSTALL_CLASS_BINOP (op_sub, minus);
00145   INSTALL_CLASS_BINOP (op_mul, mtimes);
00146   INSTALL_CLASS_BINOP (op_div, mrdivide);
00147   INSTALL_CLASS_BINOP (op_pow, mpower);
00148   INSTALL_CLASS_BINOP (op_ldiv, mldivide);
00149   INSTALL_CLASS_BINOP (op_lt, lt);
00150   INSTALL_CLASS_BINOP (op_le, le);
00151   INSTALL_CLASS_BINOP (op_eq, eq);
00152   INSTALL_CLASS_BINOP (op_ge, ge);
00153   INSTALL_CLASS_BINOP (op_gt, gt);
00154   INSTALL_CLASS_BINOP (op_ne, ne);
00155   INSTALL_CLASS_BINOP (op_el_mul, times);
00156   INSTALL_CLASS_BINOP (op_el_div, rdivide);
00157   INSTALL_CLASS_BINOP (op_el_pow, power);
00158   INSTALL_CLASS_BINOP (op_el_ldiv, ldivide);
00159   INSTALL_CLASS_BINOP (op_el_and, and);
00160   INSTALL_CLASS_BINOP (op_el_or, or);
00161 }
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines