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