GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
ov-classdef.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2012-2026 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
39class cdef_object;
40class interpreter;
41class tree_evaluator;
42class type_info;
43
44OCTAVE_END_NAMESPACE(octave)
45
47{
48public:
49
51 : octave_base_value (), m_object () { }
52
53 octave_classdef (const octave::cdef_object& obj)
54 : octave_base_value (), m_object (obj) { }
55
56 OCTAVE_DISABLE_COPY_MOVE (octave_classdef)
57
58 ~octave_classdef () = default;
59
61 {
62 return new octave_classdef (m_object.clone ());
63 }
64
66 {
67 return new octave_classdef (m_object.empty_clone ());
68 }
69
70 octave_classdef * classdef_object_value (bool = false) { return this; }
71
72 octave::cdef_object get_object () const { return m_object; }
73
74 octave::cdef_object& get_object_ref () { return m_object; }
75
76 bool is_defined () const { return true; }
77
78 bool isstruct () const { return false; }
79
80 bool isobject () const { return true; }
81
82 bool is_classdef_object () const { return true; }
83
84 OCTINTERP_API void print (std::ostream& os, bool pr_as_read_syntax = false);
85
86 OCTINTERP_API void
87 print_raw (std::ostream& os, bool pr_as_read_syntax = false) const;
88
89 OCTINTERP_API bool is_instance_of (const std::string& cls_name) const;
90
91 void break_closure_cycles (const std::shared_ptr<octave::stack_frame>& frame)
92 {
93 m_object.break_closure_cycles (frame);
94 }
95
96 OCTINTERP_API octave_value_list
97 subsref (const std::string& type, const std::list<octave_value_list>& idx,
98 int nargout);
99
100 octave_value subsref (const std::string& type,
101 const std::list<octave_value_list>& idx)
102 {
103 octave_value_list retval = subsref (type, idx, 1);
104 return (retval.length () > 0 ? retval(0) : octave_value ());
105 }
106
107 OCTINTERP_API octave_value
108 subsref (const std::string& type, const std::list<octave_value_list>& idx,
109 bool auto_add);
110
111 OCTINTERP_API octave_value
112 subsasgn (const std::string& type, const std::list<octave_value_list>& idx,
113 const octave_value& rhs);
114
115 OCTINTERP_API octave_value
116 undef_subsasgn (const std::string& type,
117 const std::list<octave_value_list>& idx,
118 const octave_value& rhs);
119
120 OCTINTERP_API Matrix size ();
121
122 OCTINTERP_API octave_value reshape (const dim_vector& new_dims) const;
123
124 OCTINTERP_API octave_idx_type xnumel (const octave_value_list&);
125
126 string_vector map_keys () const { return m_object.map_keys (); }
127
128 octave_map map_value () const { return m_object.map_value (true); }
129
130 octave_map map_value (bool warn) const { return m_object.map_value (warn); }
131
132 octave_map map_value (bool warn, bool for_save) const
133 { return m_object.map_value (warn, for_save); }
134
135 dim_vector dims () const { return m_object.dims (); }
136
137 void set_property (octave_idx_type idx, const std::string& name,
138 const octave_value& pval)
139 {
140 m_object.set_property (idx, name, pval);
141 }
142
144 get_property (octave_idx_type idx, const std::string& name) const
145 {
146 return m_object.get_property (idx, name);
147 }
148
149 static OCTINTERP_API octave_value
150 superclass_ref (const std::string& meth, const std::string& cls);
151
152 static OCTINTERP_API octave_value metaclass_query (const std::string& cls);
153
154 OCTINTERP_API bool save_ascii (std::ostream& os);
155
156 OCTINTERP_API bool load_ascii (std::istream& is);
157
159 permute (const Array<int>& vec, bool inv = false) const
160 {
161 return new octave_classdef (m_object.permute (vec, inv));
162 }
163
165 resize (const dim_vector& dv, bool fill = false) const
166 {
167 return new octave_classdef (m_object.resize (dv, fill));
168 }
169
170public:
171
172 int type_id () const { return s_t_id; }
173 std::string type_name () const { return s_t_name; }
174 std::string class_name () const { return m_object.class_name (); }
175
176 static int static_type_id () { return s_t_id; }
177 static std::string static_type_name () { return s_t_name; }
178 static std::string static_class_name () { return "<unknown>"; }
179 static OCTINTERP_API void register_type (octave::type_info&);
180
181 // Load an array of the size dv using a vector with
182 // * a map with the values of the class properties
183 // * a unique identifier of the object in the file
184 // * an indicator whether the object has a custom return type
185 OCTINTERP_API void
186 loadobj (std::vector<std::tuple<octave_map, uint32_t, bool>>& m,
187 dim_vector& dv);
188
189 // Return a vector for each element in the array containing:
190 // * a map with the values of the class properties
191 // * a unique identifier of the object in the file
192 // * an indicator whether the object has a custom return type
193 OCTINTERP_API std::vector<std::tuple<octave_map, uint32_t, bool>>
194 saveobj (std::vector<bool>& is_new);
195
196private:
197
198 octave::cdef_object m_object;
199
200 static int s_t_id;
201
202 static const std::string s_t_name;
203};
204
205void install_classdef (octave::interpreter& interp);
206
208{
209public:
210
211 octave_classdef_meta (const octave::cdef_meta_object& obj)
212 : m_object (obj)
213 { }
214
215 OCTAVE_DISABLE_COPY_MOVE (octave_classdef_meta)
216
217 ~octave_classdef_meta () { m_object.meta_release (); }
218
219 bool is_classdef_meta () const { return true; }
220
221 bool is_package () const { return m_object.is_package(); }
222
223 octave_function * function_value (bool = false) { return this; }
224
225 // We don't need to override all three forms of subsref. The using
226 // declaration will avoid warnings about partially-overloaded virtual
227 // functions.
229
231 subsref (const std::string& type,
232 const std::list<octave_value_list>& idx,
233 int nargout)
234 {
235 return m_object.meta_subsref (type, idx, nargout);
236 }
237
238 // Override default call method because we don't push a new stack
239 // frame for this operation on classdef_meta objects.
240
241 octave_value_list call (octave::tree_evaluator& tw, int nargout,
242 const octave_value_list& args)
243 {
244 return execute (tw, nargout, args);
245 }
246
247 octave_value_list execute (octave::tree_evaluator&, int nargout,
248 const octave_value_list& args)
249 {
250 // Emulate ()-type meta subsref
251
252 std::list<octave_value_list> idx (1, args);
253 std::string type ("(");
254
255 return subsref (type, idx, nargout);
256 }
257
258 bool accepts_postfix_index (char type) const
259 { return m_object.meta_accepts_postfix_index (type); }
260
261 OCTINTERP_API bool is_classdef_method (const std::string& cname = "") const;
262
263 OCTINTERP_API bool
264 is_classdef_constructor (const std::string& cname = "") const;
265
266 OCTINTERP_API std::string doc_string (const std::string& meth_name) const;
267
268 OCTINTERP_API std::string file_name () const;
269
270private:
271
272 octave::cdef_meta_object m_object;
273};
274
276{
277public:
279
280 octave_classdef_superclass_ref (const std::string& meth,
281 const std::string& cls)
282 : octave_function (), m_method_name (meth), m_class_name (cls)
283 { }
284
285 OCTAVE_DISABLE_COPY_MOVE (octave_classdef_superclass_ref)
286
288
289 bool is_classdef_superclass_ref () const { return true; }
290
291 octave_function * function_value (bool = false) { return this; }
292
293 // Override default call method because we don't push a new stack
294 // frame for this operation on classdef_superclass_ref objects.
295
296 octave_value_list call (octave::tree_evaluator& tw, int nargout,
297 const octave_value_list& args)
298 {
299 return execute (tw, nargout, args);
300 }
301
302 OCTINTERP_API octave_value_list
303 execute (octave::tree_evaluator& tw, int nargout,
304 const octave_value_list& idx);
305
306private:
307
308 OCTINTERP_API bool
309 is_constructed_object (octave::tree_evaluator& tw, const std::string& nm);
310
311private:
312
313 std::string m_method_name;
314 std::string m_class_name;
315};
316
317#endif
N Dimensional Array with copy-on-write semantics.
Definition Array-base.h:130
Vector representing the dimensions (size) of an Array.
Definition dim-vector.h:92
virtual octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
Definition ov-base.cc:245
octave_value_list call(octave::tree_evaluator &tw, int nargout, const octave_value_list &args)
octave_value_list subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
bool is_classdef_method(const std::string &cname="") const
bool accepts_postfix_index(char type) const
octave_classdef_meta(const octave::cdef_meta_object &obj)
std::string file_name() const
bool is_classdef_constructor(const std::string &cname="") const
std::string doc_string(const std::string &meth_name) const
octave_function * function_value(bool=false)
bool is_classdef_meta() const
bool is_package() const
octave_value_list execute(octave::tree_evaluator &, int nargout, const octave_value_list &args)
bool is_classdef_superclass_ref() const
octave_value_list call(octave::tree_evaluator &tw, int nargout, const octave_value_list &args)
octave_value_list execute(octave::tree_evaluator &tw, int nargout, const octave_value_list &idx)
octave_function * function_value(bool=false)
octave_classdef_superclass_ref(const std::string &meth, const std::string &cls)
octave_classdef * classdef_object_value(bool=false)
Definition ov-classdef.h:70
octave_base_value * empty_clone() const
Definition ov-classdef.h:65
static std::string static_class_name()
octave_map map_value() const
void break_closure_cycles(const std::shared_ptr< octave::stack_frame > &frame)
Definition ov-classdef.h:91
octave_value get_property(octave_idx_type idx, const std::string &name) const
dim_vector dims() const
octave_classdef(const octave::cdef_object &obj)
Definition ov-classdef.h:53
octave::cdef_object & get_object_ref()
Definition ov-classdef.h:74
octave_base_value * clone() const
Definition ov-classdef.h:60
octave_value permute(const Array< int > &vec, bool inv=false) const
bool isstruct() const
Definition ov-classdef.h:78
bool is_classdef_object() const
Definition ov-classdef.h:82
octave_map map_value(bool warn, bool for_save) const
octave::cdef_object get_object() const
Definition ov-classdef.h:72
static std::string static_type_name()
bool is_defined() const
Definition ov-classdef.h:76
static int static_type_id()
octave_value subsref(const std::string &type, const std::list< octave_value_list > &idx)
octave_value resize(const dim_vector &dv, bool fill=false) const
std::string type_name() const
std::string class_name() const
octave_map map_value(bool warn) const
void set_property(octave_idx_type idx, const std::string &name, const octave_value &pval)
string_vector map_keys() const
bool isobject() const
Definition ov-classdef.h:80
~octave_classdef()=default
int type_id() const
octave_idx_type length() const
Definition ovl.h:111
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)