45 #include <octave/oct.h>
46 #include <octave/parse.h>
47 #include <octave/dynamic-ld.h>
48 #include <octave/oct-map.h>
49 #include <octave/oct-stream.h>
50 #include <octave/ov-base-scalar.h>
60 octave_object::method_function* octave_object::class_object::lookup_method(
const std::string& method_name)
const
62 typedef method_table_t::const_iterator iter_t;
64 iter_t p = method_table.find(method_name);
66 if ( p != method_table.end() )
69 if ( parent_object != NULL )
71 return parent_object->lookup_method(method_name);
75 cerr <<
"octave_object::class_object::lookup_method(\"" << method_name <<
"\") failed!" << endl;
76 const class_object* class_obj =
this;
78 cerr <<
"dump of octave_object::class_object::method_table:" << endl;
80 while ( class_obj != NULL )
82 for ( iter_t p = class_obj->method_table.begin(); p != class_obj->method_table.end(); ++p )
83 cerr <<
" " << p->first <<
"->" << p->second << endl;
84 class_obj = class_obj->parent_object;
91 octave_object::class_object octave_object::dispatch_class_object(NULL,NULL);
93 octave_object::octave_object()
98 octave_object::~octave_object()
103 static bool any_arg_is_magic_colon (
const octave_value_list& args)
105 int nargin = args.length ();
107 for (
int i = 0; i <
nargin; i++)
108 if (args(i).is_magic_colon ())
114 #if TRACE_SUBSREF == 1
116 static void print_args(
const octave_value_list& args)
118 int nargin = args.length ();
120 for (
int i = 0; i <
nargin; i++)
122 args(i).print(octave_stdout);
125 octave_stdout <<
',';
131 octave_object::method_function* octave_object::lookup_method(
const std::string& method_name)
133 return get_class_object()->lookup_method(method_name);
136 octave_value_list octave_object::subsref (
const std::string& type,
137 const std::list<octave_value_list>& idx,
140 #if TRACE_SUBSREF == 1
142 octave_stdout << endl;
143 octave_stdout << __FILE__ <<
":" << __LINE__ <<
":" << __FUNCTION__ << endl;
144 octave_stdout <<
"type=" << type << endl;
146 typedef std::list<octave_value_list>::const_iterator iter_t;
150 for ( iter_t it = idx.begin(); it != idx.end(); ++it, ++i )
152 octave_stdout <<
"idx(" << i <<
")=";
154 octave_stdout << endl;
162 if ( type.length() < 1 )
167 const char operator_char = type[0];
169 switch( operator_char )
173 octave_value_list method_name_list = idx.front();
175 if ( method_name_list.length () != 1 || !method_name_list(0).is_string() )
178 const std::string method_name = method_name_list(0).string_value();
180 method_function *method_pfn = lookup_method(method_name);
182 if ( method_pfn == NULL )
184 error(
"octave_object: class \"%s\" has no member \"%s\"", class_name().c_str(), method_name.c_str());
188 if ( idx.size() < 2 || type[1] !=
'(')
190 retval = (*method_pfn)(
this,octave_value_list(),nargout);
195 std::list<octave_value_list>::const_iterator pidx = idx.begin();
198 if (any_arg_is_magic_colon (*pidx))
200 error(
"octave object: invalid use of colon in method argument list");
205 retval = (*method_pfn)(
this,*pidx,nargout);
214 error(
"octave object: invalid number of indices");
218 retval = (*this)(idx.front());
223 std::string nm = type_name();
224 error(
"%s cannot be indexed with %c", nm.c_str(), operator_char);
234 if ( !error_state && idx.size() > skip )
236 retval =
retval(0).next_subsref(type, idx, skip);
242 octave_value octave_object::operator()(
const octave_value_list& idx)
const
244 std::string nm = type_name();
245 error(
"%s cannot be indexed with %c", nm.c_str(),
'(');
246 return octave_value();
249 octave_value octave_object::subsref(
const std::string& type,
250 const std::list<octave_value_list>& idx)
252 octave_value_list ret_val = subsref(type,idx,1);
254 if ( ret_val.length() < 1 )
255 return octave_value();
260 void error(
const char* fmt, ...)
int error(const char *test, int value)