Skip to content

Commit 102268b

Browse files
committed
improve dynamic loading example
1 parent 7ccb931 commit 102268b

File tree

5 files changed

+22
-15
lines changed

5 files changed

+22
-15
lines changed

doc/modules/ROOT/examples/shared_libs/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ message(STATUS "Building dynamic_shared loading examples")
99
# dynamic loading, direct virtual_ptrs
1010

1111
add_library(shared SHARED overriders.cpp tiger.cpp)
12-
target_link_libraries(shared Boost::openmethod)
12+
target_link_libraries(shared Boost::openmethod Boost::dll)
1313
set_target_properties(shared PROPERTIES ENABLE_EXPORTS ON)
1414

1515
add_executable(dynamic dynamic_main.cpp)
@@ -24,7 +24,7 @@ add_test(NAME dynamic_shared COMMAND dynamic)
2424
add_library(indirect_shared SHARED indirect_overriders.cpp indirect_tiger.cpp)
2525
target_compile_definitions(
2626
indirect_shared PUBLIC BOOST_OPENMETHOD_DEFAULT_REGISTRY=indirect_registry)
27-
target_link_libraries(indirect_shared Boost::openmethod)
27+
target_link_libraries(indirect_shared Boost::openmethod Boost::dll)
2828
set_target_properties(indirect_shared PROPERTIES ENABLE_EXPORTS ON)
2929

3030
add_executable(indirect indirect_main.cpp)

doc/modules/ROOT/examples/shared_libs/dynamic_main.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,15 @@ BOOST_OPENMETHOD_OVERRIDE(
3333
// end::main[]
3434

3535
// tag::before_dlopen[]
36+
37+
#ifndef LIBRARY_NAME
38+
#ifdef _MSC_VER
39+
#define LIBRARY_NAME "shared.dll"
40+
#else
41+
#define LIBRARY_NAME "libshared.so"
42+
#endif
43+
#endif
44+
3645
auto main() -> int {
3746
using namespace boost::openmethod::aliases;
3847

@@ -48,7 +57,7 @@ auto main() -> int {
4857
}
4958

5059
boost::dll::shared_library lib(
51-
boost::dll::program_location().parent_path() / "libshared.so",
60+
boost::dll::program_location().parent_path() / LIBRARY_NAME,
5261
boost::dll::load_mode::rtld_now);
5362

5463
std::cout << "\nAfter loading the shared library.\n";
@@ -62,7 +71,7 @@ auto main() -> int {
6271
std::cout << "Gracie meets Willy -> " << meet(*gracie, *willy) << "\n";
6372
std::cout << "Willy meets Gracie -> " << meet(*willy, *gracie) << "\n";
6473

65-
auto make_tiger = lib.get<Animal*()>("make_tiger");
74+
auto make_tiger = lib.get_alias<Animal*()>("make_tiger");
6675
std::unique_ptr<Animal> hobbes{make_tiger()};
6776
std::cout << "Gracie meets Tiger -> " << meet(*gracie, *hobbes) << "\n";
6877
}

doc/modules/ROOT/examples/shared_libs/indirect_main.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@
2020

2121
using namespace boost::openmethod::aliases;
2222

23+
#ifdef _MSC_VER
24+
#define LIBRARY_NAME "indirect_shared.dll"
25+
#else
26+
#define LIBRARY_NAME "libindirect_shared.so"
27+
#endif
28+
2329
struct Cow : Herbivore {};
2430
struct Wolf : Carnivore {};
2531

@@ -46,7 +52,7 @@ auto main() -> int {
4652
std::cout << "Willy meets Gracie -> " << meet(*willy, *gracie) << "\n";
4753

4854
boost::dll::shared_library lib(
49-
boost::dll::program_location().parent_path() / "libindirect_shared.so",
55+
boost::dll::program_location().parent_path() / LIBRARY_NAME,
5056
boost::dll::load_mode::rtld_now);
5157

5258
std::cout << "\nAfter loading the shared library.\n";
@@ -57,7 +63,7 @@ auto main() -> int {
5763
std::cout << "Willy meets Gracie -> " << meet(*willy, *gracie) << "\n";
5864

5965
{
60-
auto make_tiger = lib.get<Animal*()>("make_tiger");
66+
auto make_tiger = lib.get_alias<Animal*()>("make_tiger");
6167
std::unique_ptr<Animal> hobbes{make_tiger()};
6268
std::cout << "Gracie meets Tiger -> " << meet(*gracie, *hobbes) << "\n";
6369
}

doc/modules/ROOT/examples/shared_libs/overriders.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,4 @@ BOOST_OPENMETHOD_OVERRIDE(
2424
return "hunt";
2525
}
2626

27-
struct Tiger : Carnivore {};
28-
29-
BOOST_OPENMETHOD_CLASSES(Tiger, Carnivore);
30-
31-
extern "C" auto make_tiger() -> Tiger* {
32-
return new Tiger;
33-
}
34-
3527
// end::dl_shared[]

include/boost/openmethod/initialize.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ void registry<Policies...>::compiler<Options...>::write_global_data() {
12731273
});
12741274
dispatch_data_size = std::accumulate(
12751275
classes.begin(), classes.end(), dispatch_data_size,
1276-
[](auto sum, const auto& cls) { return sum + cls.vtbl.size(); });
1276+
[](auto sum, auto& cls) { return sum + cls.vtbl.size(); });
12771277

12781278
std::vector<detail::word> new_dispatch_data(dispatch_data_size);
12791279
auto gv_first = new_dispatch_data.data();

0 commit comments

Comments
 (0)