GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
ov-classdef.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 2012-2024 The Octave Project Developers
4 //
5 // See the file COPYRIGHT.md in the top-level directory of this
6 // distribution or <https://octave.org/copyright/>.
7 //
8 // This file is part of Octave.
9 //
10 // Octave is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 3 of the License, or
13 // (at your option) any later version.
14 //
15 // Octave is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Octave; see the file COPYING. If not, see
22 // <https://www.gnu.org/licenses/>.
23 //
24 ////////////////////////////////////////////////////////////////////////
25 
26 #if ! defined (octave_classdef_h)
27 #define octave_classdef_h 1
28 
29 #include "octave-config.h"
30 
31 #include <string>
32 
33 #include "cdef-object.h"
34 #include "ov-base.h"
35 #include "ov-fcn.h"
36 
38 
39 class cdef_object;
40 class interpreter;
41 class tree_evaluator;
42 class type_info;
43 
44 OCTAVE_END_NAMESPACE(octave)
45 
46 class
48 {
49 public:
50 
52  : octave_base_value (), m_object () { }
53 
54  octave_classdef (const octave::cdef_object& obj)
55  : octave_base_value (), m_object (obj) { }
56 
57  OCTAVE_DISABLE_COPY_MOVE (octave_classdef)
58 
59  ~octave_classdef () = default;
60 
62  {
63  return new octave_classdef (m_object.clone ());
64  }
65 
67  {
68  return new octave_classdef (m_object.empty_clone ());
69  }
70 
71  octave_classdef * classdef_object_value (bool = false) { return this; }
72 
73  octave::cdef_object get_object () const { return m_object; }
74 
75  octave::cdef_object& get_object_ref () { return m_object; }
76 
77  bool is_defined () const { return true; }
78 
79  bool isstruct () const { return false; }
80 
81  bool isobject () const { return true; }
82 
83  bool is_classdef_object () const { return true; }
84 
85  OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
86 
87  OCTINTERP_API void
88  print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
89 
90  OCTINTERP_API bool is_instance_of (const std::string& cls_name) const;
91 
92  void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame)
93  {
94  m_object.break_closure_cycles (frame);
95  }
96 
97  OCTINTERP_API octave_value_list
98  subsref (const std::string& type, const std::list<octave_value_list>& idx,
99  int nargout);
100 
101  octave_value subsref (const std::string& type,
102  const std::list<octave_value_list>& idx)
103  {
104  octave_value_list retval = subsref (type, idx, 1);
105  return (retval.length () > 0 ? retval(0) : octave_value ());
106  }
107 
108  OCTINTERP_API octave_value
109  subsref (const std::string& type, const std::list<octave_value_list>& idx,
110  bool auto_add);
111 
112  OCTINTERP_API octave_value
113  subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
114  const octave_value& rhs);
115 
116  OCTINTERP_API octave_value
117  undef_subsasgn (const std::string& type,
118  const std::list<octave_value_list>& idx,
119  const octave_value& rhs);
120 
121  OCTINTERP_API Matrix size ();
122 
123  OCTINTERP_API octave_idx_type xnumel (const octave_value_list&);
124 
125  string_vector map_keys () const { return m_object.map_keys (); }
126 
127  octave_map map_value () const { return m_object.map_value (); }
128 
129  dim_vector dims () const { return m_object.dims (); }
130 
131  void set_property (octave_idx_type idx, const std::string& name,
132  const octave_value& pval)
133  {
134  m_object.set_property (idx, name, pval);
135  }
136 
138  get_property (octave_idx_type idx, const std::string& name) const
139  {
140  return m_object.get_property (idx, name);
141  }
142 
143  static OCTINTERP_API octave_value
144  superclass_ref (const std::string& meth, const std::string& cls);
145 
146  static OCTINTERP_API octave_value metaclass_query (const std::string& cls);
147 
148 public:
149 
150  int type_id () const { return s_t_id; }
151  std::string type_name () const { return s_t_name; }
152  std::string class_name () const { return m_object.class_name (); }
153 
154  static int static_type_id () { return s_t_id; }
155  static std::string static_type_name () { return s_t_name; }
156  static std::string static_class_name () { return "<unknown>"; }
157  static OCTINTERP_API void register_type (octave::type_info&);
158 
159 private:
160 
161  octave::cdef_object m_object;
162 
163  static int s_t_id;
164 
165  static const std::string s_t_name;
166 };
167 
168 void install_classdef (octave::interpreter& interp);
169 
171 {
172 public:
173 
174  octave_classdef_meta (const octave::cdef_meta_object& obj)
175  : m_object (obj)
176  { }
177 
178  OCTAVE_DISABLE_COPY_MOVE (octave_classdef_meta)
179 
180  ~octave_classdef_meta () { m_object.meta_release (); }
181 
182  bool is_classdef_meta () const { return true; }
183 
184  bool is_package () const { return m_object.is_package(); }
185 
186  octave_function * function_value (bool = false) { return this; }
187 
188  // We don't need to override all three forms of subsref. The using
189  // declaration will avoid warnings about partially-overloaded virtual
190  // functions.
192 
194  subsref (const std::string& type,
195  const std::list<octave_value_list>& idx,
196  int nargout)
197  {
198  return m_object.meta_subsref (type, idx, nargout);
199  }
200 
201  // Override default call method because we don't push a new stack
202  // frame for this operation on classdef_meta objects.
203 
204  octave_value_list call (octave::tree_evaluator& tw, int nargout,
205  const octave_value_list& args)
206  {
207  return execute (tw, nargout, args);
208  }
209 
210  octave_value_list execute (octave::tree_evaluator&, int nargout,
211  const octave_value_list& args)
212  {
213  // Emulate ()-type meta subsref
214 
215  std::list<octave_value_list> idx (1, args);
216  std::string type ("(");
217 
218  return subsref (type, idx, nargout);
219  }
220 
221  bool accepts_postfix_index (char type) const
222  { return m_object.meta_accepts_postfix_index (type); }
223 
224  OCTINTERP_API bool is_classdef_method (const std::string& cname = "") const;
225 
226  OCTINTERP_API bool
227  is_classdef_constructor (const std::string& cname = "") const;
228 
229  OCTINTERP_API std::string doc_string (const std::string& meth_name) const;
230 
231  OCTINTERP_API std::string file_name () const;
232 
233 private:
234 
235  octave::cdef_meta_object m_object;
236 };
237 
239 {
240 public:
242 
243  octave_classdef_superclass_ref (const std::string& meth,
244  const std::string& cls)
245  : octave_function (), m_method_name (meth), m_class_name (cls)
246  { }
247 
248  OCTAVE_DISABLE_COPY_MOVE (octave_classdef_superclass_ref)
249 
251 
252  bool is_classdef_superclass_ref () const { return true; }
253 
254  octave_function * function_value (bool = false) { return this; }
255 
256  // Override default call method because we don't push a new stack
257  // frame for this operation on classdef_superclass_ref objects.
258 
259  octave_value_list call (octave::tree_evaluator& tw, int nargout,
260  const octave_value_list& args)
261  {
262  return execute (tw, nargout, args);
263  }
264 
265  OCTINTERP_API octave_value_list
266  execute (octave::tree_evaluator& tw, int nargout,
267  const octave_value_list& idx);
268 
269 private:
270 
271  OCTINTERP_API bool
272  is_constructed_object (octave::tree_evaluator& tw, const std::string& nm);
273 
274 private:
275 
276  std::string m_method_name;
277  std::string m_class_name;
278 };
279 
280 #endif
Definition: dMatrix.h:42
Vector representing the dimensions (size) of an Array.
Definition: dim-vector.h:94
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-base.cc:248
octave_value_list call(octave::tree_evaluator &tw, int nargout, const octave_value_list &args)
Definition: ov-classdef.h:204
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
Definition: ov-classdef.h:194
bool is_classdef_method(const std::string &cname="") const
Definition: ov-classdef.cc:443
bool accepts_postfix_index(char type) const
Definition: ov-classdef.h:221
octave_classdef_meta(const octave::cdef_meta_object &obj)
Definition: ov-classdef.h:174
std::string file_name() const
Definition: ov-classdef.cc:526
octave_function * function_value(bool=false)
Definition: ov-classdef.h:186
bool is_classdef_constructor(const std::string &cname="") const
Definition: ov-classdef.cc:463
std::string doc_string(const std::string &meth_name) const
Definition: ov-classdef.cc:507
bool is_classdef_meta() const
Definition: ov-classdef.h:182
bool is_package() const
Definition: ov-classdef.h:184
octave_value_list execute(octave::tree_evaluator &, int nargout, const octave_value_list &args)
Definition: ov-classdef.h:210
octave_function * function_value(bool=false)
Definition: ov-classdef.h:254
bool is_classdef_superclass_ref() const
Definition: ov-classdef.h:252
octave_value_list call(octave::tree_evaluator &tw, int nargout, const octave_value_list &args)
Definition: ov-classdef.h:259
octave_value_list execute(octave::tree_evaluator &tw, int nargout, const octave_value_list &idx)
Definition: ov-classdef.cc:539
octave_classdef_superclass_ref(const std::string &meth, const std::string &cls)
Definition: ov-classdef.h:243
octave_base_value * clone() const
Definition: ov-classdef.h:61
static std::string static_class_name()
Definition: ov-classdef.h:156
octave_base_value * empty_clone() const
Definition: ov-classdef.h:66
octave_map map_value() const
Definition: ov-classdef.h:127
void break_closure_cycles(const std::shared_ptr< octave::stack_frame > &frame)
Definition: ov-classdef.h:92
octave_value get_property(octave_idx_type idx, const std::string &name) const
Definition: ov-classdef.h:138
dim_vector dims() const
Definition: ov-classdef.h:129
octave_classdef(const octave::cdef_object &obj)
Definition: ov-classdef.h:54
octave::cdef_object & get_object_ref()
Definition: ov-classdef.h:75
bool isstruct() const
Definition: ov-classdef.h:79
bool is_classdef_object() const
Definition: ov-classdef.h:83
octave::cdef_object get_object() const
Definition: ov-classdef.h:73
static std::string static_type_name()
Definition: ov-classdef.h:155
bool is_defined() const
Definition: ov-classdef.h:77
static int static_type_id()
Definition: ov-classdef.h:154
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-classdef.h:101
std::string type_name() const
Definition: ov-classdef.h:151
std::string class_name() const
Definition: ov-classdef.h:152
void set_property(octave_idx_type idx, const std::string &name, const octave_value &pval)
Definition: ov-classdef.h:131
string_vector map_keys() const
Definition: ov-classdef.h:125
octave_classdef * classdef_object_value(bool=false)
Definition: ov-classdef.h:71
bool isobject() const
Definition: ov-classdef.h:81
~octave_classdef()=default
int type_id() const
Definition: ov-classdef.h:150
octave_idx_type length() const
Definition: ovl.h:113
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void install_classdef(octave::interpreter &interp)
int register_type(const std::string &t_name, const std::string &c_name, const octave_value &val)
Definition: ov-typeinfo.cc:784