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