GNU Octave 11.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 
Loading...
Searching...
No Matches
cdef-package.cc
Go to the documentation of this file.
1////////////////////////////////////////////////////////////////////////
2//
3// Copyright (C) 2012-2026 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 "pt-assign.h"
45#include "pt-classdef.h"
46#include "pt-idx.h"
47#include "pt-misc.h"
48#include "pt-stmt.h"
49
50#define OCTAVE_CDEF_PACKAGE_DEBUG 0
51#if OCTAVE_CDEF_PACKAGE_DEBUG
52# include <iostream>
53#endif
54
56
57void
58cdef_package::cdef_package_rep::install_class (const cdef_class& cls,
59 const std::string& nm)
60{
61 m_class_map[nm] = cls;
62
63 m_member_count++;
64}
65
66void
67cdef_package::cdef_package_rep::install_function (const octave_value& fcn,
68 const std::string& nm)
69{
70 m_function_map[nm] = fcn;
71}
72
73void
74cdef_package::cdef_package_rep::install_package (const cdef_package& pack,
75 const std::string& nm)
76{
77 m_package_map[nm] = pack;
78
79 m_member_count++;
80}
81
82template <typename T1, typename T2>
83Cell
84map2Cell (const std::map<T1, T2>& m)
85{
86 Cell retval (1, m.size ());
87 int i = 0;
88
89 for (const auto& it : m)
90 retval(i++) = to_ov (it.second);
91
92 return retval;
93}
94
95Cell
96cdef_package::cdef_package_rep::get_classes () const
97{
98 return map2Cell (m_class_map);
99}
100
101Cell
102cdef_package::cdef_package_rep::get_functions () const
103{
104 return map2Cell (m_function_map);
105}
106
107Cell
108cdef_package::cdef_package_rep::get_packages () const
109{
110 return map2Cell (m_package_map);
111}
112
114cdef_package::cdef_package_rep::find (const std::string& nm)
115{
116 std::string symbol_name = get_name () + '.' + nm;
117
118 interpreter& interp = __get_interpreter__ ();
119
120 return interp.find (symbol_name);
121}
122
124cdef_package::cdef_package_rep::meta_subsref
125(const std::string& type, const std::list<octave_value_list>& idx,
126 int nargout)
127{
128 octave_value_list retval;
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 OCTAVE_CDEF_PACKAGE_DEBUG
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 {
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 interpreter& interp = __get_interpreter__ ();
165
166 retval = interp.feval (o, tmp_args, nargout);
167 }
168 else
169 retval(0) = o;
170
171 if (type.size () > 1 && idx.size () > 1)
172 retval = retval(0).next_subsref (nargout, type,
173 idx, 1);
174 }
175 else if (type.size () > 1 && idx.size () > 1)
176 retval = o.next_subsref (nargout, type, idx, 1);
177 else
178 retval(0) = o;
179 }
180 break;
181
182 default:
183 error ("invalid meta.package indexing");
184 break;
185 }
186
187 return retval;
188}
189
190void
191cdef_package::cdef_package_rep::meta_release ()
192{
193 // FIXME: Do we really want to unregister the package, as it
194 // could still be referenced by classes or sub-packages?
195 // If the package object is recreated later on, it won't
196 // match the one already referenced by those classes or
197 // sub-packages.
198
200
201 // Don't delete the "meta" package.
202 if (this != cdm.meta ().get_rep ())
203 cdm.unregister_package (wrap ());
204}
205
206OCTAVE_END_NAMESPACE(octave)
Cell map2Cell(const std::map< T1, T2 > &m)
octave_value to_ov(const cdef_object &obj)
Definition Cell.h:41
void unregister_package(const cdef_package &pkg)
const cdef_package & meta() const
octave_value find(const std::string &name)
octave_value_list feval(const char *name, const octave_value_list &args=octave_value_list(), int nargout=0)
Evaluate an Octave function (built-in or interpreted) and return the list of result values.
virtual bool accepts_postfix_index(char type) const
Definition ov-fcn.h:231
octave_function * function_value(bool silent=false) const
octave_value next_subsref(const std::string &type, const std::list< octave_value_list > &idx, std::size_t skip=1)
bool is_defined() const
Definition ov.h:590
bool is_function() const
Definition ov.h:775
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition error.cc:1008
cdef_manager & __get_cdef_manager__()
interpreter & __get_interpreter__()