Skip to content

Commit

Permalink
Modernize code and drop Boost.Function dependency (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
apolukhin authored Dec 17, 2024
1 parent c66dfab commit 06bc4c8
Show file tree
Hide file tree
Showing 26 changed files with 96 additions and 135 deletions.
1 change: 0 additions & 1 deletion build.jam
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ constant boost_dependencies :
/boost/config//boost_config
/boost/core//boost_core
/boost/filesystem//boost_filesystem
/boost/function//boost_function
/boost/predef//boost_predef
/boost/smart_ptr//boost_smart_ptr
/boost/spirit//boost_spirit
Expand Down
7 changes: 0 additions & 7 deletions doc/Jamfile.v2
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,8 @@ local doxygen_params =
\"forcedlinkfs{1}=\\xmlonly<link linkend='boost.dll.fs.\\1'>boost::dll::fs::\\1</link>\\endxmlonly\" \\
\"forcedmacrolink{1}=\\xmlonly<link linkend='\\1'>\\1</link>\\endxmlonly\" "
<doxygen:param>"PREDEFINED= \\
\"BOOST_RV_REF(T)=T&&\" \\
\"BOOST_RV_REF(shared_library)=shared_library&&\" \\
\"BOOST_COPY_ASSIGN_REF(shared_library)=const shared_library&\" \\
\"BOOST_MOVABLE_BUT_NOT_COPYABLE(shared_library)= \\
shared_library(const shared_library&) = delete; \\
shared_library& operator=(const shared_library&) = delete; \" \\
\"BOOST_DLL_IMPORT_RESULT_TYPE=result_type\" \\
\"BOOST_DLL_MANGLED_IMPORT_RESULT_TYPE=result_type\" \\
\"BOOST_EXPLICIT_OPERATOR_BOOL()=explicit operator bool() const noexcept;\" \\
\"BOOST_DLL_DOXYGEN\" "
;

Expand Down
3 changes: 1 addition & 2 deletions doc/dependencies.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
The Boost.DLL is a header only library, but it depends on the following libraries
and they must be available in order to compile programs that use Boost.DLL:

* Boost.System for the boost::system::error_code and boost::system::system_error classes.
* Boost.System for the boost::system::system_error class.
* Boost.Filesystem for directory manipulation.

Refcountable part of Boost.DLL also depends on:

* Boost.Function for creation of a callback system.
* Boost.SharedPtr for reference counting.

[endsect]
Expand Down
2 changes: 1 addition & 1 deletion doc/getting_started.qbk
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ boost::dll::shared_library lib("/test/boost/application/libtest_library.so");
Now you can easily import symbols from that library using the `get` and `get_alias` member functions:
```
int plugin_constant = lib.get<const int>("integer_variable");
boost::function<int()> f = lib.get<int()>("function_returning_int");
auto function_ptr = lib.get<int()>("function_returning_int");
int& i = lib.get_alias<int>("alias_to_int_variable");
```
In case of `boost::dll::shared_library` it is safe to use imported symbols only until `boost::dll::shared_library`
Expand Down
5 changes: 2 additions & 3 deletions example/tutorial2/tutorial2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ int main(int argc, char* argv[]) {
/*<-*/ b2_workarounds::argv_to_path_guard guard(argc, argv); /*->*/
boost::dll::fs::path shared_library_path(argv[1]); // argv[1] contains path to directory with our plugin library
shared_library_path /= "my_plugin_aggregator";
typedef boost::shared_ptr<my_plugin_api> (pluginapi_create_t)();
boost::function<pluginapi_create_t> creator;

creator = boost::dll::import_alias<pluginapi_create_t>( // type of imported symbol must be explicitly specified
using pluginapi_create_t = boost::shared_ptr<my_plugin_api>();
auto creator = boost::dll::import_alias<pluginapi_create_t>( // type of imported symbol must be explicitly specified
shared_library_path, // path to library
"create_plugin", // symbol to import
dll::load_mode::append_decorations // do append extensions and prefixes
Expand Down
7 changes: 4 additions & 3 deletions example/tutorial3/tutorial3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@ std::size_t search_for_symbols(const std::vector<boost::dll::fs::path>& plugins)
}

// library has symbol, importing...
typedef boost::shared_ptr<my_plugin_api> (pluginapi_create_t)();
boost::function<pluginapi_create_t> creator
= dll::import_alias<pluginapi_create_t>(std::move(lib), "create_plugin");
using pluginapi_create_t = boost::shared_ptr<my_plugin_api>();
auto creator = dll::import_alias<pluginapi_create_t>(
std::move(lib), "create_plugin"
);

std::cout << "Matching plugin name: " << creator()->name() << std::endl;
++ plugins_found;
Expand Down
3 changes: 1 addition & 2 deletions example/tutorial4/load_self.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ int main() {
dll::shared_library self(dll::program_location());

std::cout << "Call function" << std::endl;
boost::function<boost::shared_ptr<my_plugin_api>()> creator
= self.get_alias<boost::shared_ptr<my_plugin_api>()>("create_plugin");
auto creator = self.get_alias<boost::shared_ptr<my_plugin_api>()>("create_plugin");

std::cout << "Computed Value: " << creator()->calculate(2, 2) << std::endl;
//<-
Expand Down
7 changes: 3 additions & 4 deletions example/tutorial5/load_all.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace dll = boost::dll;

class plugins_collector {
// Name => plugin
typedef boost::container::map<std::string, dll::shared_library> plugins_t;
using plugins_t = boost::container::map<std::string, dll::shared_library>;

boost::dll::fs::path plugins_directory_;
plugins_t plugins_;
Expand Down Expand Up @@ -52,8 +52,7 @@ class plugins_collector {
//[plugcpp_plugins_collector_load_all
void plugins_collector::load_all() {
namespace fs = ::boost::dll::fs;
typedef fs::path::string_type string_type;
const string_type extension = dll::shared_library::suffix().native();
const auto extension = dll::shared_library::suffix().native();

// Searching a folder for files with '.so' or '.dll' extension
fs::recursive_directory_iterator endit;
Expand All @@ -67,7 +66,7 @@ void plugins_collector::load_all() {
}
/*->*/
// We found a file. Trying to load it
boost::dll::fs::error_code error;
std::error_code error;
dll::shared_library plugin(it->path(), error);
if (error) {
continue;
Expand Down
6 changes: 3 additions & 3 deletions example/tutorial6/on_unload_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@

//[plugcpp_on_unload
#include <boost/dll/alias.hpp> // for BOOST_DLL_ALIAS
#include <boost/function.hpp>
#include <functional>
#include <vector>

namespace my_namespace {

struct on_unload {
typedef boost::function<void()> callback_t;
typedef on_unload this_type;
using callback_t = std::function<void()> ;
using this_type = on_unload;

~on_unload() {
for (std::size_t i = 0; i < callbacks_.size(); ++i) {
Expand Down
13 changes: 6 additions & 7 deletions example/tutorial6/tutorial6.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

//[callplugcpp_tutorial6
#include <boost/dll/import.hpp>
#include <boost/function.hpp>
#include <functional>
#include <iostream>

typedef boost::function<void()> callback_t;
using callback_t = std::function<void()> ;

void print_unloaded() {
std::cout << "unloaded" << std::endl;
Expand All @@ -23,16 +23,15 @@ int main(int argc, char* argv[]) {
boost::dll::fs::path shared_library_path = /*<-*/ b2_workarounds::first_lib_from_argv(argc, argv); /*->*/ //=argv[1];

// loading library and getting a function from it
boost::function<void(const callback_t&)> on_unload
= boost::dll::import_alias<void(const callback_t&)>(
shared_library_path, "on_unload"
);
std::function<void(const callback_t&)> on_unload = boost::dll::import_alias<void(const callback_t&)>(
shared_library_path, "on_unload"
);

on_unload(&print_unloaded); // adding a callback
std::cout << "Before library unload." << std::endl;

// Releasing last reference to the library, so that it gets unloaded
on_unload.clear();
on_unload = {};
std::cout << "After library unload." << std::endl;
}
//]
Expand Down
4 changes: 2 additions & 2 deletions example/tutorial8/refcounting_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ inline boost::shared_ptr<my_refcounting_api> bind(my_refcounting_api* plugin) {
inline boost::shared_ptr<my_refcounting_api> get_plugin(
boost::dll::fs::path path, const char* func_name)
{
typedef my_refcounting_api*(func_t)();
boost::function<func_t> creator = boost::dll::import_alias<func_t>(
using func_t = my_refcounting_api*();
auto creator = boost::dll::import_alias<func_t>(
path,
func_name,
boost::dll::load_mode::append_decorations // will be ignored for executable
Expand Down
9 changes: 4 additions & 5 deletions example/tutorial9/tutorial9.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
//[callplugcpp_tutorial9
#include <boost/dll/import.hpp> // for dll::import
#include <boost/dll/shared_library.hpp> // for dll::shared_library
#include <boost/function.hpp>
#include <functional>
#include <iostream>
#include <windows.h>

Expand All @@ -29,15 +29,14 @@ int main() {
);
std::cout << "0.0 GetStdHandle() returned " << get_std_handle(STD_OUTPUT_HANDLE) << std::endl;

// You may put the `get_std_handle` into boost::function<>. But boost::function<Signature> can not compile with
// You may put the `get_std_handle` into std::function<>. But std::function<Signature> may not compile with
// Signature template parameter that contains calling conventions, so you'll have to remove the calling convention.
boost::function<HANDLE(DWORD)> get_std_handle2 = get_std_handle;
std::function<HANDLE(DWORD)> get_std_handle2 = get_std_handle;
std::cout << "0.1 GetStdHandle() returned " << get_std_handle2(STD_OUTPUT_HANDLE) << std::endl;
/*<-*/
#endif /*->*/

// OPTION #1, does not require C++11. But without C++11 dll::import<> can not handle calling conventions,
// so you'll need to hand write the import.
// OPTION #1, hand write the import.
dll::shared_library lib("Kernel32.dll", dll::load_mode::search_system_folders);
GetStdHandle_t& func = lib.get<GetStdHandle_t>("GetStdHandle");

Expand Down
8 changes: 4 additions & 4 deletions include/boost/dll/detail/aggressive_ptr_cast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ namespace boost { namespace dll { namespace detail {
// This method suppress the warnings and ensures that such casts are safe.
template <class To, class From>
BOOST_FORCEINLINE typename boost::disable_if_c<boost::is_member_pointer<To>::value || boost::is_reference<To>::value || boost::is_member_pointer<From>::value, To>::type
aggressive_ptr_cast(From v) BOOST_NOEXCEPT
aggressive_ptr_cast(From v) noexcept
{
static_assert(
boost::is_pointer<To>::value && boost::is_pointer<From>::value,
Expand Down Expand Up @@ -61,7 +61,7 @@ BOOST_FORCEINLINE typename boost::disable_if_c<boost::is_member_pointer<To>::val

template <class To, class From>
BOOST_FORCEINLINE typename boost::disable_if_c<!boost::is_reference<To>::value || boost::is_member_pointer<From>::value, To>::type
aggressive_ptr_cast(From v) BOOST_NOEXCEPT
aggressive_ptr_cast(From v) noexcept
{
static_assert(
boost::is_pointer<From>::value,
Expand Down Expand Up @@ -90,7 +90,7 @@ BOOST_FORCEINLINE typename boost::disable_if_c<!boost::is_reference<To>::value |

template <class To, class From>
BOOST_FORCEINLINE typename boost::disable_if_c<!boost::is_member_pointer<To>::value || boost::is_member_pointer<From>::value, To>::type
aggressive_ptr_cast(From v) BOOST_NOEXCEPT
aggressive_ptr_cast(From v) noexcept
{
static_assert(
boost::is_pointer<From>::value,
Expand All @@ -109,7 +109,7 @@ BOOST_FORCEINLINE typename boost::disable_if_c<!boost::is_member_pointer<To>::va

template <class To, class From>
BOOST_FORCEINLINE typename boost::disable_if_c<boost::is_member_pointer<To>::value || !boost::is_member_pointer<From>::value, To>::type
aggressive_ptr_cast(From /* v */) BOOST_NOEXCEPT
aggressive_ptr_cast(From /* v */) noexcept
{
static_assert(
boost::is_pointer<To>::value,
Expand Down
2 changes: 1 addition & 1 deletion include/boost/dll/detail/elf_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ class elf_info {
read_raw(fs, symbols[0], static_cast<std::size_t>(symtab_size - (symtab_size % sizeof(symbol_t))) );
}

static bool is_visible(const symbol_t& sym) BOOST_NOEXCEPT {
static bool is_visible(const symbol_t& sym) noexcept {
const unsigned char visibility = (sym.st_other & 0x03);
// `(sym.st_info >> 4) != STB_LOCAL_ && !!sym.st_size` check also workarounds the
// GCC's issue https://sourceware.org/bugzilla/show_bug.cgi?id=13621
Expand Down
2 changes: 1 addition & 1 deletion include/boost/dll/detail/pe_info.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ class pe_info {
MSVCR110D.dll
*/
/*
static std::vector<std::string> depend_of(boost::dll::fs::error_code &ec) BOOST_NOEXCEPT {
static std::vector<std::string> depend_of(boost::dll::fs::error_code &ec) noexcept {
std::vector<std::string> ret;
IMAGE_DOS_HEADER* image_dos_header = (IMAGE_DOS_HEADER*)native();
Expand Down
2 changes: 1 addition & 1 deletion include/boost/dll/detail/posix/path_from_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# include <cstddef> // for std::ptrdiff_t

namespace boost { namespace dll { namespace detail {
inline void* strip_handle(void* handle) BOOST_NOEXCEPT {
inline void* strip_handle(void* handle) noexcept {
return reinterpret_cast<void*>(
(reinterpret_cast<std::ptrdiff_t>(handle) >> 2) << 2
);
Expand Down
2 changes: 1 addition & 1 deletion include/boost/dll/detail/system_error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

namespace boost { namespace dll { namespace detail {

inline void reset_dlerror() BOOST_NOEXCEPT {
inline void reset_dlerror() noexcept {
#if !BOOST_OS_WINDOWS
const char* const error_txt = dlerror();
(void)error_txt;
Expand Down
2 changes: 1 addition & 1 deletion include/boost/dll/detail/windows/path_from_handle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

namespace boost { namespace dll { namespace detail {

inline std::error_code last_error_code() BOOST_NOEXCEPT {
inline std::error_code last_error_code() noexcept {
boost::winapi::DWORD_ err = boost::winapi::GetLastError();
return std::error_code(
static_cast<int>(err),
Expand Down
Loading

0 comments on commit 06bc4c8

Please sign in to comment.