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