GNU Octave  8.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-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_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
52  {
53  public:
54 
56  : cdef_meta_object_rep (), m_function (), m_dispatch_type ()
57  { }
58 
60 
61  ~cdef_method_rep (void) = default;
62 
63  cdef_object_rep * copy (void) const { return new cdef_method_rep(*this); }
64 
65  bool is_method (void) const { return true; }
66 
67  std::string get_name (void) const { return get("Name").string_value (); }
68 
69  void set_name (const std::string& nm) { put ("Name", nm); }
70 
71  bool is_static (void) const { return get("Static").bool_value (); }
72 
73  octave_value get_function (void) 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 (void);
78 
79  OCTINTERP_API bool check_access (void) const;
80 
81  bool is_external (void) 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 (void) 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 
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 (void);
118 
120  {
121  m_count++;
122  return cdef_method (this);
123  }
124 
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)
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 (void) = 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 (void) const { return get_rep ()->check_access (); }
181 
182  std::string get_name (void) const { return get_rep ()->get_name (); }
183 
184  bool is_static (void) 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 (void)
197  {
198  return get_rep ()->get_doc_string ();
199  }
200 
201  bool is_constructor (void) 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 (void) 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 
221  {
222  return dynamic_cast<cdef_method_rep *> (cdef_object::get_rep ());
223  }
224 
225  const cdef_method_rep * get_rep (void) const
226  {
227  return dynamic_cast<const cdef_method_rep *> (cdef_object::get_rep ());
228  }
229 };
230 
232 
233 #endif
OCTAVE_END_NAMESPACE(octave)
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
std::string get_name(void) const
Definition: cdef-class.h:76
void set_name(const std::string &nm)
Definition: cdef-class.h:79
cdef_class_rep * get_rep(void)
Definition: cdef-class.h:420
cdef_class & operator=(const cdef_class &cls)
Definition: cdef-class.h:260
bool is_method(void) const
Definition: cdef-object.h:684
octave_value_list meta_subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
Definition: cdef-object.h:689
void set_function(const octave_value &fcn)
Definition: cdef-method.h:75
bool meta_accepts_postfix_index(char type) const
Definition: cdef-method.h:105
cdef_method_rep(const cdef_method_rep &m)
Definition: cdef-method.h:112
bool is_method(void) const
Definition: cdef-method.h:65
bool is_external(void) const
Definition: cdef-method.h:81
cdef_object_rep * copy(void) const
Definition: cdef-method.h:63
bool is_static(void) const
Definition: cdef-method.h:71
void mark_as_external(const std::string &dtype)
Definition: cdef-method.h:83
void set_name(const std::string &nm)
Definition: cdef-method.h:69
octave_value get_function(void) const
Definition: cdef-method.h:73
std::string get_name(void) const
Definition: cdef-method.h:67
cdef_method wrap(void)
Definition: cdef-method.h:119
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
~cdef_method(void)=default
cdef_method(const std::string &nm)
Definition: cdef-method.h:137
cdef_method(const cdef_method &meth)
Definition: cdef-method.h:143
bool is_constructor(void) const
Definition: cdef-method.h:201
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_rep * get_rep(void)
Definition: cdef-method.h:220
bool check_access(void) const
Definition: cdef-method.h:180
bool is_defined_in_class(const std::string &cname) const
Definition: cdef-method.h:206
bool is_static(void) const
Definition: cdef-method.h:184
octave_value get_function(void) const
Definition: cdef-method.h:191
void set_function(const octave_value &fcn)
Definition: cdef-method.h:186
cdef_method(void)
Definition: cdef-method.h:135
const cdef_method_rep * get_rep(void) const
Definition: cdef-method.h:225
std::string get_doc_string(void)
Definition: cdef-method.h:196
bool is_external(void) const
Definition: cdef-method.h:211
std::string get_name(void) 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
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
const cdef_object_rep * get_rep(void) const
Definition: cdef-object.h:310
std::string class_name(void) const
Definition: cdef-object.h:234
octave_value get(const std::string &pname) const
Definition: cdef-object.h:268
bool bool_value(bool warn=false) const
Definition: ov.h:930
std::string string_value(bool force=false) const
Definition: ov.h:1019
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition: error.cc:979
T octave_idx_type m
Definition: mx-inlines.cc:773