GNU Octave  8.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-2023 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 
45 
46 class
48 {
49 public:
50 
52  : octave_base_value (), m_object () { }
53 
55  : octave_base_value (), m_object (obj) { }
56 
57  octave_classdef (const octave_classdef&) = delete;
58 
60 
61  ~octave_classdef (void) = default;
62 
63  octave_base_value * clone (void) const
64  {
65  return new octave_classdef (m_object.clone ());
66  }
67 
69  {
70  return new octave_classdef (m_object.empty_clone ());
71  }
72 
73  octave_classdef * classdef_object_value (bool = false) { return this; }
74 
75  octave::cdef_object get_object (void) const { return m_object; }
76 
77  octave::cdef_object& get_object_ref (void) { return m_object; }
78 
79  bool is_defined (void) const { return true; }
80 
81  bool isstruct (void) const { return false; }
82 
83  bool isobject (void) const { return true; }
84 
85  bool is_classdef_object (void) const { return true; }
86 
87  OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
88 
89  OCTINTERP_API void
90  print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
91 
92  OCTINTERP_API bool is_instance_of (const std::string& cls_name) const;
93 
94  void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame)
95  {
96  m_object.break_closure_cycles (frame);
97  }
98 
99  OCTINTERP_API octave_value_list
100  subsref (const std::string& type, const std::list<octave_value_list>& idx,
101  int nargout);
102 
103  octave_value subsref (const std::string& type,
104  const std::list<octave_value_list>& idx)
105  {
106  octave_value_list retval = subsref (type, idx, 1);
107  return (retval.length () > 0 ? retval(0) : octave_value ());
108  }
109 
110  OCTINTERP_API octave_value
111  subsref (const std::string& type, const std::list<octave_value_list>& idx,
112  bool auto_add);
113 
114  OCTINTERP_API octave_value
115  subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
116  const octave_value& rhs);
117 
118  OCTINTERP_API octave_value
119  undef_subsasgn (const std::string& type,
120  const std::list<octave_value_list>& idx,
121  const octave_value& rhs);
122 
123  OCTINTERP_API Matrix size (void);
124 
125  OCTINTERP_API octave_idx_type xnumel (const octave_value_list&);
126 
127  string_vector map_keys (void) const { return m_object.map_keys (); }
128 
129  octave_map map_value (void) const { return m_object.map_value (); }
130 
131  dim_vector dims (void) const { return m_object.dims (); }
132 
133  void set_property (octave_idx_type idx, const std::string& name,
134  const octave_value& pval)
135  {
136  m_object.set_property (idx, name, pval);
137  }
138 
140  get_property (octave_idx_type idx, const std::string& name) const
141  {
142  return m_object.get_property (idx, name);
143  }
144 
145  static OCTINTERP_API octave_value
146  superclass_ref (const std::string& meth, const std::string& cls);
147 
148  static OCTINTERP_API octave_value metaclass_query (const std::string& cls);
149 
150 public:
151 
152  int type_id (void) const { return t_id; }
153  std::string type_name (void) const { return t_name; }
154  std::string class_name (void) const { return m_object.class_name (); }
155 
156  static int static_type_id (void) { return t_id; }
157  static std::string static_type_name (void) { return t_name; }
158  static std::string static_class_name (void) { return "<unknown>"; }
159  static OCTINTERP_API void register_type (octave::type_info&);
160 
161 private:
162 
164 
165  static int t_id;
166 
167  static const std::string t_name;
168 };
169 
170 void install_classdef (octave::interpreter& interp);
171 
173 {
174 public:
175 
176  octave_classdef_meta (const octave::cdef_meta_object& obj)
177  : m_object (obj)
178  { }
179 
181 
183 
184  ~octave_classdef_meta (void) { m_object.meta_release (); }
185 
186  bool is_classdef_meta (void) const { return true; }
187 
188  bool is_package (void) const { return m_object.is_package(); }
189 
190  octave_function * function_value (bool = false) { return this; }
191 
192  // We don't need to override all three forms of subsref. The using
193  // declaration will avoid warnings about partially-overloaded virtual
194  // functions.
196 
198  subsref (const std::string& type,
199  const std::list<octave_value_list>& idx,
200  int nargout)
201  {
202  return m_object.meta_subsref (type, idx, nargout);
203  }
204 
205  // Override default call method because we don't push a new stack
206  // frame for this operation on classdef_meta objects.
207 
208  octave_value_list call (octave::tree_evaluator& tw, int nargout,
209  const octave_value_list& args)
210  {
211  return execute (tw, nargout, args);
212  }
213 
214  octave_value_list execute (octave::tree_evaluator&, int nargout,
215  const octave_value_list& args)
216  {
217  // Emulate ()-type meta subsref
218 
219  std::list<octave_value_list> idx (1, args);
220  std::string type ("(");
221 
222  return subsref (type, idx, nargout);
223  }
224 
225  bool accepts_postfix_index (char type) const
226  { return m_object.meta_accepts_postfix_index (type); }
227 
228  OCTINTERP_API bool is_classdef_method (const std::string& cname = "") const;
229 
230  OCTINTERP_API bool
231  is_classdef_constructor (const std::string& cname = "") const;
232 
233  OCTINTERP_API std::string doc_string (const std::string& meth_name) const;
234 
235  OCTINTERP_API std::string file_name (void) const;
236 
237 private:
238 
239  octave::cdef_meta_object m_object;
240 };
241 
243 {
244 public:
246 
247  octave_classdef_superclass_ref (const std::string& meth,
248  const std::string& cls)
249  : octave_function (), m_method_name (meth), m_class_name (cls)
250  { }
251 
253 
255 
257 
258  bool is_classdef_superclass_ref (void) const { return true; }
259 
260  octave_function * function_value (bool = false) { return this; }
261 
262  // Override default call method because we don't push a new stack
263  // frame for this operation on classdef_superclass_ref objects.
264 
265  octave_value_list call (octave::tree_evaluator& tw, int nargout,
266  const octave_value_list& args)
267  {
268  return execute (tw, nargout, args);
269  }
270 
271  OCTINTERP_API octave_value_list
272  execute (octave::tree_evaluator& tw, int nargout,
273  const octave_value_list& idx);
274 
275 private:
276 
277  OCTINTERP_API bool
278  is_constructed_object (octave::tree_evaluator& tw, const std::string& nm);
279 
280 private:
281 
282  std::string m_method_name;
283  std::string m_class_name;
284 };
285 
286 #endif
287 
OCTAVE_END_NAMESPACE(octave)
class OCTINTERP_API cdef_object
Definition: cdef-fwd.h:34
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:200
octave_value_list call(octave::tree_evaluator &tw, int nargout, const octave_value_list &args)
Definition: ov-classdef.h:208
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
Definition: ov-classdef.h:198
OCTINTERP_API bool is_classdef_method(const std::string &cname="") const
Definition: ov-classdef.cc:405
bool accepts_postfix_index(char type) const
Definition: ov-classdef.h:225
bool is_package(void) const
Definition: ov-classdef.h:188
octave::cdef_meta_object m_object
Definition: ov-classdef.h:239
octave_classdef_meta(const octave::cdef_meta_object &obj)
Definition: ov-classdef.h:176
octave_function * function_value(bool=false)
Definition: ov-classdef.h:190
OCTINTERP_API bool is_classdef_constructor(const std::string &cname="") const
Definition: ov-classdef.cc:424
OCTINTERP_API std::string doc_string(const std::string &meth_name) const
Definition: ov-classdef.cc:444
octave_classdef_meta & operator=(const octave_classdef_meta &)=delete
OCTINTERP_API std::string file_name(void) const
Definition: ov-classdef.cc:462
bool is_classdef_meta(void) const
Definition: ov-classdef.h:186
octave_classdef_meta(const octave_classdef_meta &)=delete
octave_value_list execute(octave::tree_evaluator &, int nargout, const octave_value_list &args)
Definition: ov-classdef.h:214
bool is_classdef_superclass_ref(void) const
Definition: ov-classdef.h:258
octave_classdef_superclass_ref & operator=(const octave_classdef_superclass_ref &)=delete
octave_function * function_value(bool=false)
Definition: ov-classdef.h:260
octave_classdef_superclass_ref(const octave_classdef_superclass_ref &)=delete
OCTINTERP_API bool is_constructed_object(octave::tree_evaluator &tw, const std::string &nm)
Definition: ov-classdef.cc:581
~octave_classdef_superclass_ref(void)=default
octave_classdef_superclass_ref(void)=delete
octave_value_list call(octave::tree_evaluator &tw, int nargout, const octave_value_list &args)
Definition: ov-classdef.h:265
OCTINTERP_API octave_value_list execute(octave::tree_evaluator &tw, int nargout, const octave_value_list &idx)
Definition: ov-classdef.cc:475
octave_classdef_superclass_ref(const std::string &meth, const std::string &cls)
Definition: ov-classdef.h:247
static int static_type_id(void)
Definition: ov-classdef.h:156
octave_base_value * empty_clone(void) const
Definition: ov-classdef.h:68
dim_vector dims(void) const
Definition: ov-classdef.h:131
bool is_defined(void) const
Definition: ov-classdef.h:79
octave_map map_value(void) const
Definition: ov-classdef.h:129
octave_classdef(const octave_classdef &)=delete
bool isobject(void) const
Definition: ov-classdef.h:83
void break_closure_cycles(const std::shared_ptr< octave::stack_frame > &frame)
Definition: ov-classdef.h:94
string_vector map_keys(void) const
Definition: ov-classdef.h:127
octave_value get_property(octave_idx_type idx, const std::string &name) const
Definition: ov-classdef.h:140
octave_classdef(const octave::cdef_object &obj)
Definition: ov-classdef.h:54
octave_base_value * clone(void) const
Definition: ov-classdef.h:63
~octave_classdef(void)=default
int type_id(void) const
Definition: ov-classdef.h:152
static std::string static_class_name(void)
Definition: ov-classdef.h:158
static const std::string t_name
Definition: ov-classdef.h:167
octave::cdef_object get_object(void) const
Definition: ov-classdef.h:75
bool isstruct(void) const
Definition: ov-classdef.h:81
octave::cdef_object & get_object_ref(void)
Definition: ov-classdef.h:77
octave_classdef(void)
Definition: ov-classdef.h:51
octave::cdef_object m_object
Definition: ov-classdef.h:163
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition: ov-classdef.h:103
std::string type_name(void) const
Definition: ov-classdef.h:153
bool is_classdef_object(void) const
Definition: ov-classdef.h:85
static int t_id
Definition: ov-classdef.h:165
static std::string static_type_name(void)
Definition: ov-classdef.h:157
void set_property(octave_idx_type idx, const std::string &name, const octave_value &pval)
Definition: ov-classdef.h:133
octave_classdef * classdef_object_value(bool=false)
Definition: ov-classdef.h:73
std::string class_name(void) const
Definition: ov-classdef.h:154
octave_idx_type length(void) const
Definition: ovl.h:113
type_info & operator=(const type_info &)=delete
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:771