fs/sys/fsDll.cpp

Go to the documentation of this file.
00001 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
00002 /*                          _______      ______    ______          __      */
00003 /*  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~    / ____(_)___ / ___ /)  / ____/___  ____/ /__    */
00004 /*       [fsDll]          / /_  / / __ \\__ \|/  / /   / __ \/ __  / _ \   */
00005 /*       rev. 8          / __/ / / /_/ /__/ /   / /___/ /_/ / /_/ /  __/   */
00006 /*    10th Jan 2007     /_/   /_/ ,___/____/    \____/\____/\__,_/\___/    */
00007 /*     [x] stable              /_/ (c) 2004-7 Filip STOKLAS (FipS)         */
00008 /*  ~ ~ ~ ~ ~ ~ ~ ~ ~ ~       http://HOLE.4FipS.com/fips_code.php          */
00009 /*                                                                         */
00010 /* This code is free for personal and commercial use. You may redistribute */
00011 /* it by any means. If you use the code for your own projects please give  */
00012 /* me credit. Please send a bug report. Don't alter or remove this header! */
00013 /*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/
00014 #include <fs/sys/fsDll.h>
00015 
00016 #if defined(UNDER_CE)
00017 #   include <fs/ut/fsStrUt.h> // wide-character string utilities
00018 #endif
00019 
00020 // >>> COMMON SUPPORT BLOCK (rev. 6)
00021 #if defined(FS_HAS_FSASSERT)
00022 #   include <fs/sys/fsAssert.h>
00023 #else
00024 #   include <cassert>
00025 #   if !defined(FS_ASSERT)
00026 #       define FS_ASSERT(exp) assert(exp)
00027 #   endif
00028 #   if !defined(FS_ASSERT_MSG)
00029 #       define FS_ASSERT_MSG(exp, msg) assert(exp && msg)
00030 #   endif
00031 #   if !defined(FS_VERIFY)
00032 #       define FS_VERIFY(exp) \
00033         { bool bExp = !!(exp); assert(bExp && #exp); bExp; }
00034 #   endif
00035 #   if !defined(FS_VERIFY_MSG)
00036 #       define FS_VERIFY_MSG(exp, msg) \
00037         { bool bExp = !!(exp); assert(bExp && #exp && msg); bExp; }
00038 #   endif
00039 #   if !defined(FS_STATIC_ASSERT)
00040 #       define FS_STATIC_ASSERT(exp) { char error[(exp) ? 1 : 0]; error; }
00041 #   endif
00042 #endif
00043 #if !defined(FS_VERIFY_RETURN)
00044 #   define FS_VERIFY_RETURN(exp) \
00045     if(!(exp)) { FS_ASSERT_MSG(0, #exp "<return>"); return; }
00046 #endif
00047 #if !defined(FS_VERIFY_RETURN_VAL)
00048 #   define FS_VERIFY_RETURN_VAL(exp, ret) \
00049     if(!(exp)) { FS_ASSERT_MSG(0, #exp "<returns:>" #ret); return (ret); }
00050 #endif
00051 // <<< COMMON SUPPORT BLOCK (rev. 6)
00052 
00053 #include <string>
00054 
00055 #if defined(_WIN32)
00056 #   include <windows.h>
00057 #elif defined(FS_PLATFORM_LINUX)
00058 #   include <dlfcn.h>
00059 #endif
00060 
00061 // provide detailed memory tracking information
00062 #if defined(FS_HAS_FSMEMMGR)
00063 #   include <fs/sys/fsMemMgr.h>
00064 #endif
00065 //---------------------------------------------------------------------------
00066 using namespace fs::sys;
00067 //---------------------------------------------------------------------------
00068 //--- ANONYMOUS NAMESPACE >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00069 //---------------------------------------------------------------------------
00070 namespace {
00071 //---------------------------------------------------------------------------
00072 //--- CLASS DllImpl_t >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00073 //---------------------------------------------------------------------------
00074 class DllImpl_t
00075 {
00076 public:
00077 
00078     explicit DllImpl_t(const char *pszFile);
00079     ~DllImpl_t();
00080     bool IsValid() const;
00081         
00082     const void * GetSymb(const char *pszName) const;
00083     const char * GetFile() const;
00084 
00085 private:
00086 
00087     DllImpl_t();
00088     DllImpl_t(const DllImpl_t &);
00089     DllImpl_t & operator = (const DllImpl_t &);
00090     
00091     void * Load(const char *pszFile) const;
00092 
00093     void *m_pHandle;
00094     std::string m_strFile;
00095 };
00096 //---------------------------------------------------------------------------
00097 DllImpl_t::DllImpl_t(const char *pszFile):
00098 m_pHandle(0)
00099 {
00100     void *pHandle = Load(pszFile);
00101     FS_VERIFY_RETURN(pHandle);
00102     
00103     //--- TRANSFER THE STATE --- 
00104     
00105     m_pHandle = pHandle;
00106     m_strFile = pszFile;
00107 }
00108 //---------------------------------------------------------------------------
00109 DllImpl_t::~DllImpl_t()
00110 {
00111 #if defined(_WIN32)
00112 
00113     if(m_pHandle)
00114         ::FreeLibrary(static_cast<HINSTANCE>(m_pHandle));
00115 
00116 #elif defined(FS_PLATFORM_LINUX)
00117 
00118     if(m_pHandle)
00119         ::dlclose(m_pHandle);
00120 
00121 #else
00122 #   error Unknown platform!
00123 #endif
00124 }
00125 //---------------------------------------------------------------------------
00126 bool DllImpl_t::IsValid() const
00127 {
00128     return m_pHandle ? true : false;
00129 }
00130 //---------------------------------------------------------------------------
00131 const void * DllImpl_t::GetSymb(const char *pszName) const
00132 {
00133     FS_VERIFY_RETURN_VAL(IsValid(), 0);
00134     FS_VERIFY_RETURN_VAL(pszName, 0);
00135     FS_ASSERT(m_pHandle);
00136     
00137 #if defined(UNDER_CE)
00138     
00139     return ::GetProcAddress(static_cast<HINSTANCE>(m_pHandle),
00140      fs::ut::str::StrHldr_t(pszName).GetStr());
00141 
00142 #elif defined(_WIN32)
00143 
00144     return ::GetProcAddress(static_cast<HINSTANCE>(m_pHandle), pszName);
00145     
00146 #elif defined(FS_PLATFORM_LINUX)
00147 
00148     return ::dlsym(m_pHandle, pszName);
00149     
00150 #else
00151 #   error Unknown platform!
00152 #endif
00153 }
00154 //---------------------------------------------------------------------------
00155 const char * DllImpl_t::GetFile() const
00156 {
00157     FS_VERIFY_RETURN_VAL(IsValid(), 0);
00158     
00159     return m_strFile.c_str();
00160 }
00161 //---------------------------------------------------------------------------
00162 void * DllImpl_t::Load(const char *pszFile) const
00163 {
00164     FS_VERIFY_RETURN_VAL(pszFile, 0);
00165     
00166     void *pHandle = 0;
00167     
00168 #if defined(UNDER_CE)
00169 
00170     pHandle = ::LoadLibrary(fs::ut::str::WStrHldr_t(pszFile).GetWStr());
00171 
00172 #elif defined(_WIN32)
00173 
00174     pHandle = ::LoadLibraryA(pszFile);
00175 
00176 #elif defined(FS_PLATFORM_LINUX)
00177 
00178     pHandle = ::dlopen(pszFile, RTLD_LAZY);
00179 
00180 #else
00181 #   error Unknown platform!
00182 #endif
00183 
00184     return pHandle;
00185 }
00186 //---------------------------------------------------------------------------
00187 } // anonymous namespace
00188 //---------------------------------------------------------------------------
00189 //--- CLASS Dll_t >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
00190 //---------------------------------------------------------------------------
00191 Dll_t::Dll_t(const char *pszFile):
00192 m_pImpl(new DllImpl_t(pszFile))
00193 {
00194     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00195 }
00196 //---------------------------------------------------------------------------
00197 Dll_t::~Dll_t()
00198 {
00199     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00200     delete static_cast<DllImpl_t *>(m_pImpl);
00201 }
00202 //---------------------------------------------------------------------------
00203 bool Dll_t::IsValid() const
00204 {
00205     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00206     return static_cast<DllImpl_t *>(m_pImpl)->IsValid();
00207 }
00208 //---------------------------------------------------------------------------
00209 const void * Dll_t::GetSymb(const char *pszName) const
00210 {
00211     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00212     return static_cast<DllImpl_t *>(m_pImpl)->GetSymb((pszName));
00213 }
00214 //---------------------------------------------------------------------------
00215 const char * Dll_t::GetFile() const
00216 {
00217     FS_ASSERT_MSG(m_pImpl, "Invalid implementation pointer!");
00218     return static_cast<DllImpl_t *>(m_pImpl)->GetFile();
00219 }
00220 //---------------------------------------------------------------------------

FipS' Code (Thu Feb 15 22:43:35 2007) - (c) Filip STOKLAS (FipS) - [ www ] [ Guest Book ]