GNU Octave  6.2.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-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_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 
40 namespace octave
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 
53  {
54  public:
55 
56  fcn_info_rep (const std::string& nm)
57  : name (nm), package_name (), local_functions (),
61  {
62  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  // No copying!
72 
73  fcn_info_rep (const fcn_info_rep&) = delete;
74 
76 
77  ~fcn_info_rep (void) = default;
78 
79  octave_value install_local_function (const std::string& file_name);
80 
81  octave_value load_private_function (const std::string& dir_name);
82 
84 
85  octave_value load_class_method (const std::string& dispatch_type);
86 
87  octave_value find (const symbol_scope& search_scope,
88  const octave_value_list& args);
89 
90  octave_value builtin_find (const symbol_scope& search_scope);
91 
92  octave_value find_scoped_function (const symbol_scope& search_scope);
93 
94  octave_value find_private_function (const std::string& dir_name);
95 
96  octave_value find_method (const std::string& dispatch_type);
97 
99 
101 
102  octave_value find_package (void);
103 
105 
106  bool is_user_function_defined (void) const
107  {
108  return function_on_path.is_defined ();
109  }
110 
112  const octave_value_list& args)
113  {
114  return find (search_scope, args);
115  }
116 
118  {
120  }
121 
123  const std::string& file_name)
124  {
125  local_functions[file_name] = f;
126  }
127 
129  {
131  }
132 
134  {
136  }
137 
138  void install_built_in_dispatch (const std::string& klass);
139 
140  template <typename T>
141  void
142  clear_map (std::map<T, octave_value>& map, bool force = false)
143  {
144  auto p = map.begin ();
145 
146  while (p != map.end ())
147  {
148  if (force || ! p->second.islocked ())
149  map.erase (p++);
150  else
151  p++;
152  }
153  }
154 
155  void clear_autoload_function (bool force = false)
156  {
157  if (force || ! autoload_function.islocked ())
159  }
160 
161  // We also clear command line functions here, as these are both
162  // "user defined"
163  void clear_user_function (bool force = false)
164  {
165  clear_autoload_function (force);
166 
167  if (force || ! function_on_path.islocked ())
169 
170  if (force || ! cmdline_function.islocked ())
172  }
173 
174  void clear_mex_function (void)
175  {
178  }
179 
180  void clear_package (void)
181  {
182  package = octave_value ();
183  }
184 
185  void clear (bool force = false)
186  {
187  clear_map (local_functions, force);
188  clear_map (private_functions, force);
189  clear_map (class_constructors, force);
190  clear_map (class_methods, force);
191 
192  clear_autoload_function (force);
193  clear_user_function (force);
194  clear_package ();
195  }
196 
197  octave_value dump (void) const;
198 
199  std::string full_name (void) const
200  {
201  if (package_name.empty ())
202  return name;
203  else
204  return package_name + '.' + name;
205  }
206 
207  std::string name;
208 
209  std::string package_name;
210 
211  // File name to function object.
212  std::map<std::string, octave_value> local_functions;
213 
214  // Directory name to function object.
215  std::map<std::string, octave_value> private_functions;
216 
217  // Class name to function object.
218  std::map<std::string, octave_value> class_constructors;
219 
220  // Dispatch type to function object.
221  std::map<std::string, octave_value> class_methods;
222 
224 
226 
228 
230 
232 
233  private:
234 
235  octave_value xfind (const symbol_scope& search_scope,
236  const octave_value_list& args);
237 
238  octave_value x_builtin_find (const symbol_scope& search_scope);
239  };
240 
241  public:
242 
243  fcn_info (const std::string& nm = "")
244  : m_rep (new fcn_info_rep (nm)) { }
245 
246  fcn_info (const fcn_info&) = default;
247 
248  fcn_info& operator = (const fcn_info&) = default;
249 
250  ~fcn_info (void) = default;
251 
252  octave_value find (const symbol_scope& search_scope,
253  const octave_value_list& args = octave_value_list ())
254  {
255  return m_rep->find (search_scope, args);
256  }
257 
259  builtin_find (const symbol_scope& search_scope)
260  {
261  return m_rep->builtin_find (search_scope);
262  }
263 
264  octave_value find_scoped_function (const symbol_scope& search_scope) const
265  {
266  return m_rep->find_scoped_function (search_scope);
267  }
268 
269  octave_value find_private_function (const std::string& dir_name) const
270  {
271  return m_rep->find_private_function (dir_name);
272  }
273 
274  octave_value find_method (const std::string& dispatch_type) const
275  {
276  return m_rep->find_method (dispatch_type);
277  }
278 
280  {
281  return m_rep->built_in_function;
282  }
283 
285  {
286  return m_rep->cmdline_function;
287  }
288 
290  {
291  return m_rep->find_autoload ();
292  }
293 
294  // FIXME: find_function_on_path might be a better name?
296  {
297  return m_rep->find_user_function ();
298  }
299 
300  bool is_user_function_defined (void) const
301  {
302  return m_rep->is_user_function_defined ();
303  }
304 
306  find_function (const symbol_scope& search_scope,
307  const octave_value_list& args = octave_value_list ())
308  {
309  return m_rep->find_function (search_scope, args);
310  }
311 
313  {
314  m_rep->install_cmdline_function (f);
315  }
316 
318  const std::string& file_name)
319  {
320  m_rep->install_local_function (f, file_name);
321  }
322 
324  {
325  m_rep->install_user_function (f);
326  }
327 
329  {
330  m_rep->install_built_in_function (f);
331  }
332 
333  void install_built_in_dispatch (const std::string& klass)
334  {
335  m_rep->install_built_in_dispatch (klass);
336  }
337 
338  void clear (bool force = false) { m_rep->clear (force); }
339 
340  void clear_user_function (bool force = false)
341  {
342  m_rep->clear_user_function (force);
343  }
344 
345  void clear_autoload_function (bool force = false)
346  {
347  m_rep->clear_autoload_function (force);
348  }
349 
350  void clear_mex_function (void) { m_rep->clear_mex_function (); }
351 
352  octave_value dump (void) const { return m_rep->dump (); }
353 
354  private:
355 
356  std::shared_ptr<fcn_info_rep> m_rep;
357  };
358 
359  extern OCTINTERP_API std::string
360  get_dispatch_type (const octave_value_list& args);
361 
362  extern OCTINTERP_API std::string
364  builtin_type_t& builtin_type);
365 
366  extern octave_value
367  dump_function_map (const std::map<std::string, octave_value>& fcn_map);
368 }
369 
370 #endif
void install_built_in_function(const octave_value &f)
Definition: fcn-info.h:133
octave_value load_private_function(const std::string &dir_name)
Definition: fcn-info.cc:53
std::map< std::string, octave_value > local_functions
Definition: fcn-info.h:212
octave_value x_builtin_find(const symbol_scope &search_scope)
Definition: fcn-info.cc:843
fcn_info_rep(const std::string &nm)
Definition: fcn-info.h:56
std::map< std::string, octave_value > class_constructors
Definition: fcn-info.h:218
bool is_user_function_defined(void) const
Definition: fcn-info.h:106
std::map< std::string, octave_value > class_methods
Definition: fcn-info.h:221
octave_value load_class_method(const std::string &dispatch_type)
Definition: fcn-info.cc:154
std::string full_name(void) const
Definition: fcn-info.h:199
octave_value find_autoload(void)
Definition: fcn-info.cc:973
octave_value autoload_function
Definition: fcn-info.h:225
void clear(bool force=false)
Definition: fcn-info.h:185
fcn_info_rep(const fcn_info_rep &)=delete
void install_local_function(const octave_value &f, const std::string &file_name)
Definition: fcn-info.h:122
void install_cmdline_function(const octave_value &f)
Definition: fcn-info.h:117
octave_value find_scoped_function(const symbol_scope &search_scope)
Definition: fcn-info.cc:628
void install_user_function(const octave_value &f)
Definition: fcn-info.h:128
octave_value dump(void) const
Definition: fcn-info.cc:1075
octave_value find_function(const symbol_scope &search_scope, const octave_value_list &args)
Definition: fcn-info.h:111
fcn_info_rep & operator=(const fcn_info_rep &)=delete
octave_value builtin_find(const symbol_scope &search_scope)
Definition: fcn-info.cc:818
void install_built_in_dispatch(const std::string &klass)
Definition: fcn-info.cc:1054
octave_value find_private_function(const std::string &dir_name)
Definition: fcn-info.cc:671
octave_value install_local_function(const std::string &file_name)
octave_value find_package(void)
Definition: fcn-info.cc:1036
octave_value find_user_function(void)
Definition: fcn-info.cc:1005
octave_value load_class_constructor(void)
Definition: fcn-info.cc:95
octave_value find(const symbol_scope &search_scope, const octave_value_list &args)
Definition: fcn-info.cc:354
void clear_map(std::map< T, octave_value > &map, bool force=false)
Definition: fcn-info.h:142
void clear_user_function(bool force=false)
Definition: fcn-info.h:163
octave_value built_in_function
Definition: fcn-info.h:231
std::map< std::string, octave_value > private_functions
Definition: fcn-info.h:215
octave_value xfind(const symbol_scope &search_scope, const octave_value_list &args)
Definition: fcn-info.cc:720
octave_value find_method(const std::string &dispatch_type)
Definition: fcn-info.cc:938
void clear_autoload_function(bool force=false)
Definition: fcn-info.h:155
bool is_user_function_defined(void) const
Definition: fcn-info.h:300
~fcn_info(void)=default
void install_local_function(const octave_value &f, const std::string &file_name)
Definition: fcn-info.h:317
octave_value find_function(const symbol_scope &search_scope, const octave_value_list &args=octave_value_list())
Definition: fcn-info.h:306
void clear_user_function(bool force=false)
Definition: fcn-info.h:340
octave_value dump(void) const
Definition: fcn-info.h:352
void clear_autoload_function(bool force=false)
Definition: fcn-info.h:345
octave_value find(const symbol_scope &search_scope, const octave_value_list &args=octave_value_list())
Definition: fcn-info.h:252
octave_value find_built_in_function(void) const
Definition: fcn-info.h:279
octave_value builtin_find(const symbol_scope &search_scope)
Definition: fcn-info.h:259
std::map< std::string, octave_value >::iterator str_val_iterator
Definition: fcn-info.h:48
void clear(bool force=false)
Definition: fcn-info.h:338
octave_value find_user_function(void)
Definition: fcn-info.h:295
void clear_mex_function(void)
Definition: fcn-info.h:350
fcn_info(const std::string &nm="")
Definition: fcn-info.h:243
void install_built_in_dispatch(const std::string &klass)
Definition: fcn-info.h:333
octave_value find_private_function(const std::string &dir_name) const
Definition: fcn-info.h:269
void install_user_function(const octave_value &f)
Definition: fcn-info.h:323
void install_built_in_function(const octave_value &f)
Definition: fcn-info.h:328
octave_value find_cmdline_function(void) const
Definition: fcn-info.h:284
std::shared_ptr< fcn_info_rep > m_rep
Definition: fcn-info.h:356
fcn_info(const fcn_info &)=default
octave_value find_autoload(void)
Definition: fcn-info.h:289
octave_value find_scoped_function(const symbol_scope &search_scope) const
Definition: fcn-info.h:264
std::map< std::string, octave_value >::const_iterator str_val_const_iterator
Definition: fcn-info.h:47
octave_value find_method(const std::string &dispatch_type) const
Definition: fcn-info.h:274
void install_cmdline_function(const octave_value &f)
Definition: fcn-info.h:312
bool is_defined(void) const
Definition: ov.h:551
bool islocked(void) const
Definition: ov.h:1358
bool is_mex_function(void) const
Definition: ov.h:748
static double f(double k, double l_nu, double c_pm)
Definition: randpoisson.cc:118
std::string get_dispatch_type(const octave_value_list &args, builtin_type_t &builtin_type)
Definition: fcn-info.cc:274
octave_value dump_function_map(const std::map< std::string, octave_value > &fcn_map)
Definition: fcn-info.cc:1093
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:72