oct-shlib.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 1999-2012 John W. Eaton
00004 Copyright (C) 2009 VZLU Prague
00005 
00006 This file is part of Octave.
00007 
00008 Octave is free software; you can redistribute it and/or modify it
00009 under the terms of the GNU General Public License as published by the
00010 Free Software Foundation; either version 3 of the License, or (at your
00011 option) any later version.
00012 
00013 Octave is distributed in the hope that it will be useful, but WITHOUT
00014 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with Octave; see the file COPYING.  If not, see
00020 <http://www.gnu.org/licenses/>.
00021 
00022 */
00023 
00024 #if !defined (octave_shlib_h)
00025 #define octave_shlib_h 1
00026 
00027 #include <string>
00028 #include <map>
00029 
00030 #include "oct-time.h"
00031 #include "oct-refcount.h"
00032 
00033 class
00034 OCTAVE_API
00035 octave_shlib
00036 {
00037 public: // FIXME: make this class private?
00038 
00039   typedef std::string (*name_mangler) (const std::string&);
00040   typedef void (*close_hook) (const std::string&);
00041 
00042   class shlib_rep
00043   {
00044   public:
00045 
00046     shlib_rep (void)
00047       : count (1), file (), tm_loaded (time_t ()), fcn_names () { }
00048 
00049   protected:
00050 
00051     shlib_rep (const std::string& f);
00052 
00053   public:
00054 
00055     virtual ~shlib_rep (void)
00056       {
00057         instances.erase (file);
00058       }
00059 
00060     virtual bool is_open (void) const
00061       { return false; }
00062 
00063     virtual void *search (const std::string&, name_mangler = 0)
00064       { return 0; }
00065 
00066     bool is_out_of_date (void) const;
00067 
00068     // This method will be overriden conditionally.
00069     static shlib_rep *new_instance (const std::string& f);
00070 
00071     static shlib_rep *get_instance (const std::string& f, bool fake);
00072 
00073     octave_time time_loaded (void) const
00074       { return tm_loaded; }
00075 
00076     std::string file_name (void) const
00077       { return file; }
00078 
00079     size_t num_fcn_names (void) const { return fcn_names.size (); }
00080 
00081     void add_fcn_name (const std::string&);
00082 
00083     bool remove_fcn_name (const std::string&);
00084 
00085     void do_close_hook (close_hook cl_hook);
00086 
00087   public:
00088 
00089     octave_refcount<int> count;
00090 
00091   protected:
00092 
00093     void fake_reload (void);
00094 
00095     std::string file;
00096     octave_time tm_loaded;
00097 
00098     // Set of hooked function names.
00099     typedef std::map<std::string, size_t>::iterator fcn_names_iterator;
00100     typedef std::map<std::string, size_t>::const_iterator fcn_names_const_iterator;
00101 
00102     std::map<std::string, size_t> fcn_names;
00103 
00104     static std::map<std::string, shlib_rep *> instances;
00105   };
00106 
00107 private:
00108 
00109   static shlib_rep nil_rep;
00110 
00111 public:
00112 
00113   octave_shlib (void) : rep (&nil_rep) { rep->count++; }
00114 
00115   octave_shlib (const std::string& f, bool fake = true)
00116     : rep (shlib_rep::get_instance (f, fake)) { }
00117 
00118   ~octave_shlib (void)
00119     {
00120       if (--rep->count == 0)
00121         delete rep;
00122     }
00123 
00124   octave_shlib (const octave_shlib& sl)
00125     : rep (sl.rep)
00126     {
00127       rep->count++;
00128     }
00129 
00130   octave_shlib& operator = (const octave_shlib& sl)
00131     {
00132       if (rep != sl.rep)
00133         {
00134           if (--rep->count == 0)
00135             delete rep;
00136 
00137           rep = sl.rep;
00138           rep->count++;
00139         }
00140 
00141       return *this;
00142     }
00143 
00144   bool operator == (const octave_shlib& sl) const
00145     { return (rep == sl.rep); }
00146 
00147   operator bool () const { return rep->is_open (); }
00148 
00149   void open (const std::string& f)
00150     { *this = octave_shlib (f); }
00151 
00152   void close (close_hook cl_hook = 0)
00153     {
00154       if (cl_hook)
00155         rep->do_close_hook (cl_hook);
00156 
00157       *this = octave_shlib ();
00158     }
00159 
00160   void *search (const std::string& nm, name_mangler mangler = 0) const
00161     {
00162       void *f = rep->search (nm, mangler);
00163       if (f)
00164         rep->add_fcn_name (nm);
00165 
00166       return f;
00167     }
00168 
00169   void add (const std::string& name)
00170     { rep->add_fcn_name (name); }
00171 
00172   bool remove (const std::string& name)
00173     { return rep->remove_fcn_name (name); }
00174 
00175   size_t number_of_functions_loaded (void) const
00176     { return rep->num_fcn_names (); }
00177 
00178   bool is_out_of_date (void) const
00179     { return rep->is_out_of_date (); }
00180 
00181   std::string file_name (void) const
00182     { return rep->file_name (); }
00183 
00184   octave_time time_loaded (void) const
00185     { return rep->time_loaded (); }
00186 
00187 private:
00188 
00189   shlib_rep *rep;
00190 };
00191 
00192 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines