GNU Octave  4.4.1
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-2018 John W. Eaton
4 Copyright (C) 2009 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if ! defined (octave_fcn_info_h)
25 #define octave_fcn_info_h 1
26 
27 #include "octave-config.h"
28 
29 #include <list>
30 #include <map>
31 #include <memory>
32 #include <string>
33 
34 #include "ov.h"
35 #include "ovl.h"
36 
37 namespace octave
38 {
39  class fcn_info
40  {
41  public:
42 
43  typedef std::map<std::string, octave_value>::const_iterator
45  typedef std::map<std::string, octave_value>::iterator str_val_iterator;
46 
47  private:
48 
50  {
51  public:
52 
54  : name (nm), package_name (), local_functions (),
58  {
59  size_t pos = name.rfind ('.');
60 
61  if (pos != std::string::npos)
62  {
63  package_name = name.substr (0, pos);
64  name = name.substr (pos+1);
65  }
66  }
67 
68  // No copying!
69 
70  fcn_info_rep (const fcn_info_rep&) = delete;
71 
72  fcn_info_rep& operator = (const fcn_info_rep&) = delete;
73 
74  ~fcn_info_rep (void) = default;
75 
77 
79 
81 
82  octave_value load_class_method (const std::string& dispatch_type);
83 
84  octave_value find (const octave_value_list& args, bool local_funcs);
85 
87 
88  octave_value find_method (const std::string& dispatch_type);
89 
91 
93 
95 
96  bool is_user_function_defined (void) const
97  {
98  return function_on_path.is_defined ();
99  }
100 
102  bool local_funcs)
103  {
104  return find (args, local_funcs);
105  }
106 
108  {
110  }
111 
113  const std::string& file_name)
114  {
115  local_functions[file_name] = f;
116  }
117 
119  {
121  }
122 
124  {
126  }
127 
128  void install_built_in_dispatch (const std::string& klass);
129 
130  template <typename T>
131  void
132  clear_map (std::map<T, octave_value>& map, bool force = false)
133  {
134  typename std::map<T, octave_value>::iterator p = map.begin ();
135 
136  while (p != map.end ())
137  {
138  if (force || ! p->second.islocked ())
139  map.erase (p++);
140  else
141  p++;
142  }
143  }
144 
145  void clear_autoload_function (bool force = false)
146  {
147  if (force || ! autoload_function.islocked ())
149  }
150 
151  // We also clear command line functions here, as these are both
152  // "user defined"
153  void clear_user_function (bool force = false)
154  {
155  clear_autoload_function (force);
156 
157  if (force || ! function_on_path.islocked ())
159 
160  if (force || ! cmdline_function.islocked ())
162  }
163 
164  void clear_mex_function (void)
165  {
168  }
169 
170  void clear_package (void)
171  {
172  package = octave_value ();
173  }
174 
175  void clear (bool force = false)
176  {
177  clear_map (local_functions, force);
178  clear_map (private_functions, force);
179  clear_map (class_constructors, force);
180  clear_map (class_methods, force);
181 
182  clear_autoload_function (force);
183  clear_user_function (force);
184  clear_package ();
185  }
186 
187  octave_value dump (void) const;
188 
189  std::string full_name (void) const
190  {
191  if (package_name.empty ())
192  return name;
193  else
194  return package_name + '.' + name;
195  }
196 
198 
200 
201  // File name to function object.
202  std::map<std::string, octave_value> local_functions;
203 
204  // Directory name to function object.
205  std::map<std::string, octave_value> private_functions;
206 
207  // Class name to function object.
208  std::map<std::string, octave_value> class_constructors;
209 
210  // Dispatch type to function object.
211  std::map<std::string, octave_value> class_methods;
212 
214 
216 
218 
220 
222 
223  private:
224 
225  octave_value xfind (const octave_value_list& args, bool local_funcs);
226 
228  };
229 
230  public:
231 
232  fcn_info (const std::string& nm = "")
233  : m_rep (new fcn_info_rep (nm)) { }
234 
235  fcn_info (const fcn_info&) = default;
236 
237  fcn_info& operator = (const fcn_info&) = default;
238 
239  ~fcn_info (void) = default;
240 
242  bool local_funcs = true)
243  {
244  return m_rep->find (args, local_funcs);
245  }
246 
248  {
249  return m_rep->builtin_find ();
250  }
251 
252  octave_value find_method (const std::string& dispatch_type) const
253  {
254  return m_rep->find_method (dispatch_type);
255  }
256 
258  {
259  return m_rep->built_in_function;
260  }
261 
263  {
264  return m_rep->cmdline_function;
265  }
266 
268  {
269  return m_rep->find_autoload ();
270  }
271 
273  {
274  return m_rep->find_user_function ();
275  }
276 
277  bool is_user_function_defined (void) const
278  {
279  return m_rep->is_user_function_defined ();
280  }
281 
283  = octave_value_list (),
284  bool local_funcs = true)
285  {
286  return m_rep->find_function (args, local_funcs);
287  }
288 
290  {
291  m_rep->install_cmdline_function (f);
292  }
293 
295  const std::string& file_name)
296  {
297  m_rep->install_local_function (f, file_name);
298  }
299 
301  {
302  m_rep->install_user_function (f);
303  }
304 
306  {
307  m_rep->install_built_in_function (f);
308  }
309 
311  {
312  m_rep->install_built_in_dispatch (klass);
313  }
314 
315  void clear (bool force = false) { m_rep->clear (force); }
316 
317  void clear_user_function (bool force = false)
318  {
319  m_rep->clear_user_function (force);
320  }
321 
322  void clear_autoload_function (bool force = false)
323  {
324  m_rep->clear_autoload_function (force);
325  }
326 
327  void clear_mex_function (void) { m_rep->clear_mex_function (); }
328 
329  octave_value dump (void) const { return m_rep->dump (); }
330 
331  private:
332 
333  std::shared_ptr<fcn_info_rep> m_rep;
334  };
335 
337  dump_function_map (const std::map<std::string, octave_value>& fcn_map);
338 }
339 
340 #endif
fcn_info_rep(const std::string &nm)
Definition: fcn-info.h:53
std::shared_ptr< fcn_info_rep > m_rep
Definition: fcn-info.h:333
void install_cmdline_function(const octave_value &f)
Definition: fcn-info.h:107
octave_value find(const octave_value_list &args, bool local_funcs)
Definition: fcn-info.cc:348
octave_value builtin_find(void)
Definition: fcn-info.cc:544
octave_value x_builtin_find(void)
Definition: fcn-info.cc:565
octave_value dump_function_map(const std::map< std::string, octave_value > &fcn_map)
Definition: fcn-info.cc:825
octave_value autoload_function
Definition: fcn-info.h:215
octave_value load_class_constructor(void)
Definition: fcn-info.cc:86
octave_value find_function(const octave_value_list &args, bool local_funcs)
Definition: fcn-info.h:101
void install_built_in_function(const octave_value &f)
Definition: fcn-info.h:123
octave_value load_class_method(const std::string &dispatch_type)
Definition: fcn-info.cc:145
octave_value dump(void) const
Definition: fcn-info.cc:807
void clear_map(std::map< T, octave_value > &map, bool force=false)
Definition: fcn-info.h:132
bool is_mex_function(void) const
Definition: ov.h:776
std::map< std::string, octave_value > private_functions
Definition: fcn-info.h:205
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
void install_built_in_function(const octave_value &f)
Definition: fcn-info.h:305
octave_value find_function(const octave_value_list &args=octave_value_list(), bool local_funcs=true)
Definition: fcn-info.h:282
octave_value find_cmdline_function(void) const
Definition: fcn-info.h:262
void clear_user_function(bool force=false)
Definition: fcn-info.h:317
std::map< std::string, octave_value >::const_iterator str_val_const_iterator
Definition: fcn-info.h:44
octave_value find_method(const std::string &dispatch_type)
Definition: fcn-info.cc:670
void install_local_function(const octave_value &f, const std::string &file_name)
Definition: fcn-info.h:294
void clear_mex_function(void)
Definition: fcn-info.h:327
void clear_user_function(bool force=false)
Definition: fcn-info.h:153
bool is_defined(void) const
Definition: ov.h:523
octave_value find_user_function(void)
Definition: fcn-info.cc:734
const_iterator end(void) const
Definition: oct-map.h:308
void clear_autoload_function(bool force=false)
Definition: fcn-info.h:145
std::map< std::string, octave_value > class_constructors
Definition: fcn-info.h:208
void install_built_in_dispatch(const std::string &klass)
Definition: fcn-info.h:310
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
void install_cmdline_function(const octave_value &f)
Definition: fcn-info.h:289
void clear(bool force=false)
Definition: fcn-info.h:175
void clear(bool force=false)
Definition: fcn-info.h:315
std::map< std::string, octave_value >::iterator str_val_iterator
Definition: fcn-info.h:45
void install_user_function(const octave_value &f)
Definition: fcn-info.h:118
octave_value xfind(const octave_value_list &args, bool local_funcs)
Definition: fcn-info.cc:369
bool islocked(void) const
Definition: ov.h:1397
octave_value find_method(const std::string &dispatch_type) const
Definition: fcn-info.h:252
void install_user_function(const octave_value &f)
Definition: fcn-info.h:300
std::map< std::string, octave_value > local_functions
Definition: fcn-info.h:202
void install_local_function(const octave_value &f, const std::string &file_name)
Definition: fcn-info.h:112
octave_value built_in_function
Definition: fcn-info.h:221
octave_value install_local_function(const std::string &file_name)
void install_built_in_dispatch(const std::string &klass)
Definition: fcn-info.cc:786
std::string full_name(void) const
Definition: fcn-info.h:189
fcn_info(const std::string &nm="")
Definition: fcn-info.h:232
p
Definition: lu.cc:138
octave_value find(const octave_value_list &args=octave_value_list(), bool local_funcs=true)
Definition: fcn-info.h:241
std::map< std::string, octave_value > class_methods
Definition: fcn-info.h:211
octave_value find_built_in_function(void) const
Definition: fcn-info.h:257
octave_map map(dims)
octave_value find_package(void)
Definition: fcn-info.cc:765
bool is_user_function_defined(void) const
Definition: fcn-info.h:277
octave_value find_autoload(void)
Definition: fcn-info.cc:705
~fcn_info(void)=default
fcn_info_rep & operator=(const fcn_info_rep &)=delete
octave_value dump(void) const
Definition: fcn-info.h:329
octave_value find_autoload(void)
Definition: fcn-info.h:267
octave_value load_private_function(const std::string &dir_name)
Definition: fcn-info.cc:44
const_iterator begin(void) const
Definition: oct-map.h:307
bool is_user_function_defined(void) const
Definition: fcn-info.h:96
void clear_autoload_function(bool force=false)
Definition: fcn-info.h:322
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
octave_value find_user_function(void)
Definition: fcn-info.h:272
octave_value builtin_find(void)
Definition: fcn-info.h:247