GNU Octave  9.1.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
oct-shlib.h
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (C) 1999-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_oct_shlib_h)
27 #define octave_oct_shlib_h 1
28 
29 #include "octave-config.h"
30 
31 #include <functional>
32 #include <list>
33 #include <map>
34 #include <string>
35 
36 #include "oct-time.h"
37 #include "oct-refcount.h"
38 
40 
41 class
43 {
44 public: // FIXME: make this class private?
45 
46  typedef std::function<std::string (const std::string&)> name_mangler;
47 
48  class dynlib_rep
49  {
50  public:
51 
53  : m_count (1), m_fcn_names (), m_file (), m_time_loaded (OCTAVE_TIME_T ()),
54  m_search_all_loaded (false)
55  { }
56 
57  OCTAVE_DISABLE_COPY_MOVE (dynlib_rep)
58 
59  protected:
60 
62  dynlib_rep (const std::string& f);
63 
64  public:
65 
66  virtual ~dynlib_rep ()
67  {
68  s_instances.erase (m_file);
69  }
70 
71  virtual bool is_open () const
72  { return false; }
73 
74  virtual void * search (const std::string&,
75  const name_mangler& = name_mangler ())
76  { return nullptr; }
77 
78  OCTAVE_API bool is_out_of_date () const;
79 
80  // This method will be overridden conditionally.
81  static OCTAVE_API dynlib_rep * new_instance (const std::string& f);
82 
83  static OCTAVE_API dynlib_rep * get_instance (const std::string& f, bool fake);
84 
85  sys::time time_loaded () const
86  { return m_time_loaded; }
87 
88  std::string file_name () const
89  { return m_file; }
90 
91  std::size_t num_fcn_names () const { return m_fcn_names.size (); }
92 
93  OCTAVE_API std::list<std::string> function_names () const;
94 
95  OCTAVE_API void add_fcn_name (const std::string&);
96 
97  OCTAVE_API bool remove_fcn_name (const std::string&);
98 
99  void clear_fcn_names () { m_fcn_names.clear (); }
100 
101  public:
102 
104 
105  protected:
106 
107  OCTAVE_API void fake_reload ();
108 
109  static OCTAVE_API std::map<std::string, dynlib_rep *> s_instances;
110 
111  // Set of hooked function names.
112  typedef std::map<std::string, std::size_t>::iterator fcn_names_iterator;
113  typedef std::map<std::string, std::size_t>::const_iterator fcn_names_const_iterator;
114 
115  std::map<std::string, std::size_t> m_fcn_names;
116  std::string m_file;
117  sys::time m_time_loaded;
119  };
120 
121 private:
122 
123  static OCTAVE_API dynlib_rep s_nil_rep;
124 
125 public:
126 
127  dynamic_library () : m_rep (&s_nil_rep) { m_rep->m_count++; }
128 
129  dynamic_library (const std::string& f, bool fake = true)
130  : m_rep (dynlib_rep::get_instance (f, fake)) { }
131 
133  {
134  if (--m_rep->m_count == 0 && m_rep != &s_nil_rep)
135  delete m_rep;
136  }
137 
138  OCTAVE_API void delete_later ();
139 
141  : m_rep (sl.m_rep)
142  {
143  m_rep->m_count++;
144  }
145 
146  dynamic_library& operator = (const dynamic_library& sl)
147  {
148  if (m_rep != sl.m_rep)
149  {
150  if (--m_rep->m_count == 0 && m_rep != &s_nil_rep)
151  delete m_rep;
152 
153  m_rep = sl.m_rep;
154  m_rep->m_count++;
155  }
156 
157  return *this;
158  }
159 
160  bool operator == (const dynamic_library& sl) const
161  { return (m_rep == sl.m_rep); }
162 
163  operator bool () const { return m_rep->is_open (); }
164 
165  void open (const std::string& f)
166  { *this = dynamic_library (f); }
167 
168  std::list<std::string> close ()
169  {
170  std::list<std::string> removed_fcns = m_rep->function_names ();
171 
172  m_rep->clear_fcn_names ();
173 
174  *this = dynamic_library ();
175 
176  return removed_fcns;
177  }
178 
179  void * search (const std::string& nm,
180  const name_mangler& mangler = name_mangler ()) const
181  {
182  void *f = m_rep->search (nm, mangler);
183  if (f)
184  m_rep->add_fcn_name (nm);
185 
186  return f;
187  }
188 
189  void add (const std::string& name)
190  { m_rep->add_fcn_name (name); }
191 
192  bool remove (const std::string& name)
193  { return m_rep->remove_fcn_name (name); }
194 
195  std::size_t number_of_functions_loaded () const
196  { return m_rep->num_fcn_names (); }
197 
198  bool is_out_of_date () const
199  { return m_rep->is_out_of_date (); }
200 
201  std::string file_name () const
202  { return m_rep->file_name (); }
203 
204  sys::time time_loaded () const
205  { return m_rep->time_loaded (); }
206 
207 private:
208 
209  dynlib_rep *m_rep;
210 };
211 
212 // FIXME: Currently must return int so that it may be used as an
213 // event_hook function.
214 
216 
217 OCTAVE_END_NAMESPACE(octave)
218 
219 #endif
std::map< std::string, std::size_t >::const_iterator fcn_names_const_iterator
Definition: oct-shlib.h:113
virtual void * search(const std::string &, const name_mangler &=name_mangler())
Definition: oct-shlib.h:74
std::size_t num_fcn_names() const
Definition: oct-shlib.h:91
sys::time time_loaded() const
Definition: oct-shlib.h:85
static std::map< std::string, dynlib_rep * > s_instances
Definition: oct-shlib.h:109
virtual bool is_open() const
Definition: oct-shlib.h:71
std::map< std::string, std::size_t > m_fcn_names
Definition: oct-shlib.h:115
std::string file_name() const
Definition: oct-shlib.h:88
std::map< std::string, std::size_t >::iterator fcn_names_iterator
Definition: oct-shlib.h:112
refcount< octave_idx_type > m_count
Definition: oct-shlib.h:103
void * search(const std::string &nm, const name_mangler &mangler=name_mangler()) const
Definition: oct-shlib.h:179
void open(const std::string &f)
Definition: oct-shlib.h:165
std::string file_name() const
Definition: oct-shlib.h:201
std::list< std::string > close()
Definition: oct-shlib.h:168
bool is_out_of_date() const
Definition: oct-shlib.h:198
std::function< std::string(const std::string &)> name_mangler
Definition: oct-shlib.h:46
dynamic_library(const std::string &f, bool fake=true)
Definition: oct-shlib.h:129
sys::time time_loaded() const
Definition: oct-shlib.h:204
std::size_t number_of_functions_loaded() const
Definition: oct-shlib.h:195
dynamic_library(const dynamic_library &sl)
Definition: oct-shlib.h:140
void add(const std::string &name)
Definition: oct-shlib.h:189
bool remove(const std::string &name)
Definition: oct-shlib.h:192
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
bool operator==(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:520
F77_RET_T const F77_DBLE const F77_DBLE * f
#define OCTAVE_API
Definition: main.cc:55
int release_unreferenced_dynamic_libraries()
Definition: oct-shlib.cc:72