LCOV - code coverage report
Current view: top level - lunchbox - dso.cpp (source / functions) Hit Total Coverage
Test: Lunchbox Lines: 37 41 90.2 %
Date: 2018-10-03 05:33:11 Functions: 11 11 100.0 %

          Line data    Source code
       1             : 
       2             : /* Copyright (c) 2009, Cedric Stalder <cedric.stalder@gmail.com>
       3             :  *               2009-2014, Stefan Eilemann <eile@equalizergraphics.com>
       4             :  *
       5             :  * This library is free software; you can redistribute it and/or modify it under
       6             :  * the terms of the GNU Lesser General Public License version 2.1 as published
       7             :  * by the Free Software Foundation.
       8             :  *
       9             :  * This library is distributed in the hope that it will be useful, but WITHOUT
      10             :  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
      11             :  * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
      12             :  * details.
      13             :  *
      14             :  * You should have received a copy of the GNU Lesser General Public License
      15             :  * along with this library; if not, write to the Free Software Foundation, Inc.,
      16             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      17             :  */
      18             : 
      19             : #include "dso.h"
      20             : 
      21             : #include "debug.h"
      22             : #include "log.h"
      23             : #include "os.h"
      24             : 
      25             : #ifdef _WIN32 //_MSC_VER
      26             : #define LB_DL_ERROR sysError
      27             : #else
      28             : #include <dlfcn.h>
      29             : #define LB_DL_ERROR dlerror()
      30             : #endif
      31             : 
      32             : namespace lunchbox
      33             : {
      34             : namespace detail
      35             : {
      36             : class DSO
      37             : {
      38             : public:
      39           3 :     DSO()
      40           3 :         : dso(0)
      41             :     {
      42           3 :     }
      43             : 
      44             : #ifdef _WIN32 //_MSC_VER
      45             :     HMODULE dso;
      46             : #else
      47             :     void* dso;
      48             : #endif
      49             : };
      50             : }
      51             : 
      52           1 : DSO::DSO()
      53           1 :     : _impl(new detail::DSO)
      54             : {
      55           1 : }
      56             : 
      57           2 : DSO::DSO(const std::string& name)
      58           2 :     : _impl(new detail::DSO)
      59             : {
      60           2 :     open(name);
      61           2 : }
      62             : 
      63           6 : DSO::~DSO()
      64             : {
      65           3 :     close();
      66           3 :     delete _impl;
      67           3 : }
      68             : 
      69           4 : bool DSO::open(const std::string& fileName)
      70             : {
      71           4 :     if (_impl->dso)
      72             :     {
      73           1 :         LBWARN << "DSO already open, close it first" << std::endl;
      74           1 :         return false;
      75             :     }
      76             : 
      77           3 :     if (fileName.empty())
      78             :     {
      79             : #ifdef _WIN32 //_MSC_VER
      80             :         _impl->dso = GetModuleHandle(0);
      81             :         LBASSERT(_impl->dso);
      82             : #else
      83           0 :         _impl->dso = RTLD_DEFAULT;
      84             : #endif
      85             :     }
      86             :     else
      87             :     {
      88             : #ifdef _WIN32 //_MSC_VER
      89             :         _impl->dso = LoadLibrary(fileName.c_str());
      90             : #elif defined(RTLD_LOCAL)
      91           3 :         _impl->dso = dlopen(fileName.c_str(), RTLD_LAZY | RTLD_LOCAL);
      92             : #else
      93             :         _impl->dso = dlopen(fileName.c_str(), RTLD_LAZY);
      94             : #endif
      95           3 :         if (!_impl->dso)
      96             :         {
      97           0 :             LBDEBUG << "Can't open library " << fileName << ": " << LB_DL_ERROR
      98           0 :                     << std::endl;
      99           0 :             return false;
     100             :         }
     101             :     }
     102             : 
     103           3 :     return true;
     104             : }
     105             : 
     106           5 : void DSO::close()
     107             : {
     108           5 :     if (!_impl->dso)
     109           2 :         return;
     110             : 
     111             : #ifdef _WIN32 //_MSC_VER
     112             :     if (_impl->dso != GetModuleHandle(0))
     113             :         FreeLibrary(_impl->dso);
     114             : #else
     115           3 :     if (_impl->dso != RTLD_DEFAULT)
     116           3 :         dlclose(_impl->dso);
     117             : #endif
     118             : 
     119           3 :     _impl->dso = 0;
     120             : }
     121             : 
     122           4 : void* DSO::getFunctionPointer(const std::string& name) const
     123             : {
     124           4 :     if (!_impl->dso)
     125           1 :         return 0;
     126             : 
     127             : #ifdef _WIN32 //_MSC_VER
     128             :     return (void*)GetProcAddress(_impl->dso, name.c_str());
     129             : #else
     130           3 :     return dlsym(_impl->dso, name.c_str());
     131             : #endif
     132             : }
     133             : 
     134           3 : bool DSO::isOpen() const
     135             : {
     136           3 :     return _impl->dso != 0;
     137             : }
     138             : 
     139           8 : bool DSO::operator==(const DSO& rhs) const
     140             : {
     141           8 :     return _impl->dso == rhs._impl->dso;
     142             : }
     143          75 : }

Generated by: LCOV version 1.11