Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/utility - DynamicLibrary.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 13 19 68.4 %
Date: 2012-04-09 Functions: 6 7 85.7 %
Branches: 5 24 20.8 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright 2006-2009 Savarese Software Research Corporation
       3                 :            :  *
       4                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
       5                 :            :  * you may not use this file except in compliance with the License.
       6                 :            :  * You may obtain a copy of the License at
       7                 :            :  *
       8                 :            :  *     https://www.savarese.com/software/ApacheLicense-2.0
       9                 :            :  *
      10                 :            :  * Unless required by applicable law or agreed to in writing, software
      11                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      12                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      13                 :            :  * See the License for the specific language governing permissions and
      14                 :            :  * limitations under the License.
      15                 :            :  */
      16                 :            : 
      17                 :            : /**
      18                 :            :  * @file
      19                 :            :  * This header defines the DynamicLibrary class.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_UTILITY_DYNAMC_LIBRARY_H
      23                 :            : #define __SSRC_WSPR_UTILITY_DYNAMC_LIBRARY_H
      24                 :            : 
      25                 :            : namespace NS_POSIX {
      26                 :            : #include <dlfcn.h>
      27                 :            : }
      28                 :            : 
      29                 :            : #include <ssrc/wispers/utility/LoadError.h>
      30                 :            : 
      31                 :            : __BEGIN_NS_SSRC_WSPR_UTILITY
      32                 :            : 
      33                 :            : class DynamicLibrary {
      34                 :            :   std::string _filename;
      35                 :            :   void *_handle;
      36                 :            : 
      37                 :            : public:
      38                 :            : 
      39                 :            :   enum Mode {
      40                 :            :     LazyLocal  = RTLD_LAZY | RTLD_LOCAL,
      41                 :            :     LazyGlobal = RTLD_LAZY | RTLD_GLOBAL,
      42                 :            :     NowLocal   = RTLD_NOW | RTLD_LOCAL,
      43                 :            :     NowGlobal  = RTLD_NOW | RTLD_GLOBAL
      44                 :            :   };
      45                 :            : 
      46                 :            :   explicit
      47                 :          3 :   DynamicLibrary(const std::string & filename, const Mode mode = LazyLocal)
      48                 :            :     SSRC_DECL_THROW(LoadError) :
      49   [ +  -  +  - ]:          3 :     _filename(filename), _handle(NS_POSIX::dlopen(filename.c_str(), mode))
      50                 :            :   {
      51         [ -  + ]:          3 :     if(!_handle) {
      52         [ #  # ]:          0 :       std::string error;
      53         [ #  # ]:          0 :       const char *message = NS_POSIX::dlerror();
      54         [ #  # ]:          0 :       if(message)
      55         [ #  # ]:          0 :         error.append(message);
      56                 :            :       else
      57   [ #  #  #  # ]:          0 :         error.append("DynamicLibrary load error: ").append(filename);
      58         [ #  # ]:          0 :       throw LoadError(error);
      59                 :            :     }
      60                 :          3 :   }
      61                 :            : 
      62                 :          6 :   virtual ~DynamicLibrary() {
      63         [ +  - ]:          3 :     NS_POSIX::dlclose(_handle);
      64         [ -  + ]:          3 :   }
      65                 :            : 
      66                 :          1 :   std::string filename() const {
      67                 :          1 :     return _filename;
      68                 :            :   }
      69                 :            : 
      70                 :          5 :   void * symbol(const std::string & name) {
      71                 :          5 :     return NS_POSIX::dlsym(_handle, name.c_str());
      72                 :            :   }
      73                 :            : 
      74                 :            :   template<typename T>
      75                 :          5 :   T symbol(const std::string & name) {
      76                 :          5 :     return reinterpret_cast<T>(symbol(name));
      77                 :            :   }
      78                 :            : };
      79                 :            : 
      80                 :            : __END_NS_SSRC_WSPR_UTILITY
      81                 :            : 
      82                 :            : #endif