GNU Octave  6.2.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-2021 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 
39 namespace octave
40 {
41  class
42  OCTAVE_API
44  {
45  public: // FIXME: make this class private?
46 
47  typedef std::function<std::string (const std::string&)> name_mangler;
48 
49  class dynlib_rep
50  {
51  public:
52 
53  dynlib_rep (void)
54  : m_count (1), m_fcn_names (), m_file (), m_time_loaded (time_t ()),
55  m_search_all_loaded (false)
56  { }
57 
58  protected:
59 
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  bool is_out_of_date (void) const;
77 
78  // This method will be overridden conditionally.
79  static dynlib_rep * new_instance (const std::string& f);
80 
81  static dynlib_rep * get_instance (const std::string& f, bool fake);
82 
83  sys::time time_loaded (void) const
84  { return m_time_loaded; }
85 
86  std::string file_name (void) const
87  { return m_file; }
88 
89  size_t num_fcn_names (void) const { return m_fcn_names.size (); }
90 
91  std::list<std::string> function_names (void) const;
92 
93  void add_fcn_name (const std::string&);
94 
95  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  void fake_reload (void);
106 
107  static std::map<std::string, dynlib_rep *> s_instances;
108 
109  // Set of hooked function names.
110  typedef std::map<std::string, size_t>::iterator fcn_names_iterator;
111  typedef std::map<std::string, size_t>::const_iterator fcn_names_const_iterator;
112 
113  std::map<std::string, 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 
137  : m_rep (sl.m_rep)
138  {
139  m_rep->m_count++;
140  }
141 
142  dynamic_library& operator = (const dynamic_library& sl)
143  {
144  if (m_rep != sl.m_rep)
145  {
146  if (--m_rep->m_count == 0 && m_rep != &s_nil_rep)
147  delete m_rep;
148 
149  m_rep = sl.m_rep;
150  m_rep->m_count++;
151  }
152 
153  return *this;
154  }
155 
156  bool operator == (const dynamic_library& sl) const
157  { return (m_rep == sl.m_rep); }
158 
159  operator bool () const { return m_rep->is_open (); }
160 
161  void open (const std::string& f)
162  { *this = dynamic_library (f); }
163 
164  std::list<std::string> close (void)
165  {
166  std::list<std::string> removed_fcns = m_rep->function_names ();
167 
168  m_rep->clear_fcn_names ();
169 
170  *this = dynamic_library ();
171 
172  return removed_fcns;
173  }
174 
175  void * search (const std::string& nm,
176  const name_mangler& mangler = name_mangler ()) const
177  {
178  void *f = m_rep->search (nm, mangler);
179  if (f)
180  m_rep->add_fcn_name (nm);
181 
182  return f;
183  }
184 
185  void add (const std::string& name)
186  { m_rep->add_fcn_name (name); }
187 
188  bool remove (const std::string& name)
189  { return m_rep->remove_fcn_name (name); }
190 
191  size_t number_of_functions_loaded (void) const
192  { return m_rep->num_fcn_names (); }
193 
194  bool is_out_of_date (void) const
195  { return m_rep->is_out_of_date (); }
196 
197  std::string file_name (void) const
198  { return m_rep->file_name (); }
199 
200  sys::time time_loaded (void) const
201  { return m_rep->time_loaded (); }
202 
203  private:
204 
206  };
207 }
208 
209 #endif
static std::map< std::string, dynlib_rep * > s_instances
Definition: oct-shlib.h:107
std::map< std::string, size_t > m_fcn_names
Definition: oct-shlib.h:113
virtual bool is_open(void) const
Definition: oct-shlib.h:69
std::map< std::string, size_t >::iterator fcn_names_iterator
Definition: oct-shlib.h:110
std::string file_name(void) const
Definition: oct-shlib.h:86
size_t num_fcn_names(void) const
Definition: oct-shlib.h:89
std::map< std::string, size_t >::const_iterator fcn_names_const_iterator
Definition: oct-shlib.h:111
virtual void * search(const std::string &, const name_mangler &=name_mangler())
Definition: oct-shlib.h:72
refcount< octave_idx_type > m_count
Definition: oct-shlib.h:101
sys::time time_loaded(void) const
Definition: oct-shlib.h:83
sys::time time_loaded(void) const
Definition: oct-shlib.h:200
bool remove(const std::string &name)
Definition: oct-shlib.h:188
dynamic_library(const dynamic_library &sl)
Definition: oct-shlib.h:136
std::function< std::string(const std::string &)> name_mangler
Definition: oct-shlib.h:47
void add(const std::string &name)
Definition: oct-shlib.h:185
std::list< std::string > close(void)
Definition: oct-shlib.h:164
dynlib_rep * m_rep
Definition: oct-shlib.h:205
bool is_out_of_date(void) const
Definition: oct-shlib.h:194
static dynlib_rep s_nil_rep
Definition: oct-shlib.h:121
size_t number_of_functions_loaded(void) const
Definition: oct-shlib.h:191
std::string file_name(void) const
Definition: oct-shlib.h:197
dynamic_library(const std::string &f, bool fake=true)
Definition: oct-shlib.h:127
void open(const std::string &f)
Definition: oct-shlib.h:161
void * search(const std::string &nm, const name_mangler &mangler=name_mangler()) const
Definition: oct-shlib.h:175
QString name
static double f(double k, double l_nu, double c_pm)
Definition: randpoisson.cc:118
bool operator==(const cdef_class &clsa, const cdef_class &clsb)
Definition: cdef-class.h:416