41 #ifndef OCTAVE_OBJECT_H_
42 #define OCTAVE_OBJECT_H_
48 #include <octave/oct.h>
58 class octave_object:
public octave_base_value
61 typedef octave_value_list method_function(octave_object*,
const octave_value_list&,
int);
62 typedef void setup_class_object_function();
66 explicit class_object(class_object* parent_obj,setup_class_object_function* setup_class_obj_func)
67 :parent_object(parent_obj)
69 if ( NULL != setup_class_obj_func )
70 (*setup_class_obj_func)();
73 method_function*& operator[](
const std::string& method_name)
75 return method_table[method_name];
80 return method_table.size();
82 method_function* lookup_method(
const std::string& method_name)
const;
84 typedef std::map<std::string,method_function*> method_table_t;
85 method_table_t method_table;
86 class_object*
const parent_object;
90 virtual ~octave_object();
92 static class_object dispatch_class_object;
94 virtual octave_value operator()(
const octave_value_list& idx)
const;
95 virtual const class_object* get_class_object()=0;
96 virtual octave_value_list subsref(
const std::string& type,
97 const std::list<octave_value_list>& idx,
99 virtual octave_value subsref(
const std::string& type,
100 const std::list<octave_value_list>& idx);
101 method_function* lookup_method(
const std::string& method_name);
103 virtual bool is_constant(
void)
const{
return true; }
104 virtual bool is_defined(
void)
const{
return true; }
107 template <
typename T>
108 class octave_object_ptr:
public octave_value
116 explicit octave_object_ptr(T* p)
122 explicit octave_object_ptr(octave_base_value* p)
128 octave_object_ptr(
const octave_object_ptr& ptr)
133 octave_object_ptr(
const octave_value& rhs)
136 check_type(rhs.get_rep());
139 template <
typename U>
140 octave_object_ptr(
const octave_object_ptr<U>& rhs)
143 check_type(rhs.get_rep());
146 octave_object_ptr& operator=(
const octave_object_ptr& rhs)
148 octave_value::operator =(rhs);
152 octave_object_ptr& operator=(
const octave_value& rhs)
154 check_type(rhs.get_rep());
156 octave_value::operator=(rhs);
160 template <
typename U>
161 octave_object_ptr& operator=(
const octave_object_ptr<U>& rhs)
163 check_type(rhs.get_rep());
165 octave_value::operator=(rhs);
171 return static_cast<T&
>(
const_cast<octave_base_value&
>(octave_value::get_rep()));
180 static void check_type(
const octave_base_value& ref)
throw(std::bad_cast)
182 if ( NULL == dynamic_cast<const T*>(&ref) )
183 throw std::bad_cast();
187 extern void error(
const char* fmt, ...)
189 __attribute__((format (printf, 1, 2)))
191 #warning "printf format will not be checked!"
195 #define BEGIN_METHOD_TABLE_DECLARE() \
196 virtual const class_object* get_class_object(); \
197 static class_object dispatch_class_object; \
198 static void setup_class_object();
200 #define METHOD_DECLARE(method_name) \
201 octave_value_list method_name(const octave_value_list&, int); \
202 static octave_value_list method_name(octave_object* object__,const octave_value_list&,int);
204 #define END_METHOD_TABLE_DECLARE()
206 #define METHOD_DEFINE(class_name,method_name,args,nargout) \
207 octave_value_list class_name::method_name(octave_object *object__, \
208 const octave_value_list& args, \
211 return dynamic_cast<class_name&>(*object__).method_name(args,nargout); \
213 octave_value_list class_name::method_name(const octave_value_list& args, int nargout)
215 #define BEGIN_METHOD_TABLE(class_name,base_class_name) \
216 octave_object::class_object class_name::dispatch_class_object(&base_class_name::dispatch_class_object,&class_name::setup_class_object); \
217 const octave_object::class_object* class_name::get_class_object() \
219 return &dispatch_class_object; \
221 void class_name::setup_class_object() \
223 if ( dispatch_class_object.size() == 0 ) \
226 #define METHOD_DISPATCH(class_name,method_name) \
227 dispatch_class_object[#method_name] = &class_name::method_name;
229 #define END_METHOD_TABLE() \
230 } else assert(false); \
int error(const char *test, int value)