GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
fcn-info.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1993-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_fcn_info_h)
27 #define octave_fcn_info_h 1
28 
29 #include "octave-config.h"
30 
31 #include <list>
32 #include <map>
33 #include <memory>
34 #include <string>
35 
36 #include "ov.h"
37 #include "ovl.h"
38 #include "symscope.h"
39 
41 
42 class fcn_info
43 {
44 public:
45 
46  typedef std::map<std::string, octave_value>::const_iterator
48  typedef std::map<std::string, octave_value>::iterator str_val_iterator;
49 
50 private:
51 
52  class fcn_info_rep
53  {
54  public:
55 
56  fcn_info_rep (const std::string& nm)
57  : name (nm), package_name (), local_functions (),
58  private_functions (), class_constructors (), class_methods (),
59  cmdline_function (), autoload_function (), function_on_path (),
60  built_in_function ()
61  {
62  std::size_t pos = name.rfind ('.');
63 
64  if (pos != std::string::npos)
65  {
66  package_name = name.substr (0, pos);
67  name = name.substr (pos+1);
68  }
69  }
70 
71  OCTAVE_DISABLE_CONSTRUCT_COPY_MOVE (fcn_info_rep)
72 
73  ~fcn_info_rep () = default;
74 
75  octave_value install_local_function (const std::string& file_name);
76 
77  octave_value load_private_function (const std::string& dir_name);
78 
79  octave_value load_class_constructor ();
80 
81  octave_value load_class_method (const std::string& dispatch_type);
82 
83  octave_value find (const symbol_scope& search_scope,
84  const octave_value_list& args);
85 
86  octave_value builtin_find (const symbol_scope& search_scope);
87 
88  octave_value find_scoped_function (const symbol_scope& search_scope);
89 
90  octave_value find_private_function (const std::string& dir_name);
91 
92  octave_value find_method (const std::string& dispatch_type);
93 
94  octave_value find_method (const octave_value_list& args);
95 
96  octave_value find_autoload ();
97 
98  octave_value find_package ();
99 
100  octave_value find_user_function ();
101 
102  bool is_user_function_defined () const
103  {
104  return function_on_path.is_defined ();
105  }
106 
107  octave_value find_function (const symbol_scope& search_scope,
108  const octave_value_list& args)
109  {
110  return find (search_scope, args);
111  }
112 
113  void install_cmdline_function (const octave_value& f)
114  {
115  cmdline_function = f;
116  }
117 
118  void install_local_function (const octave_value& f,
119  const std::string& file_name)
120  {
121  local_functions[file_name] = f;
122  }
123 
124  void install_user_function (const octave_value& f)
125  {
126  function_on_path = f;
127  }
128 
129  void install_built_in_function (const octave_value& f)
130  {
131  built_in_function = f;
132  }
133 
134  void install_built_in_dispatch (const std::string& klass);
135 
136  template <typename T>
137  void
138  clear_map (std::map<T, octave_value>& map, bool force = false)
139  {
140  auto p = map.begin ();
141 
142  while (p != map.end ())
143  {
144  if (force || ! p->second.islocked ())
145  map.erase (p++);
146  else
147  p++;
148  }
149  }
150 
151  void clear_autoload_function (bool force = false)
152  {
153  if (force || ! autoload_function.islocked ())
154  autoload_function = octave_value ();
155  }
156 
157  // We also clear command line functions here, as these are both
158  // "user defined"
159  void clear_user_function (bool force = false)
160  {
161  clear_autoload_function (force);
162 
163  if (force || ! function_on_path.islocked ())
164  function_on_path = octave_value ();
165 
166  if (force || ! cmdline_function.islocked ())
167  cmdline_function = octave_value ();
168  }
169 
170  void clear_mex_function ()
171  {
172  if (function_on_path.is_mex_function ())
173  clear_user_function ();
174  }
175 
176  void clear_package ()
177  {
178  package = octave_value ();
179  }
180 
181  void clear (bool force = false)
182  {
183  clear_map (local_functions, force);
184  clear_map (private_functions, force);
185  clear_map (class_constructors, force);
186  clear_map (class_methods, force);
187 
188  clear_autoload_function (force);
189  clear_user_function (force);
190  clear_package ();
191  }
192 
193  octave_value dump () const;
194 
195  std::string full_name () const
196  {
197  if (package_name.empty ())
198  return name;
199  else
200  return package_name + '.' + name;
201  }
202 
203  std::string name;
204 
205  std::string package_name;
206 
207  // File name to function object.
208  std::map<std::string, octave_value> local_functions;
209 
210  // Directory name to function object.
211  std::map<std::string, octave_value> private_functions;
212 
213  // Class name to function object.
214  std::map<std::string, octave_value> class_constructors;
215 
216  // Dispatch type to function object.
217  std::map<std::string, octave_value> class_methods;
218 
219  octave_value cmdline_function;
220 
221  octave_value autoload_function;
222 
223  octave_value function_on_path;
224 
225  octave_value package;
226 
227  octave_value built_in_function;
228 
229  private:
230 
231  octave_value xfind (const symbol_scope& search_scope,
232  const octave_value_list& args);
233 
234  octave_value x_builtin_find (const symbol_scope& search_scope);
235  };
236 
237 public:
238 
239  fcn_info (const std::string& nm = "")
240  : m_rep (new fcn_info_rep (nm)) { }
241 
242  fcn_info (const fcn_info&) = default;
243 
244  fcn_info& operator = (const fcn_info&) = default;
245 
246  ~fcn_info () = default;
247 
248  octave_value find (const symbol_scope& search_scope,
249  const octave_value_list& args = octave_value_list ())
250  {
251  return m_rep->find (search_scope, args);
252  }
253 
255  builtin_find (const symbol_scope& search_scope)
256  {
257  return m_rep->builtin_find (search_scope);
258  }
259 
260  octave_value find_scoped_function (const symbol_scope& search_scope) const
261  {
262  return m_rep->find_scoped_function (search_scope);
263  }
264 
265  octave_value find_private_function (const std::string& dir_name) const
266  {
267  return m_rep->find_private_function (dir_name);
268  }
269 
270  octave_value find_method (const std::string& dispatch_type) const
271  {
272  return m_rep->find_method (dispatch_type);
273  }
274 
276  {
277  return m_rep->built_in_function;
278  }
279 
281  {
282  return m_rep->cmdline_function;
283  }
284 
286  {
287  return m_rep->find_autoload ();
288  }
289 
290  // FIXME: find_function_on_path might be a better name?
292  {
293  return m_rep->find_user_function ();
294  }
295 
297  {
298  return m_rep->is_user_function_defined ();
299  }
300 
302  find_function (const symbol_scope& search_scope,
303  const octave_value_list& args = octave_value_list ())
304  {
305  return m_rep->find_function (search_scope, args);
306  }
307 
309  {
310  m_rep->install_cmdline_function (f);
311  }
312 
314  const std::string& file_name)
315  {
316  m_rep->install_local_function (f, file_name);
317  }
318 
320  {
321  m_rep->install_user_function (f);
322  }
323 
325  {
326  m_rep->install_built_in_function (f);
327  }
328 
329  void install_built_in_dispatch (const std::string& klass)
330  {
331  m_rep->install_built_in_dispatch (klass);
332  }
333 
334  void clear (bool force = false) { m_rep->clear (force); }
335 
336  void clear_user_function (bool force = false)
337  {
338  m_rep->clear_user_function (force);
339  }
340 
341  void clear_autoload_function (bool force = false)
342  {
343  m_rep->clear_autoload_function (force);
344  }
345 
346  void clear_mex_function () { m_rep->clear_mex_function (); }
347 
348  octave_value dump () const { return m_rep->dump (); }
349 
350 private:
351 
352  std::shared_ptr<fcn_info_rep> m_rep;
353 };
354 
355 extern OCTINTERP_API std::string
357 
358 extern OCTINTERP_API std::string
360  builtin_type_t& builtin_type);
361 
362 extern octave_value
363 dump_function_map (const std::map<std::string, octave_value>& fcn_map);
364 
365 OCTAVE_END_NAMESPACE(octave)
366 
367 #endif
void clear_mex_function()
Definition: fcn-info.h:346
octave_value find_method(const std::string &dispatch_type) const
Definition: fcn-info.h:270
fcn_info(const fcn_info &)=default
octave_value find_user_function()
Definition: fcn-info.h:291
octave_value find_scoped_function(const symbol_scope &search_scope) const
Definition: fcn-info.h:260
octave_value find_function(const symbol_scope &search_scope, const octave_value_list &args=octave_value_list())
Definition: fcn-info.h:302
std::map< std::string, octave_value >::const_iterator str_val_const_iterator
Definition: fcn-info.h:47
void install_built_in_dispatch(const std::string &klass)
Definition: fcn-info.h:329
octave_value builtin_find(const symbol_scope &search_scope)
Definition: fcn-info.h:255
void clear(bool force=false)
Definition: fcn-info.h:334
octave_value dump() const
Definition: fcn-info.h:348
octave_value find_built_in_function() const
Definition: fcn-info.h:275
void clear_autoload_function(bool force=false)
Definition: fcn-info.h:341
octave_value find_autoload()
Definition: fcn-info.h:285
octave_value find_cmdline_function() const
Definition: fcn-info.h:280
void install_user_function(const octave_value &f)
Definition: fcn-info.h:319
void install_local_function(const octave_value &f, const std::string &file_name)
Definition: fcn-info.h:313
void install_cmdline_function(const octave_value &f)
Definition: fcn-info.h:308
octave_value find(const symbol_scope &search_scope, const octave_value_list &args=octave_value_list())
Definition: fcn-info.h:248
~fcn_info()=default
void clear_user_function(bool force=false)
Definition: fcn-info.h:336
fcn_info(const std::string &nm="")
Definition: fcn-info.h:239
octave_value find_private_function(const std::string &dir_name) const
Definition: fcn-info.h:265
std::map< std::string, octave_value >::iterator str_val_iterator
Definition: fcn-info.h:48
void install_built_in_function(const octave_value &f)
Definition: fcn-info.h:324
bool is_user_function_defined() const
Definition: fcn-info.h:296
octave_value dump() const
Definition: ov.h:1439
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::string get_dispatch_type(const octave_value_list &args)
octave_value dump_function_map(const std::map< std::string, octave_value > &fcn_map)
F77_RET_T const F77_DBLE const F77_DBLE * f
return octave_value(v1.char_array_value() . concat(v2.char_array_value(), ra_idx),((a1.is_sq_string()||a2.is_sq_string()) ? '\'' :'"'))
builtin_type_t
Definition: ov-base.h:83