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