GNU Octave  9.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-2024 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
54  cdef_package_rep : public cdef_meta_object_rep
55  {
56  public:
57 
58  cdef_package_rep () : cdef_meta_object_rep (), m_member_count (0) { }
59 
60  cdef_package_rep& operator = (const cdef_package_rep&) = delete;
61 
62  ~cdef_package_rep () = default;
63 
64  cdef_object_rep * copy () const
65  { return new cdef_package_rep (*this); }
66 
67  bool is_package () const { return true; }
68 
69  std::string get_name () 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 () const;
83 
84  OCTINTERP_API Cell get_functions () const;
85 
86  OCTINTERP_API Cell get_packages () const;
87 
88  octave_idx_type static_count () const { return m_member_count; }
89 
90  void destroy ()
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 ();
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.
127  octave_idx_type m_member_count;
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
133  function_const_iterator;
134  typedef std::map<std::string, cdef_package>::iterator package_iterator;
135  typedef std::map<std::string, cdef_package>::const_iterator
136  package_const_iterator;
137 
138  cdef_package_rep (const cdef_package_rep& p)
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 
144  cdef_package wrap ()
145  {
146  m_count++;
147  return cdef_package (this);
148  }
149  };
150 
151 public:
152 
154 
155  cdef_package (const std::string& nm)
156  : cdef_meta_object (new cdef_package_rep ())
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 () = 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 () const
197  {
198  return get_rep ()->get_classes ();
199  }
200 
202  {
203  return get_rep ()->get_functions ();
204  }
205 
207  {
208  return get_rep ()->get_packages ();
209  }
210 
211  std::string get_name () 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 
220  cdef_package_rep * get_rep ()
221  {
222  return dynamic_cast<cdef_package_rep *> (cdef_object::get_rep ());
223  }
224 
225  const cdef_package_rep * get_rep () 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 
233 OCTAVE_END_NAMESPACE(octave)
234 
235 #endif
Definition: Cell.h:43
cdef_class & operator=(const cdef_class &cls)
Definition: cdef-class.h:256
std::string get_name() const
Definition: cdef-class.h:318
bool meta_accepts_postfix_index(char type) const
Definition: cdef-object.h:707
bool is_package() const
Definition: cdef-object.h:692
octave_value_list meta_subsref(const std::string &type, const std::list< octave_value_list > &idx, int nargout)
Definition: cdef-object.h:699
const cdef_object_rep * get_rep() const
Definition: cdef-object.h:310
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
std::string class_name() const
Definition: cdef-object.h:234
cdef_object copy() const
Definition: cdef-object.h:250
octave_value get(const std::string &pname) const
Definition: cdef-object.h:268
void install_package(const cdef_package &pack, const std::string &nm)
Definition: cdef-package.h:191
~cdef_package()=default
cdef_package(const cdef_object &obj)
Definition: cdef-package.h:163
cdef_package(const cdef_package &pack)
Definition: cdef-package.h:161
Cell get_functions() const
Definition: cdef-package.h:201
Cell get_classes() const
Definition: cdef-package.h:196
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
Cell get_packages() const
Definition: cdef-package.h:206
octave_value find(const std::string &nm)
Definition: cdef-package.h:213
friend void install_classdef(octave::interpreter &interp)
std::string get_name() const
Definition: cdef-package.h:211
std::string string_value(bool force=false) const
Definition: ov.h:974
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
void() error(const char *fmt,...)
Definition: error.cc:988