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