GNU Octave 10.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-2025 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 to 1 to enable debugging statements.
51#define DEBUG_TRACE 0
52
54
55void
56cdef_package::cdef_package_rep::install_class (const cdef_class& cls,
57 const std::string& nm)
58{
59 m_class_map[nm] = cls;
60
61 m_member_count++;
62}
63
64void
65cdef_package::cdef_package_rep::install_function (const octave_value& fcn,
66 const std::string& nm)
67{
68 m_function_map[nm] = fcn;
69}
70
71void
72cdef_package::cdef_package_rep::install_package (const cdef_package& pack,
73 const std::string& nm)
74{
75 m_package_map[nm] = pack;
76
77 m_member_count++;
78}
79
80template <typename T1, typename T2>
81Cell
82map2Cell (const std::map<T1, T2>& m)
83{
84 Cell retval (1, m.size ());
85 int i = 0;
86
87 for (const auto& it : m)
88 retval(i++) = to_ov (it.second);
89
90 return retval;
91}
92
93Cell
94cdef_package::cdef_package_rep::get_classes () const
95{
96 return map2Cell (m_class_map);
97}
98
99Cell
100cdef_package::cdef_package_rep::get_functions () const
101{
102 return map2Cell (m_function_map);
103}
104
105Cell
106cdef_package::cdef_package_rep::get_packages () const
107{
108 return map2Cell (m_package_map);
109}
110
112cdef_package::cdef_package_rep::find (const std::string& nm)
113{
114 std::string symbol_name = get_name () + '.' + nm;
115
116 interpreter& interp = __get_interpreter__ ();
117
118 return interp.find (symbol_name);
119}
120
122cdef_package::cdef_package_rep::meta_subsref
123(const std::string& type, const std::list<octave_value_list>& idx,
124 int nargout)
125{
126 octave_value_list retval;
127
128 switch (type[0])
129 {
130 case '.':
131 {
132 if (idx.front ().length () != 1)
133 error ("invalid meta.package indexing");
134
135 std::string nm = idx.front ()(0).xstring_value ("invalid meta.package indexing, expected a symbol name");
136
137#if DEBUG_TRACE
138 std::cerr << "meta.package query: " << nm << std::endl;
139#endif
140
141 octave_value o = find (nm);
142
143 if (! o.is_defined ())
144 error ("member '%s' in package '%s' does not exist",
145 nm.c_str (), get_name ().c_str ());
146
147 if (o.is_function ())
148 {
150
151 // NOTE: the case where the package query is the last
152 // part of this subsref index is handled in the parse
153 // tree, because there is some logic to handle magic
154 // "end" that makes it impossible to execute the
155 // function call at this stage.
156
157 if (type.size () > 1
158 && ! fcn->accepts_postfix_index (type[1]))
159 {
160 octave_value_list tmp_args;
161
162 interpreter& interp = __get_interpreter__ ();
163
164 retval = interp.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
188void
189cdef_package::cdef_package_rep::meta_release ()
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
204OCTAVE_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:227
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:592
bool is_function() const
Definition ov.h:777
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition error.cc:1003
cdef_manager & __get_cdef_manager__()
interpreter & __get_interpreter__()