Skip to content

Commit 8fbe178

Browse files
committed
improve dynamic loading example
1 parent 8e929d6 commit 8fbe178

File tree

12 files changed

+212
-186
lines changed

12 files changed

+212
-186
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ message(STATUS "Building dynamic_shared loading examples")
88
# ------------------------------------------------------------------------------
99
# static linking
1010

11-
add_library(shared SHARED overriders.cpp tiger.cpp)
11+
add_library(shared SHARED extensions.cpp)
1212
target_link_libraries(shared Boost::openmethod)
1313
set_target_properties(shared PROPERTIES ENABLE_EXPORTS ON)
1414

@@ -28,7 +28,7 @@ add_test(NAME dynamic_shared COMMAND dynamic)
2828
# ------------------------------------------------------------------------------
2929
# dynamic loading, indirect virtual_ptrs
3030

31-
add_library(indirect_shared SHARED indirect_overriders.cpp indirect_tiger.cpp)
31+
add_library(indirect_shared SHARED indirect_extensions.cpp)
3232
target_compile_definitions(
3333
indirect_shared PUBLIC BOOST_OPENMETHOD_DEFAULT_REGISTRY=indirect_registry)
3434
target_link_libraries(indirect_shared Boost::openmethod Boost::dll)

doc/modules/ROOT/examples/shared_libs/animals.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
// tag::content[]
1212
// animals.hpp
13+
1314
#include <string>
1415
#include <boost/openmethod.hpp>
1516

@@ -22,7 +23,6 @@ BOOST_OPENMETHOD(
2223
boost::openmethod::virtual_ptr<Animal>,
2324
boost::openmethod::virtual_ptr<Animal>),
2425
std::string);
25-
2626
// end::content[]
2727

2828
#endif

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

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,25 @@
33
// accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
55

6-
// tag::main[]
6+
#ifndef LIBRARY_NAME
7+
#ifdef _MSC_VER
8+
#define LIBRARY_NAME "shared.dll"
9+
#else
10+
#define LIBRARY_NAME "libshared.so"
11+
#endif
12+
#endif
713

8-
// dl_main.cpp
14+
// tag::before[]
15+
// dynamic_main.cpp
916

1017
#include "animals.hpp"
1118

1219
#include <boost/openmethod.hpp>
1320
#include <boost/openmethod/initialize.hpp>
14-
#include <boost/openmethod/interop/std_unique_ptr.hpp>
15-
1621
#include <boost/dll/shared_library.hpp>
1722
#include <boost/dll/runtime_symbol_info.hpp>
18-
1923
#include <iostream>
24+
#include <memory>
2025

2126
using namespace boost::openmethod::aliases;
2227

@@ -30,64 +35,68 @@ BOOST_OPENMETHOD_OVERRIDE(
3035
return "greet";
3136
}
3237

