GNU Octave 7.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-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_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
39namespace octave
40{
41 class
43 {
44 public: // FIXME: make this class private?
45
46 typedef std::function<std::string (const std::string&)> name_mangler;
47
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 protected:
58
60 dynlib_rep (const std::string& f);
61
62 public:
63
64 virtual ~dynlib_rep (void)
65 {
66 s_instances.erase (m_file);
67 }
68
69 virtual bool is_open (void) const
70 { return false; }
71
72 virtual void * search (const std::string&,
73 const name_mangler& = name_mangler ())
74 { return nullptr; }
75
76 OCTAVE_API bool is_out_of_date (void) const;
77
78 // This method will be overridden conditionally.
79 static OCTAVE_API dynlib_rep * new_instance (const std::string& f);
80
81 static OCTAVE_API dynlib_rep * get_instance (const std::string& f, bool fake);
82
84 { return m_time_loaded; }
85
86 std::string file_name (void) const
87 { return m_file; }
88
89 std::size_t num_fcn_names (void) const { return m_fcn_names.size (); }
90
91 OCTAVE_API std::list<std::string> function_names (void) const;
92
93 OCTAVE_API void add_fcn_name (const std::string&);
94
95 OCTAVE_API bool remove_fcn_name (const std::string&);
96
97 void clear_fcn_names (void) { m_fcn_names.clear (); }
98
99 public:
100
102
103 protected:
104
105 OCTAVE_API void fake_reload (void);
106
107 static OCTAVE_API std::map<std::string, dynlib_rep *> s_instances;
108
109 // Set of hooked function names.
110 typedef std::map<std::string, std::size_t>::iterator fcn_names_iterator;
111 typedef std::map<std::string, std::size_t>::const_iterator fcn_names_const_iterator;
112
113 std::map<std::string, std::size_t> m_fcn_names;
114 std::string m_file;
117 };
118
119 private:
120
122
123 public:
124
125 dynamic_library (void) : m_rep (&s_nil_rep) { m_rep->m_count++; }
126
127 dynamic_library (const std::string& f, bool fake = true)
128 : m_rep (dynlib_rep::get_instance (f, fake)) { }
129
131 {
132 if (--m_rep->m_count == 0 && m_rep != &s_nil_rep)
133 delete m_rep;
134 }
135
136 OCTAVE_API void delete_later (void);
137
139 : m_rep (sl.m_rep)
140 {
141 m_rep->m_count++;
142 }
143
144 dynamic_library& operator = (const dynamic_library& sl)
145 {
146 if (m_rep != sl.m_rep)
147 {
148 if (--m_rep->m_count == 0 && m_rep != &s_nil_rep)
149 delete m_rep;
150
151 m_rep = sl.m_rep;
152 m_rep->m_count++;
153 }
154
155 return *this;
156 }
157
158 bool operator == (const dynamic_library& sl) const
159 { return (m_rep == sl.m_rep); }
160
161 operator bool () const { return m_rep->is_open (); }
162
163 void open (const std::string& f)
164 { *this = dynamic_library (f); }
165
166 std::list<std::string> close (void)
167 {
168 std::list<std::string> removed_fcns = m_rep->function_names ();
169
170 m_rep->clear_fcn_names ();
171
172 *this = dynamic_library ();
173
174 return removed_fcns;
175 }
176
177 void * search (const std::string& nm,
178 const name_mangler& mangler = name_mangler ()) const
179 {
180 void *f = m_rep->search (nm, mangler);
181 if (f)
182 m_rep->add_fcn_name (nm);
183
184 return f;
185 }
186
187 void add (const std::string& name)
188 { m_rep->add_fcn_name (name); }
189
190 bool remove (const std::string& name)
191 { return m_rep->remove_fcn_name (name); }
192
193 std::size_t number_of_functions_loaded (void) const
194 { return m_rep->num_fcn_names (); }
195
196 bool is_out_of_date (void) const
197 { return m_rep->is_out_of_date (); }
198
199 std::string file_name (void) const
200 { return m_rep->file_name (); }
201
203 { return m_rep->time_loaded (); }
204
205 private:
206
208 };
209
210 // FIXME: Currently must return int so that it may be used as an
211 // event_hook function.
212
214}
215
216#endif
std::map< std::string, std::size_t > m_fcn_names
Definition: oct-shlib.h:113
static OCTAVE_API std::map< std::string, dynlib_rep * > s_instances
Definition: oct-shlib.h:107
std::map< std::string, std::size_t >::const_iterator fcn_names_const_iterator
Definition: oct-shlib.h:111
virtual bool is_open(void) const
Definition: oct-shlib.h:69
virtual void * search(const std::string &, const name_mangler &=name_mangler())
Definition: oct-shlib.h:72
std::string file_name(void) const
Definition: oct-shlib.h:86
std::map< std::string, std::size_t >::iterator fcn_names_iterator
Definition: oct-shlib.h:110
refcount< octave_idx_type > m_count
Definition: oct-shlib.h:101
std::size_t num_fcn_names(void) const
Definition: oct-shlib.h:89
sys::time time_loaded(void) const
Definition: oct-shlib.h:83
sys::time time_loaded(void) const
Definition: oct-shlib.h:202
bool remove(const std::string &name)
Definition: oct-shlib.h:190
dynamic_library(const dynamic_library &sl)
Definition: oct-shlib.h:138
std::function< std::string(const std::string &)> name_mangler
Definition: oct-shlib.h:46
void add(const std::string &name)
Definition: oct-shlib.h:187
dynlib_rep * m_rep
Definition: oct-shlib.h:207
void * search(const std::string &nm, const name_mangler &mangler=name_mangler()) const
Definition: oct-shlib.h:177
bool is_out_of_date(void) const
Definition: oct-shlib.h:196
static OCTAVE_API dynlib_rep s_nil_rep
Definition: oct-shlib.h:121
std::string file_name(void) const
Definition: oct-shlib.h:199
dynamic_library(const std::string &f, bool fake=true)
Definition: oct-shlib.h:127
std::list< std::string > close(void)
Definition: oct-shlib.h:166
void open(const std::string &f)
Definition: oct-shlib.h:163
std::size_t number_of_functions_loaded(void) const
Definition: oct-shlib.h:193
QString name
#define OCTAVE_API
Definition: main.in.cc:55
static double f(double k, double l_nu, double c_pm)
Definition: randpoisson.cc:118
int release_unreferenced_dynamic_libraries(void)
Definition: oct-shlib.cc:70
bool operator==(const cdef_class &clsa, const cdef_class &clsb)
Definition: cdef-class.h:437