00001
00011 #ifndef CXXUTILS_SEAL_SHAREDLIB_H // wlav SEAL_BASE_SHARED_LIBRARY_H
00012 #define CXXUTILS_SEAL_SHAREDLIB_H // wlav SEAL_BASE_SHARED_LIBRARY_H
00013
00014
00015
00016
00017
00018 # include <string>
00019 # include <list>
00020 # include <exception>
00021
00022
00023 namespace Athena {
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 template <class T1>
00035 class Callback1Rep
00036 {
00037 public:
00038 Callback1Rep (void) : m_refs (0) { }
00039 virtual ~Callback1Rep (void) { }
00040
00041 virtual void call (T1) = 0;
00042 virtual bool equal (const Callback1Rep *x) const = 0;
00043
00044 void ref (void) { ++m_refs; }
00045 void unref (void) { if (--m_refs == 0) delete this; }
00046
00047 private:
00048 int m_refs;
00049 };
00050
00051 template <class T1>
00052 class Callback1
00053 {
00054 public:
00055 Callback1 (Callback1Rep<T1> *implementation = 0);
00056 Callback1 (const Callback1 &x);
00057 ~Callback1 (void);
00058 Callback1 & operator= (const Callback1 &x);
00059
00060 bool operator== (const Callback1 &x) const;
00061 operator bool (void) const;
00062 void operator() (T1) const;
00063
00064 private:
00065 Callback1Rep<T1> *m_rep;
00066 };
00067
00068 template <class T1, class T2>
00069 class CallbackImpF11 : public Callback1Rep<T1>
00070 {
00071 typedef CallbackImpF11 self;
00072 public:
00073 CallbackImpF11 (void (*function) (T1, T2),
00074 const T2 &fill_2)
00075 : m_function (function),
00076 m_fill_2 (fill_2)
00077 { }
00078
00079 virtual void call (T1 a)
00080 { (*m_function) (a, m_fill_2); }
00081
00082 virtual bool equal (const Callback1Rep<T1> *other) const
00083 { const self *x = dynamic_cast<const self *> (other);
00084 return x && x->m_function == m_function && x->m_fill_2 == m_fill_2; }
00085
00086 private:
00087 void (*m_function) (T1, T2);
00088 T2 m_fill_2;
00089 };
00090
00091 template <class T1>
00092 inline
00093 Callback1<T1>::Callback1 (Callback1Rep<T1> *implementation )
00094 : m_rep (implementation)
00095 { if (m_rep) m_rep->ref (); }
00096
00097 template <class T1>
00098 inline
00099 Callback1<T1>::Callback1 (const Callback1<T1> &x)
00100 : m_rep (x.m_rep)
00101 { if (m_rep) m_rep->ref (); }
00102
00103 template <class T1>
00104 inline
00105 Callback1<T1>::~Callback1 (void)
00106 { if (m_rep) m_rep->unref (); }
00107
00108 template <class T1>
00109 inline Callback1<T1> &
00110 Callback1<T1>::operator= (const Callback1<T1> &x)
00111 {
00112 if (m_rep != x.m_rep)
00113 {
00114 if (m_rep) m_rep->unref ();
00115 m_rep = x.m_rep;
00116 if (m_rep) m_rep->ref ();
00117 }
00118 return *this;
00119 }
00120
00121 template <class T1>
00122 inline bool
00123 Callback1<T1>::operator== (const Callback1<T1> &x) const
00124 { return m_rep == x.m_rep || (m_rep && x.m_rep && m_rep->equal (x.m_rep)); }
00125
00126 template <class T1>
00127 inline
00128 Callback1<T1>::operator bool (void) const
00129 { return m_rep ? true : false; }
00130
00131 template <class T1>
00132 inline void
00133 Callback1<T1>::operator() (T1 a) const
00134 { m_rep->call (a); }
00135
00136 template <class T1, class T2>
00137 inline Callback1Rep<T1> *
00138 CreateCallback (void (*function) (T1, T2),
00139 const T2 &fill_2)
00140 { return new CallbackImpF11<T1,T2> (function, fill_2); }
00141
00142
00143
00145 class SharedLibraryError : public std::exception
00146 {
00147 public:
00148 SharedLibraryError( const char *context, const std::string &cause );
00149 virtual ~SharedLibraryError() throw() {}
00150
00151 virtual const char* what() const throw();
00152
00153 private:
00154 std::string m_context;
00155 std::string m_cause;
00156 };
00157
00158
00160 class SharedLibrary
00161 {
00162 public:
00163 typedef void * Data;
00164 typedef void (*Function) (void);
00165
00167 struct LibraryInfo
00168 {
00169 unsigned long m_text_start;
00170 unsigned long m_text_end;
00171 unsigned long m_data_start;
00172 unsigned long m_data_end;
00173 unsigned long m_bss_start;
00174 unsigned long m_bss_end;
00175 const char *m_filename;
00176 };
00177
00178 typedef Callback1<const LibraryInfo &> InfoHandler;
00179
00180 static std::string path (void);
00181 static void path (const std::string &path);
00182 static std::string libname (const std::string &name);
00183 static std::string symname (const std::string &name);
00184
00185 static SharedLibrary * self (void);
00186 static SharedLibrary * load (const std::string &name);
00187 static void loaded (const InfoHandler &handler);
00188
00189 void release (void);
00190 void abandon (void);
00191
00192 Data data (const std::string &name, bool mangle = true) const;
00193 Function function (const std::string &name, bool mangle = true) const;
00194
00195 protected:
00196 SharedLibrary (void *handle);
00197 ~SharedLibrary (void);
00198
00199 private:
00200 void *m_handle;
00201
00202
00203 SharedLibrary (const SharedLibrary &);
00204 SharedLibrary &operator= (const SharedLibrary &);
00205 };
00206
00207
00208
00209
00210
00211 }
00212 #endif // CXXUTILS_SEAL_SHAREDLIB_H wlav SEAL_BASE_SHARED_LIBRARY_H