GNU Octave  8.1.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-2023 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 
55 
56 void
58  const std::string& nm)
59 {
60  m_class_map[nm] = cls;
61 
63 }
64 
65 void
67  const std::string& nm)
68 {
69  m_function_map[nm] = fcn;
70 }
71 
72 void
74  const std::string& nm)
75 {
76  m_package_map[nm] = pack;
77 
78  m_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 (m_class_map);
98 }
99 
100 Cell
102 {
103  return map2Cell (m_function_map);
104 }
105 
106 Cell
108 {
109  return map2Cell (m_package_map);
110 }
111 
114 {
115  std::string symbol_name = get_name () + '.' + nm;
116 
117  interpreter& interp = __get_interpreter__ ();
118 
119  return interp.find (symbol_name);
120 }
121 
124 (const std::string& type, const std::list<octave_value_list>& idx,
125  int nargout)
126 {
127  octave_value_list retval;
128 
129  switch (type[0])
130  {
131  case '.':
132  {
133  if (idx.front ().length () != 1)
134  error ("invalid meta.package indexing");
135 
136  std::string nm = idx.front ()(
137  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 
198 
199  // Don't delete the "meta" package.
200  if (this != cdm.meta ().get_rep ())
201  cdm.unregister_package (wrap ());
202 }
203 
OCTAVE_END_NAMESPACE(octave)
Cell map2Cell(const std::map< T1, T2 > &m)
Definition: cdef-package.cc:83
octave_value to_ov(const cdef_object &obj)
Definition: cdef-utils.cc:128
Definition: Cell.h:43
const cdef_package & meta(void) const
Definition: cdef-manager.h:98
void unregister_package(const cdef_package &pkg)
Definition: cdef-manager.h:88
OCTINTERP_API Cell get_packages(void) const
OCTINTERP_API void install_package(const cdef_package &pack, const std::string &nm)
Definition: cdef-package.cc:73
OCTINTERP_API void meta_release(void)
OCTINTERP_API Cell get_functions(void) const
OCTINTERP_API Cell get_classes(void) const
Definition: cdef-package.cc:95
OCTINTERP_API void install_class(const cdef_class &cls, const std::string &nm)
Definition: cdef-package.cc:57
OCTINTERP_API void install_function(const octave_value &fcn, const std::string &nm)
Definition: cdef-package.cc:66
OCTINTERP_API octave_value find(const std::string &nm)
OCTINTERP_API octave_value_list meta_subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
std::map< std::string, cdef_class > m_class_map
Definition: cdef-package.h:121
cdef_package_rep * get_rep(void)
Definition: cdef-package.h:220
std::string get_name(void) const
Definition: cdef-package.h:211
octave_value find(const std::string &nm)
Definition: cdef-package.h:213
octave_value find(const std::string &name)
virtual bool accepts_postfix_index(char type) const
Definition: ov-fcn.h:227
bool is_function(void) const
Definition: ov.h:822
OCTINTERP_API octave_function * function_value(bool silent=false) const
bool is_defined(void) const
Definition: ov.h:637
OCTINTERP_API octave_value next_subsref(const std::string &type, const std::list< octave_value_list > &idx, std::size_t skip=1)
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition: error.cc:979
interpreter & __get_interpreter__(void)
cdef_manager & __get_cdef_manager__(void)
T octave_idx_type m
Definition: mx-inlines.cc:773
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:10370