33-
// end::main[]
34-
35-
// 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-
45-
auto main() -> int {
46-
using namespace boost::openmethod::aliases;
47-
38+
// tag::load[]
39+
int main() {
40+
// end::load[]
41+
// end::unload[]
4842
std::cout << "Before loading the shared library.\n";
43+
4944
initialize();
5045

51-
{
52-
auto gracie = make_unique_virtual<Cow>();
53-
auto willy = make_unique_virtual<Wolf>();
46+
std::cout << "cow meets wolf -> "
47+
<< meet(*std::make_unique<Cow>(), *std::make_unique<Wolf>())
48+
<< "\n"; // greet
49+
std::cout << "wolf meets cow -> "
50+
<< meet(*std::make_unique<Wolf>(), *std::make_unique<Cow>())
51+
<< "\n"; // greet
52+
53+
// to be continued...
54+
// end::before[]
55+
// tag::load[]
56+
// ...
5457

55-
std::cout << "cow meets wolf -> " << meet(*gracie, *willy) << "\n";
56-
std::cout << "wolf meets cow -> " << meet(*willy, *gracie) << "\n";
57-
}
58+
std::cout << "\nAfter loading the shared library.\n";
5859

5960
boost::dll::shared_library lib(
6061
boost::dll::program_location().parent_path() / LIBRARY_NAME,
6162
boost::dll::load_mode::rtld_now);
62-
63-
std::cout << "\nAfter loading the shared library.\n";
64-
6563
initialize();
6664

67-
{
68-
auto gracie = make_unique_virtual<Cow>();
69-
auto willy = make_unique_virtual<Wolf>();
65+
std::cout << "cow meets wolf -> "
66+
<< meet(*std::make_unique<Cow>(), *std::make_unique<Wolf>())
67+
<< "\n"; // run
68+
std::cout << "wolf meets cow -> "
69+
<< meet(*std::make_unique<Wolf>(), *std::make_unique<Cow>())
70+
<< "\n"; // hunt
7071

71-
std::cout << "cow meets wolf -> " << meet(*gracie, *willy) << "\n";
72-
std::cout << "wolf meets cow -> " << meet(*willy, *gracie) << "\n";
72+
auto make_tiger = lib.get<Animal*()>("make_tiger");
73+
std::cout << "cow meets tiger -> "
74+
<< meet(
75+
*std::make_unique<Cow>(),
76+
*std::unique_ptr<Animal>(make_tiger()))
77+
<< "\n"; // hunt
78+
// end::load[]
7379

74-
auto make_tiger = lib.get<Animal*()>("make_tiger");
75-
std::unique_ptr<Animal> hobbes{make_tiger()};
76-
std::cout << "cow meets tiger -> " << meet(*gracie, *hobbes) << "\n";
77-
}
80+
// tag::unload[]
81+
// ...
7882

7983
std::cout << "\nAfter unloading the shared library.\n";
80-
lib.unload();
8184

85+
lib.unload();
8286
initialize();
8387

84-
{
85-
std::unique_ptr<Animal> gracie(new Cow());
86-
std::unique_ptr<Animal> willy(new Wolf());
87-
88-
std::cout << "cow meets wolf -> " << meet(*gracie, *willy) << "\n";
89-
std::cout << "wolf meets cow -> " << meet(*willy, *gracie) << "\n";
90-
}
88+
std::cout << "cow meets wolf -> "
89+
<< meet(*std::make_unique<Cow>(), *std::make_unique<Wolf>())
90+
<< "\n"; // greet
91+
std::cout << "wolf meets cow -> "
92+
<< meet(*std::make_unique<Wolf>(), *std::make_unique<Cow>())
93+
<< "\n"; // greet
94+
// tag::before[]
95+
// tag::load[]
96+
// tag::unload[]
9197

9298
return 0;
9399
}
100+
// end::before[]
101+
// end::load[]
102+
// end::unload[]

doc/modules/ROOT/examples/shared_libs/overriders.cpp renamed to doc/modules/ROOT/examples/shared_libs/extensions.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
// http://www.boost.org/LICENSE_1_0.txt)
55

66
// tag::content[]
7-
// overriders.cpp
7+
// extensions.cpp
88
#include "animals.hpp"
99

1010
using namespace boost::openmethod::aliases;
@@ -18,4 +18,17 @@ BOOST_OPENMETHOD_OVERRIDE(
1818
meet, (virtual_ptr<Carnivore>, virtual_ptr<Herbivore>), std::string) {
1919
return "hunt";
2020
}
21+
22+
struct Tiger : Carnivore {};
23+
24+
BOOST_OPENMETHOD_CLASSES(Tiger, Carnivore);
25+
26+
extern "C" {
27+
#ifdef _WIN32
28+
__declspec(dllexport)
29+
#endif
30+
auto make_tiger() -> Animal* {
31+
return new Tiger;
32+
}
33+
}
2134
// end::content[]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "extensions.cpp"

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

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,26 @@
33
// accompanying file LICENSE_1_0.txt or copy at
44
// http://www.boost.org/LICENSE_1_0.txt)
55

6-
// tag::main[]
6+
#ifdef _MSC_VER
7+
#define LIBRARY_NAME "indirect_shared.dll"
8+
#else
9+
#define LIBRARY_NAME "libindirect_shared.so"
10+
#endif
711

8-
// dl_main.cpp
12+
// tag::content[]
13+
// indirect_main.cpp
914

1015
#include "animals.hpp"
1116

1217
#include <boost/openmethod.hpp>
1318
#include <boost/openmethod/initialize.hpp>
1419
#include <boost/openmethod/interop/std_unique_ptr.hpp>
15-
1620
#include <boost/dll/shared_library.hpp>
1721
#include <boost/dll/runtime_symbol_info.hpp>
18-
1922
#include <iostream>
2023

2124
using namespace boost::openmethod::aliases;
2225

23-
#ifdef _MSC_VER
24-
#define LIBRARY_NAME "indirect_shared.dll"
25-
#else
26-
#define LIBRARY_NAME "libindirect_shared.so"
27-
#endif
28-
2926
struct Cow : Herbivore {};
3027
struct Wolf : Carnivore {};
3128

@@ -36,9 +33,6 @@ BOOST_OPENMETHOD_OVERRIDE(
3633
return "greet";
3734
}
3835

39-
// end::main[]
40-
41-
// tag::before_dlopen[]
4236
auto main() -> int {
4337
using namespace boost::openmethod::aliases;
4438

@@ -48,33 +42,23 @@ auto main() -> int {
4842
auto gracie = make_unique_virtual<Cow>();
4943
auto willy = make_unique_virtual<Wolf>();
5044

51-
std::cout << "cow meets wolf -> " << meet(*gracie, *willy) << "\n";
52-
std::cout << "wolf meets cow -> " << meet(*willy, *gracie) << "\n";
45+
std::cout << "cow meets wolf -> " << meet(*gracie, *willy) << "\n"; // greet
46+
std::cout << "wolf meets cow -> " << meet(*willy, *gracie) << "\n"; // greet
47+
std::cout << "cow.vptr() = " << gracie.vptr() << "\n"; // 0x5d3121d22be8
48+
std::cout << "wolf.vptr() = " << willy.vptr() << "\n"; // 0x5d3121d22bd8
49+
50+
std::cout << "\nAfter loading the shared library.\n";
5351

5452
boost::dll::shared_library lib(
5553
boost::dll::program_location().parent_path() / LIBRARY_NAME,
5654
boost::dll::load_mode::rtld_now);
5755

58-
std::cout << "\nAfter loading the shared library.\n";
59-
60-
initialize();
61-
62-
std::cout << "cow meets wolf -> " << meet(*gracie, *willy) << "\n";
63-
std::cout << "wolf meets cow -> " << meet(*willy, *gracie) << "\n";
64-
65-
{
66-
auto make_tiger = lib.get<Animal*()>("make_tiger");
67-
std::unique_ptr<Animal> hobbes{make_tiger()};
68-
std::cout << "cow meets tiger -> " << meet(*gracie, *hobbes) << "\n";
69-
}
70-
71-
std::cout << "\nAfter unloading the shared library.\n";
72-
lib.unload();
73-
7456
initialize();
7557

76-
std::cout << "cow meets wolf -> " << meet(*gracie, *willy) << "\n";
77-
std::cout << "wolf meets cow -> " << meet(*willy, *gracie) << "\n";
58+
std::cout << "cow meets wolf -> " << meet(*gracie, *willy) << "\n"; // run
59+
std::cout << "wolf meets cow -> " << meet(*willy, *gracie) << "\n"; // hunt
60+
std::cout << "cow.vptr() = " << gracie.vptr() << "\n"; // 0x5d3121d21998
61+
std::cout << "wolf.vptr() = " << willy.vptr() << "\n"; // 0x5d3121d21988
7862

7963
return 0;
8064
}

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 0 additions & 1 deletion
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// http://www.boost.org/LICENSE_1_0.txt)
55

66
// tag::content[]
7-
87
// static_main.cpp
8+
99
#include "animals.hpp"
1010
#include <boost/openmethod/initialize.hpp>
1111
#include <iostream>

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

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)