GNU Octave 10.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
cdef-method.h
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2012-2025 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_cdef_method_h)
27#define octave_cdef_method_h 1
28
29#include "octave-config.h"
30
31#include <map>
32#include <set>
33#include <string>
34
35#include "oct-refcount.h"
36
37#include "cdef-object.h"
38#include "ov.h"
39
41
42class OCTINTERP_API cdef_method : public cdef_meta_object
43{
44 friend class cdef_class;
45
46private:
47
48 class cdef_method_rep : public cdef_meta_object_rep
49 {
50 public:
51
52 cdef_method_rep ()
53 : cdef_meta_object_rep (), m_function (), m_dispatch_type ()
54 { }
55
56 cdef_method_rep& operator = (const cdef_method_rep& m) = delete;
57
58 ~cdef_method_rep () = default;
59
60 cdef_object_rep * copy () const { return new cdef_method_rep(*this); }
61
62 bool is_method () const { return true; }
63
64 std::string get_name () const { return get("Name").string_value (); }
65
66 void set_name (const std::string& nm) { put ("Name", nm); }
67
68 bool is_static () const { return get("Static").bool_value (); }
69
70 octave_value get_function () const { return m_function; }
71
72 void set_function (const octave_value& fcn) { m_function = fcn; }
73
74 OCTINTERP_API std::string get_doc_string ();
75
76 OCTINTERP_API bool check_access () const;
77
78 bool is_external () const { return ! m_dispatch_type.empty (); }
79
80 void mark_as_external (const std::string& dtype)
81 {
82 m_dispatch_type = dtype;
83 }
84
85 OCTINTERP_API octave_value_list
86 execute (const octave_value_list& args, int nargout,
87 bool do_check_access = true, const std::string& who = "");
88
89 OCTINTERP_API octave_value_list
90 execute (const cdef_object& obj,
91 const octave_value_list& args, int nargout,
92 bool do_check_access = true, const std::string& who = "");
93
94 OCTINTERP_API bool is_constructor () const;
95
96 OCTINTERP_API bool is_defined_in_class (const std::string& cname) const;
97
99 meta_subsref (const std::string& type,
100 const std::list<octave_value_list>& idx, int nargout);
101
102 bool meta_accepts_postfix_index (char type) const
103 {
104 return (type == '(' || type == '.');
105 }
106
107 private:
108
109 cdef_method_rep (const cdef_method_rep& m)
110 : cdef_meta_object_rep (m), m_function (m.m_function),
111 m_dispatch_type (m.m_dispatch_type)
112 { }
113
114 OCTINTERP_API void check_method ();
115
116 cdef_method wrap ()
117 {
118 m_count++;
119 return cdef_method (this);
120 }
121
122 octave_value m_function;
123
124 // When non-empty, the method is externally defined and this member
125 // is used to cache the dispatch type to look for the method.
126
127 std::string m_dispatch_type;
128 };
129
130public:
131
133
134 cdef_method (const std::string& nm)
135 : cdef_meta_object (new cdef_method_rep ())
136 {
137 get_rep ()->set_name (nm);
138 }
139
140 cdef_method (const cdef_method& meth) : cdef_meta_object (meth) { }
141
143 : cdef_meta_object (obj)
144 {
145 // This should never happen...
146 if (! is_method ())
147 error ("internal error: invalid assignment from %s to meta.method object",
148 class_name ().c_str ());
149 }
150
151 cdef_method& operator = (const cdef_method& meth)
152 {
154
155 return *this;
156 }
157
158 ~cdef_method () = default;
159
160 // normal invocation
161 octave_value_list execute (const octave_value_list& args, int nargout,
162 bool do_check_access = true,
163 const std::string& who = "")
164 {
165 return get_rep ()->execute (args, nargout, do_check_access, who);
166 }
167
168 // dot-invocation: object is pushed as 1st argument
170 const octave_value_list& args, int nargout,
171 bool do_check_access = true,
172 const std::string& who = "")
173 {
174 return get_rep ()->execute (obj, args, nargout, do_check_access, who);
175 }
176
177 bool check_access () const { return get_rep ()->check_access (); }
178
179 std::string get_name () const { return get_rep ()->get_name (); }
180
181 bool is_static () const { return get_rep ()->is_static (); }
182
183 void set_function (const octave_value& fcn)
184 {
185 get_rep ()->set_function (fcn);
186 }
187
189 {
190 return get_rep ()->get_function ();
191 }
192
193 std::string get_doc_string ()
194 {
195 return get_rep ()->get_doc_string ();
196 }
197
198 bool is_constructor () const
199 {
200 return get_rep ()->is_constructor ();
201 }
202
203 bool is_defined_in_class (const std::string& cname) const
204 {
205 return get_rep ()->is_defined_in_class (cname);
206 }
207
208 bool is_external () const { return get_rep ()->is_external (); }
209
210 void mark_as_external (const std::string& dtype)
211 {
212 get_rep ()->mark_as_external (dtype);
213 }
214
215private:
216
217 cdef_method_rep * get_rep ()
218 {
219 return dynamic_cast<cdef_method_rep *> (cdef_object::get_rep ());
220 }
221
222 const cdef_method_rep * get_rep () const
223 {
224 return dynamic_cast<const cdef_method_rep *> (cdef_object::get_rep ());
225 }
226};
227
228OCTAVE_END_NAMESPACE(octave)
229
230#endif
bool check_access(const cdef_class &cls, const octave_value &acc, const std::string &meth_name, const std::string &prop_name, bool is_prop_set)
cdef_meta_object_rep & operator=(const cdef_meta_object_rep &)=delete
octave_value_list execute(const cdef_object &obj, const octave_value_list &args, int nargout, bool do_check_access=true, const std::string &who="")
bool is_constructor() const
bool is_external() const
cdef_method(const std::string &nm)
cdef_method(const cdef_method &meth)
bool check_access() const
octave_value_list execute(const octave_value_list &args, int nargout, bool do_check_access=true, const std::string &who="")
~cdef_method()=default
bool is_static() const
bool is_defined_in_class(const std::string &cname) const
octave_value get_function() const
void set_function(const octave_value &fcn)
std::string get_doc_string()
std::string get_name() const
cdef_method(const cdef_object &obj)
void mark_as_external(const std::string &dtype)
cdef_object & operator=(const cdef_object &obj)
const cdef_object_rep * get_rep() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition error.cc:1003