GNU Octave  6.2.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
cdef-package.cc
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 (HAVE_CONFIG_H)
27 # include "config.h"
28 #endif
29 
30 #include <algorithm>
31 #include <iomanip>
32 
33 #include "cdef-class.h"
34 #include "cdef-manager.h"
35 #include "cdef-utils.h"
36 #include "errwarn.h"
37 #include "interpreter-private.h"
38 #include "interpreter.h"
39 #include "load-path.h"
40 #include "ov-builtin.h"
41 #include "ov-classdef.h"
42 #include "ov-fcn-handle.h"
43 #include "ov-usr-fcn.h"
44 #include "parse.h"
45 #include "pt-assign.h"
46 #include "pt-classdef.h"
47 #include "pt-idx.h"
48 #include "pt-misc.h"
49 #include "pt-stmt.h"
50 
51 // Define to 1 to enable debugging statements.
52 #define DEBUG_TRACE 0
53 
54 namespace octave
55 {
56  void
58  const std::string& nm)
59  {
60  class_map[nm] = cls;
61 
62  member_count++;
63  }
64 
65  void
67  const std::string& nm)
68  {
69  function_map[nm] = fcn;
70  }
71 
72  void
74  const std::string& nm)
75  {
76  package_map[nm] = pack;
77 
78  member_count++;
79  }
80 
81  template <typename T1, typename T2>
82  Cell
83  map2Cell (const std::map<T1, T2>& m)
84  {
85  Cell retval (1, m.size ());
86  int i = 0;
87 
88  for (auto it = m.begin (); it != m.end (); ++it, ++i)
89  retval(i) = to_ov (it->second);
90 
91  return retval;
92  }
93 
94  Cell
96  {
97  return map2Cell (class_map);
98  }
99 
100  Cell
102  {
103  return map2Cell (function_map);
104  }
105 
106  Cell
108  {
109  return map2Cell (package_map);
110  }
111 
113  cdef_package::cdef_package_rep::find (const std::string& nm)
114  {
115  std::string symbol_name = get_name () + '.' + nm;
116 
117  interpreter& interp
118  = __get_interpreter__ ("cdef_package::cdef_package_rep::find");
119 
120  return interp.find (symbol_name);
121  }
122 
125  (const std::string& type, const std::list<octave_value_list>& idx,
126  int nargout)
127  {
129 
130  switch (type[0])
131  {
132  case '.':
133  {
134  if (idx.front ().length () != 1)
135  error ("invalid meta.package indexing");
136 
137  std::string nm = idx.front ()(0).xstring_value ("invalid meta.package indexing, expected a symbol name");
138 
139 #if DEBUG_TRACE
140  std::cerr << "meta.package query: " << nm << std::endl;
141 #endif
142 
143  octave_value o = find (nm);
144 
145  if (! o.is_defined ())
146  error ("member '%s' in package '%s' does not exist",
147  nm.c_str (), get_name ().c_str ());
148 
149  if (o.is_function ())
150  {
151  octave_function *fcn = o.function_value ();
152 
153  // NOTE: the case where the package query is the last
154  // part of this subsref index is handled in the parse
155  // tree, because there is some logic to handle magic
156  // "end" that makes it impossible to execute the
157  // function call at this stage.
158 
159  if (type.size () > 1
160  && ! fcn->accepts_postfix_index (type[1]))
161  {
162  octave_value_list tmp_args;
163 
164  retval = feval (o, tmp_args, nargout);
165  }
166  else
167  retval(0) = o;
168 
169  if (type.size () > 1 && idx.size () > 1)
170  retval = retval(0).next_subsref (nargout, type,
171  idx, 1);
172  }
173  else if (type.size () > 1 && idx.size () > 1)
174  retval = o.next_subsref (nargout, type, idx, 1);
175  else
176  retval(0) = o;
177  }
178  break;
179 
180  default:
181  error ("invalid meta.package indexing");
182  break;
183  }
184 
185  return retval;
186  }
187 
188  void
190  {
191  // FIXME: Do we really want to unregister the package, as it
192  // could still be referenced by classes or sub-packages?
193  // If the package object is recreated later on, it won't
194  // match the one already referenced by those classes or
195  // sub-packages.
196 
197  cdef_manager& cdm
198  = __get_cdef_manager__ ("cdef_package::cdef_package_rep::meta_release");
199 
200  // Don't delete the "meta" package.
201  if (this != cdm.meta ().get_rep ())
202  cdm.unregister_package (wrap ());
203  }
204 }
Definition: Cell.h:43
const cdef_package & meta(void) const
Definition: cdef-manager.h:93
void unregister_package(const cdef_package &pkg)
Definition: cdef-manager.h:83
void install_package(const cdef_package &pack, const std::string &nm)
Definition: cdef-package.cc:73
octave_value find(const std::string &nm)
octave_value_list meta_subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
void install_function(const octave_value &fcn, const std::string &nm)
Definition: cdef-package.cc:66
std::map< std::string, cdef_class > class_map
Definition: cdef-package.h:115
void install_class(const cdef_class &cls, const std::string &nm)
Definition: cdef-package.cc:57
octave_value find(const std::string &nm)
Definition: cdef-package.h:207
cdef_package_rep * get_rep(void)
Definition: cdef-package.h:214
std::string get_name(void) const
Definition: cdef-package.h:205
octave_value find(const std::string &name)
virtual bool accepts_postfix_index(char type) const
Definition: ov-fcn.h:236
bool is_function(void) const
Definition: ov.h:730
bool is_defined(void) const
Definition: ov.h:551
octave_function * function_value(bool silent=false) const
octave_value next_subsref(const std::string &type, const std::list< octave_value_list > &idx, size_t skip=1)
void error(const char *fmt,...)
Definition: error.cc:968
T octave_idx_type m
Definition: mx-inlines.cc:773
octave_value to_ov(const cdef_object &obj)
Definition: cdef-utils.cc:128
interpreter & __get_interpreter__(const std::string &who)
Cell map2Cell(const std::map< T1, T2 > &m)
Definition: cdef-package.cc:83
octave_value_list feval(const char *name, const octave_value_list &args, int nargout)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
Definition: oct-parse.cc:9580
cdef_manager & __get_cdef_manager__(const std::string &who)
octave_value::octave_value(const Array< char > &chm, char type) return retval
Definition: ov.cc:811