Web Wispers 1.2.2 C++ Unit Test Coverage
Current view: top level - ssrc/wispers/lua - LoadConfig.h (source / functions) Hit Total Coverage
Test: Web Wispers 1.2.2 C++ Unit Tests Lines: 10 14 71.4 %
Date: 2012-04-09 Functions: 4 4 100.0 %
Branches: 11 30 36.7 %

           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 This header defines a function for dynamically loading properties
      19                 :            :  *       files from programs not linked against libwspr-lua and liblua.
      20                 :            :  */
      21                 :            : 
      22                 :            : #ifndef __SSRC_WSPR_LUA_LOAD_CONFIG_H
      23                 :            : #define __SSRC_WSPR_LUA_LOAD_CONFIG_H
      24                 :            : 
      25                 :            : #include <boost/filesystem/path.hpp>
      26                 :            : 
      27                 :            : #include <ssrc/wispers/utility/DynamicLibrary.h>
      28                 :            : #include <ssrc/wispers/lua/Properties.h>
      29                 :            : 
      30                 :            : __BEGIN_NS_SSRC_WSPR_LUA
      31                 :            : 
      32                 :            : extern "C" typedef properties_ptr fun_type(const std::string &);
      33                 :            : extern "C" typedef fun_type* fun_ptr;
      34                 :            : 
      35                 :            : /**
      36                 :            :  * Dynamically loads the code necessary to read a configuration file,
      37                 :            :  * reads the configuration file, converts it to a properties_ptr, and
      38                 :            :  * unloads the code.  This function is intended for use by programs that
      39                 :            :  * don't use Lua for anything other than reading configuration files.
      40                 :            :  */
      41                 :          2 : class LoadConfig {
      42                 :            :   NS_SSRC_WSPR_UTILITY::DynamicLibrary _lib;
      43                 :            :   const fun_ptr _load;
      44                 :            :   const fun_ptr _load_str;
      45                 :            : 
      46                 :            : public:
      47                 :            : 
      48                 :          2 :   LoadConfig(const boost::filesystem::path & module_dir)
      49                 :            :     SSRC_DECL_THROW(NS_SSRC_WSPR_UTILITY::LoadError) :
      50   [ +  -  +  - ]:          4 :     _lib((module_dir / "wspr.dso.load_config.so").string()),
      51   [ +  -  +  -  :          4 :     _load(_lib.symbol<fun_ptr>("ssrc_wispers_load_config")),
                   +  - ]
      52   [ +  -  +  -  :          6 :     _load_str(_lib.symbol<fun_ptr>("ssrc_wispers_load_string_config"))
             +  -  +  - ]
      53                 :          2 :   { }
      54                 :            : 
      55                 :          1 :   inline properties_ptr operator()(const std::string & filename) const
      56                 :            :     SSRC_DECL_THROW(LuaCallError)
      57                 :            :   {
      58                 :            :     try {
      59         [ +  - ]:          1 :       return _load(filename);
      60         [ #  # ]:          0 :     } catch(const LuaCallError & lce) {
      61                 :            :       // Re-throw a copy to avoid possible segfault if library is unloaded
      62                 :            :       // before next catch..
      63         [ #  # ]:          0 :       throw LuaCallError(lce);
      64                 :            :     }
      65                 :            :   }
      66                 :            : 
      67                 :          1 :   inline properties_ptr load_from_string(const std::string & code_str) const
      68                 :            :     SSRC_DECL_THROW(LuaCallError)
      69                 :            :   {
      70                 :            :     try {
      71         [ +  - ]:          1 :       return _load_str(code_str);
      72         [ #  # ]:          0 :     } catch(const LuaCallError & lce) {
      73                 :            :       // Re-throw a copy to avoid possible segfault if library is unloaded
      74                 :            :       // before next catch..
      75         [ #  # ]:          0 :       throw LuaCallError(lce);
      76                 :            :     }
      77                 :            :   }
      78                 :            : };
      79                 :            : 
      80                 :            : __END_NS_SSRC_WSPR_LUA
      81                 :            : 
      82                 :            : #endif