GNU Octave  4.4.1
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-2018 John W. Eaton
4 Copyright (C) 2009 VZLU Prague
5 
6 This file is part of Octave.
7 
8 Octave is free software: you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with Octave; see the file COPYING. If not, see
20 <https://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if ! defined (octave_oct_shlib_h)
25 #define octave_oct_shlib_h 1
26 
27 #include "octave-config.h"
28 
29 #include <list>
30 #include <string>
31 #include <map>
32 
33 #include "oct-time.h"
34 #include "oct-refcount.h"
35 
36 namespace octave
37 {
38  class
39  OCTAVE_API
41  {
42  public: // FIXME: make this class private?
43 
44  typedef std::string (*name_mangler) (const std::string&);
45 
46  class dynlib_rep
47  {
48  public:
49 
50  dynlib_rep (void)
51  : count (1), file (), tm_loaded (time_t ()), fcn_names (),
52  search_all_loaded (false)
53  { }
54 
55  protected:
56 
57  dynlib_rep (const std::string& f);
58 
59  public:
60 
61  virtual ~dynlib_rep (void)
62  {
63  instances.erase (file);
64  }
65 
66  virtual bool is_open (void) const
67  { return false; }
68 
69  virtual void * search (const std::string&, name_mangler = nullptr)
70  { return nullptr; }
71 
72  bool is_out_of_date (void) const;
73 
74  // This method will be overridden conditionally.
75  static dynlib_rep * new_instance (const std::string& f);
76 
77  static dynlib_rep * get_instance (const std::string& f, bool fake);
78 
79  sys::time time_loaded (void) const
80  { return tm_loaded; }
81 
82  std::string file_name (void) const
83  { return file; }
84 
85  size_t num_fcn_names (void) const { return fcn_names.size (); }
86 
87  std::list<std::string> function_names (void) const;
88 
89  void add_fcn_name (const std::string&);
90 
91  bool remove_fcn_name (const std::string&);
92 
93  void clear_fcn_names (void) { fcn_names.clear (); }
94 
95  public:
96 
98 
99  protected:
100 
101  void fake_reload (void);
102 
105 
106  // Set of hooked function names.
107  typedef std::map<std::string, size_t>::iterator fcn_names_iterator;
108  typedef std::map<std::string, size_t>::const_iterator fcn_names_const_iterator;
109 
110  std::map<std::string, size_t> fcn_names;
111 
112  static std::map<std::string, dynlib_rep *> instances;
114  };
115 
116  private:
117 
119 
120  public:
121 
122  dynamic_library (void) : rep (&nil_rep) { rep->count++; }
123 
124  dynamic_library (const std::string& f, bool fake = true)
125  : rep (dynlib_rep::get_instance (f, fake)) { }
126 
128  {
129  if (--rep->count == 0)
130  delete rep;
131  }
132 
134  : rep (sl.rep)
135  {
136  rep->count++;
137  }
138 
139  dynamic_library& operator = (const dynamic_library& sl)
140  {
141  if (rep != sl.rep)
142  {
143  if (--rep->count == 0)
144  delete rep;
145 
146  rep = sl.rep;
147  rep->count++;
148  }
149 
150  return *this;
151  }
152 
153  bool operator == (const dynamic_library& sl) const
154  { return (rep == sl.rep); }
155 
156  operator bool () const { return rep->is_open (); }
157 
158  void open (const std::string& f)
159  { *this = dynamic_library (f); }
160 
161  std::list<std::string> close (void)
162  {
163  std::list<std::string> removed_fcns = rep->function_names ();
164 
165  rep->clear_fcn_names ();
166 
167  *this = dynamic_library ();
168 
169  return removed_fcns;
170  }
171 
172  void * search (const std::string& nm, name_mangler mangler = nullptr) const
173  {
174  void *f = rep->search (nm, mangler);
175  if (f)
176  rep->add_fcn_name (nm);
177 
178  return f;
179  }
180 
181  void add (const std::string& name)
182  { rep->add_fcn_name (name); }
183 
184  bool remove (const std::string& name)
185  { return rep->remove_fcn_name (name); }
186 
187  size_t number_of_functions_loaded (void) const
188  { return rep->num_fcn_names (); }
189 
190  bool is_out_of_date (void) const
191  { return rep->is_out_of_date (); }
192 
193  std::string file_name (void) const
194  { return rep->file_name (); }
195 
196  sys::time time_loaded (void) const
197  { return rep->time_loaded (); }
198 
199  private:
200 
202  };
203 }
204 
205 #if defined (OCTAVE_USE_DEPRECATED_FUNCTIONS)
206 
207 OCTAVE_DEPRECATED (4.2, "use 'octave::dynamic_library' instead")
208 typedef octave::dynamic_library octave_shlib;
209 
210 #endif
211 
212 #endif
size_t number_of_functions_loaded(void) const
Definition: oct-shlib.h:187
For example cd octave end example noindent changes the current working directory to file
Definition: dirfns.cc:124
std::string file_name(void) const
Definition: oct-shlib.h:193
sys::time time_loaded(void) const
Definition: oct-shlib.h:79
virtual bool is_open(void) const
Definition: oct-shlib.h:66
F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE * f
std::map< std::string, size_t >::const_iterator fcn_names_const_iterator
Definition: oct-shlib.h:108
static std::map< std::string, dynlib_rep * > instances
Definition: oct-shlib.h:112
size_t num_fcn_names(void) const
Definition: oct-shlib.h:85
bool is_out_of_date(void) const
Definition: oct-shlib.h:190
std::map< std::string, size_t > fcn_names
Definition: oct-shlib.h:110
std::list< std::string > close(void)
Definition: oct-shlib.h:161
sys::time time_loaded(void) const
Definition: oct-shlib.h:196
dynamic_library(const std::string &f, bool fake=true)
Definition: oct-shlib.h:124
virtual void * search(const std::string &, name_mangler=nullptr)
Definition: oct-shlib.h:69
nd deftypefn *std::string name
Definition: sysdep.cc:647
OCTAVE_EXPORT octave_value_list isdir nd deftypefn *std::string nm
Definition: utils.cc:975
is false
Definition: cellfun.cc:400
bool operator==(const dim_vector &a, const dim_vector &b)
Definition: dim-vector.h:550
static dynlib_rep nil_rep
Definition: oct-shlib.h:118
void add(const std::string &name)
Definition: oct-shlib.h:181
dynamic_library(const dynamic_library &sl)
Definition: oct-shlib.h:133
void open(const std::string &f)
Definition: oct-shlib.h:158
std::string file_name(void) const
Definition: oct-shlib.h:82
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:888
std::map< std::string, size_t >::iterator fcn_names_iterator
Definition: oct-shlib.h:107
void * search(const std::string &nm, name_mangler mangler=nullptr) const
Definition: oct-shlib.h:172