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.h
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 (octave_cdef_package_h)
27#define octave_cdef_package_h 1
28
29#include "octave-config.h"
30
31#include <map>
32#include <set>
33#include <string>
34
35#include "oct-refcount.h"
36
37#include "cdef-fwd.h"
38#include "cdef-object.h"
39#include "ov.h"
40
42
43class interpreter;
44
45class OCTINTERP_API cdef_package : public cdef_meta_object
46{
47 friend class cdef_class;
48
49private:
50
51 class cdef_package_rep : public cdef_meta_object_rep
52 {
53 public:
54
55 cdef_package_rep () : cdef_meta_object_rep (), m_member_count (0) { }
56
57 cdef_package_rep& operator = (const cdef_package_rep&) = delete;
58
59 ~cdef_package_rep () = default;
60
61 cdef_object_rep * copy () const
62 { return new cdef_package_rep (*this); }
63
64 bool is_package () const { return true; }
65
66 std::string get_name () const { return get("Name").string_value (); }
67
68 void set_name (const std::string& nm) { put ("Name", nm); }
69
70 OCTINTERP_API void
71 install_class (const cdef_class& cls, const std::string& nm);
72
73 OCTINTERP_API void
74 install_function (const octave_value& fcn, const std::string& nm);
75
76 OCTINTERP_API void
77 install_package (const cdef_package& pack, const std::string& nm);
78
79 OCTINTERP_API Cell get_classes () const;
80
81 OCTINTERP_API Cell get_functions () const;
82
83 OCTINTERP_API Cell get_packages () const;
84
85 octave_idx_type static_count () const { return m_member_count; }
86
87 void destroy ()
88 {
89 if (m_member_count)
90 {
91 m_count++;
92 cdef_package lock (this);
93
94 m_member_count = 0;
95 m_class_map.clear ();
96 m_package_map.clear ();
97 }
98 else
99 delete this;
100 }
101
102 OCTINTERP_API octave_value_list
103 meta_subsref (const std::string& type,
104 const std::list<octave_value_list>& idx, int nargout);
105
106 OCTINTERP_API void meta_release ();
107
108 bool meta_accepts_postfix_index (char type) const
109 {
110 return (type == '.');
111 }
112
113 OCTINTERP_API octave_value find (const std::string& nm);
114
115 private:
116
117 std::string m_full_name;
118 std::map<std::string, cdef_class> m_class_map;
119 std::map<std::string, octave_value> m_function_map;
120 std::map<std::string, cdef_package> m_package_map;
121
122 // The number of registered members in this package (classes, packages).
123 // This only accounts for the members that back-reference to this package.
124 octave_idx_type m_member_count;
125
126 typedef std::map<std::string, cdef_class>::iterator class_iterator;
127 typedef std::map<std::string, cdef_class>::const_iterator class_const_iterator;
128 typedef std::map<std::string, octave_value>::iterator function_iterator;
129 typedef std::map<std::string, octave_value>::const_iterator
130 function_const_iterator;
131 typedef std::map<std::string, cdef_package>::iterator package_iterator;
132 typedef std::map<std::string, cdef_package>::const_iterator
133 package_const_iterator;
134
135 cdef_package_rep (const cdef_package_rep& p)
136 : cdef_meta_object_rep (p), m_full_name (p.m_full_name),
137 m_class_map (p.m_class_map), m_function_map (p.m_function_map),
138 m_package_map (p.m_package_map), m_member_count (p.m_member_count)
139 { }
140
141 cdef_package wrap ()
142 {
143 m_count++;
144 return cdef_package (this);
145 }
146 };
147
148public:
149
151
152 cdef_package (const std::string& nm)
153 : cdef_meta_object (new cdef_package_rep ())
154 {
155 get_rep ()->set_name (nm);
156 }
157
158 cdef_package (const cdef_package& pack) : cdef_meta_object (pack) { }
159
161 : cdef_meta_object (obj)
162 {
163 // This should never happen...
164 if (! is_package ())
165 error ("internal error: invalid assignment from %s to meta.package object",
166 class_name ().c_str ());
167 }
168
170 {
172
173 return *this;
174 }
175
176 ~cdef_package () = default;
177
178 void install_class (const cdef_class& cls, const std::string& nm)
179 {
180 get_rep ()->install_class (cls, nm);
181 }
182
183 void install_function (const octave_value& fcn, const std::string& nm)
184 {
185 get_rep ()->install_function (fcn, nm);
186 }
187
188 void install_package (const cdef_package& pack, const std::string& nm)
189 {
190 get_rep ()->install_package (pack, nm);
191 }
192
194 {
195 return get_rep ()->get_classes ();
196 }
197
199 {
200 return get_rep ()->get_functions ();
201 }
202
204 {
205 return get_rep ()->get_packages ();
206 }
207
208 std::string get_name () const { return get_rep ()->get_name (); }
209
210 octave_value find (const std::string& nm)
211 {
212 return get_rep ()->find (nm);
213 }
214
215private:
216
217 cdef_package_rep * get_rep ()
218 {
219 return dynamic_cast<cdef_package_rep *> (cdef_object::get_rep ());
220 }
221
222 const cdef_package_rep * get_rep () const
223 {
224 return dynamic_cast<const cdef_package_rep *> (cdef_object::get_rep ());
225 }
226
227 friend void install_classdef (octave::interpreter& interp);
228};
229
230OCTAVE_END_NAMESPACE(octave)
231
232#endif
Definition Cell.h:41
cdef_meta_object & operator=(const cdef_object &)=delete
cdef_object & operator=(const cdef_object &obj)
const cdef_object_rep * get_rep() const
void install_package(const cdef_package &pack, const std::string &nm)
~cdef_package()=default
cdef_package(const cdef_object &obj)
cdef_package(const cdef_package &pack)
Cell get_functions() const
Cell get_classes() const
void install_function(const octave_value &fcn, const std::string &nm)
cdef_package(const std::string &nm)
void install_class(const cdef_class &cls, const std::string &nm)
Cell get_packages() const
octave_value find(const std::string &nm)
friend void install_classdef(octave::interpreter &interp)
std::string get_name() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void error(const char *fmt,...)
Definition error.cc:1003