GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
oct-shlib.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1999-2013 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 the
10 Free Software Foundation; either version 3 of the License, or (at your
11 option) any later version.
12 
13 Octave is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 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 <http://www.gnu.org/licenses/>.
21 
22 */
23 
24 #if !defined (octave_oct_shlib_h)
25 #define octave_oct_shlib_h 1
26 
27 #include <string>
28 #include <map>
29 
30 #include "oct-time.h"
31 #include "oct-refcount.h"
32 
33 class
34 OCTAVE_API
36 {
37 public: // FIXME: make this class private?
38 
39  typedef std::string (*name_mangler) (const std::string&);
40  typedef void (*close_hook) (const std::string&);
41 
42  class shlib_rep
43  {
44  public:
45 
46  shlib_rep (void)
47  : count (1), file (), tm_loaded (time_t ()), fcn_names () { }
48 
49  protected:
50 
51  shlib_rep (const std::string& f);
52 
53  public:
54 
55  virtual ~shlib_rep (void)
56  {
57  instances.erase (file);
58  }
59 
60  virtual bool is_open (void) const
61  { return false; }
62 
63  virtual void *search (const std::string&, name_mangler = 0)
64  { return 0; }
65 
66  bool is_out_of_date (void) const;
67 
68  // This method will be overriden conditionally.
69  static shlib_rep *new_instance (const std::string& f);
70 
71  static shlib_rep *get_instance (const std::string& f, bool fake);
72 
73  octave_time time_loaded (void) const
74  { return tm_loaded; }
75 
76  std::string file_name (void) const
77  { return file; }
78 
79  size_t num_fcn_names (void) const { return fcn_names.size (); }
80 
81  void add_fcn_name (const std::string&);
82 
83  bool remove_fcn_name (const std::string&);
84 
85  void do_close_hook (close_hook cl_hook);
86 
87  public:
88 
90 
91  protected:
92 
93  void fake_reload (void);
94 
95  std::string file;
97 
98  // Set of hooked function names.
99  typedef std::map<std::string, size_t>::iterator fcn_names_iterator;
100  typedef std::map<std::string, size_t>::const_iterator fcn_names_const_iterator;
101 
102  std::map<std::string, size_t> fcn_names;
103 
104  static std::map<std::string, shlib_rep *> instances;
105  };
106 
107 private:
108 
110 
111 public:
112 
113  octave_shlib (void) : rep (&nil_rep) { rep->count++; }
114 
115  octave_shlib (const std::string& f, bool fake = true)
116  : rep (shlib_rep::get_instance (f, fake)) { }
117 
118  ~octave_shlib (void)
119  {
120  if (--rep->count == 0)
121  delete rep;
122  }
123 
125  : rep (sl.rep)
126  {
127  rep->count++;
128  }
129 
130  octave_shlib& operator = (const octave_shlib& sl)
131  {
132  if (rep != sl.rep)
133  {
134  if (--rep->count == 0)
135  delete rep;
136 
137  rep = sl.rep;
138  rep->count++;
139  }
140 
141  return *this;
142  }
143 
144  bool operator == (const octave_shlib& sl) const
145  { return (rep == sl.rep); }
146 
147  operator bool () const { return rep->is_open (); }
148 
149  void open (const std::string& f)
150  { *this = octave_shlib (f); }
151 
152  void close (close_hook cl_hook = 0)
153  {
154  if (cl_hook)
155  rep->do_close_hook (cl_hook);
156 
157  *this = octave_shlib ();
158  }
159 
160  void *search (const std::string& nm, name_mangler mangler = 0) const
161  {
162  void *f = rep->search (nm, mangler);
163  if (f)
164  rep->add_fcn_name (nm);
165 
166  return f;
167  }
168 
169  void add (const std::string& name)
170  { rep->add_fcn_name (name); }
171 
172  bool remove (const std::string& name)
173  { return rep->remove_fcn_name (name); }
174 
175  size_t number_of_functions_loaded (void) const
176  { return rep->num_fcn_names (); }
177 
178  bool is_out_of_date (void) const
179  { return rep->is_out_of_date (); }
180 
181  std::string file_name (void) const
182  { return rep->file_name (); }
183 
184  octave_time time_loaded (void) const
185  { return rep->time_loaded (); }
186 
187 private:
188 
190 };
191 
192 #endif