Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
BasedOnStyle: llvm
BraceWrapping:
AfterFunction: true
AllowShortFunctionsOnASingleLine: None
BinPackArguments: false
BinPackParameters: false
NamespaceIndentation: All
IndentPPDirectives: AfterHash
ColumnLimit: 80 # Disables column limit to prevent unwanted line wrapping.
CommentPragmas: true # Properly formats comments with pragmas.
SpaceBeforeParens: ControlStatements # Space before parentheses for control statements.
UseTab: Never # Use spaces for indentation instead of tabs.
PointerAlignment: Left
BreakBeforeBraces: Allman
27 changes: 15 additions & 12 deletions examples/advanced/loader.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#include "loader.hpp"
#include "modules/modules.hpp"
#include <cstdio>
#include <dynlib/dynlib.hpp>

namespace UnsafeUDF {
[[nodiscard]] std::shared_ptr<DynamicLibrary> init_lib(std::string_view path) {
auto _handle = DynamicLibrary::getLib(path);
auto _mod = DynamicLibrary::getModule<Module>(_handle);

make_udf = _mod._make_udf_m;
init_udf = _mod._init_udf_m;
delete_udf = _mod._delete_udf_m;
return _handle;
}
#include <loader.hpp>
namespace UnsafeUDF
{
[[nodiscard]] std::shared_ptr<DynamicLibrary> init_lib(std::string_view path)
{
auto _handle = DynamicLibrary::getLib(path);
const auto _mod =
DynamicLibrary::getModule<const Module>(_handle, &default_module);
std::printf("build tag: %lld\r\n", _mod.build_tag);
make_udf = _mod._make_udf_m;
init_udf = _mod._init_udf_m;
delete_udf = _mod._delete_udf_m;
return _handle;
}
} // namespace UnsafeUDF
7 changes: 4 additions & 3 deletions examples/advanced/loader.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#ifndef LOADER_HPP
#define LOADER_HPP
#include <string_view>
#include <memory>
#include <string_view>

#include <dynlib/dynlib.hpp>

namespace UnsafeUDF {
[[nodiscard]] std::shared_ptr<DynamicLibrary> init_lib(std::string_view path);
namespace UnsafeUDF
{
[[nodiscard]] std::shared_ptr<DynamicLibrary> init_lib(std::string_view path);
}

#endif
19 changes: 12 additions & 7 deletions examples/advanced/main.cpp
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
#include "loader.hpp"
#include "modules/modules.hpp"
#include <dynlib/dynlib.hpp>
#include <exception>
#include <stdexcept>
#include <string_view>
#include "loader.hpp"

#define LOAD_LIB(name) auto _ = UnsafeUDF::init_lib(name);

int exec() {
int exec()
{

Model m;

m.init(42);
return 0;
}

int main(int argc, char **argv) {
int main(int argc, char** argv)
{
std::string_view arg0;

if (argc == 2) {
if (argc == 2)
{
arg0 = argv[1];
try {
try
{

LOAD_LIB(arg0);
return exec();
} catch (...) {
}
catch (...)
{
std::cerr
<< "Not default provided and library is not available: early return "
<< std::endl;
Expand Down
28 changes: 16 additions & 12 deletions examples/advanced/modules/default_lib.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
#include "modules.hpp"
#include <iostream>

struct impl {

struct impl
{
};

static void _init_udf(impl&, int a)
{
std::cout << "a=" << a << std::endl;
}

static void _init_udf(impl &, int a) { std::cout << "a=" << a << std::endl; }

static void _make_udf(Model &model) { model.pimpl = new impl; }
static void _make_udf(Model& model)
{
model.pimpl = new impl;
}

static void _delete_udf(impl **pimpl) {
if (pimpl != nullptr) {
static void _delete_udf(impl** pimpl)
{
if (pimpl != nullptr)
{
delete *pimpl;
*pimpl = nullptr;
}
}


#ifdef DEFAULT_MODULE
EXPORT_MODULE(default_module,&_init_udf, &_make_udf,
&_delete_udf);
EXPORT_MODULE(default_module, &_init_udf, &_make_udf, &_delete_udf);
#else
EXPORT_MODULE(module,&_init_udf, &_make_udf,
&_delete_udf);
EXPORT_MODULE(module, &_init_udf, &_make_udf, &_delete_udf);
#endif
15 changes: 9 additions & 6 deletions examples/advanced/modules/external_module.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
#include "modules.hpp"
#include <iostream>

struct impl {
struct impl
{
int a;
};

void _init_udf(impl &pimpl, int a) {
void _init_udf(impl& pimpl, int a)
{
std::cout << "Hello from dynamically loaded object" << std::endl;
std::cout << "internal data: " << pimpl.a << std::endl;
}

void _make_udf(Model &model) {
void _make_udf(Model& model)
{
model.pimpl = new impl;
model.pimpl->a = 11272024;
}

// This macro can be used for trivial desctruction
// This declare de _delete_udf function
// This macro can be used for trivial destruction
// This declare _delete_udf function
DECLARE_DELETER(impl)

// Don't forget to export the module (and deleter).
EXPORT_MODULE(module,&_init_udf, &_make_udf, &_delete_udf);
EXPORT_MODULE_WITH_TAG(module, 1, &_init_udf, &_make_udf, &_delete_udf);
26 changes: 17 additions & 9 deletions examples/advanced/modules/modules.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
#include "modules.hpp"

namespace UnsafeUDF {
void (*make_udf)(Model &) = nullptr; // Define the pointers
void (*init_udf)(impl &, int) = nullptr;
void (*delete_udf)(impl **) = nullptr;
namespace UnsafeUDF
{
void (*make_udf)(Model&) = nullptr; // Define the pointers
void (*init_udf)(impl&, int) = nullptr;
void (*delete_udf)(impl**) = nullptr;
} // namespace UnsafeUDF

Model::Model() { UnsafeUDF::make_udf(*this); }
Model::Model()
{
UnsafeUDF::make_udf(*this);
}

void Model::init(int a) {
if (pimpl != nullptr) {
void Model::init(int a)
{
if (pimpl != nullptr)
{
return UnsafeUDF::init_udf(*pimpl, a);
}
}
Model::~Model() {
if (pimpl != nullptr) {
Model::~Model()
{
if (pimpl != nullptr)
{
UnsafeUDF::delete_udf(&pimpl);
}
}
23 changes: 12 additions & 11 deletions examples/advanced/modules/modules.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@
struct impl;
struct Model;

namespace UnsafeUDF {
extern void (*make_udf)(Model &);
extern void (*init_udf)(impl &, int);
extern void (*delete_udf)(impl **);
} // namespace UsafeUDF

struct Model {
Model() ;
namespace UnsafeUDF
{
extern void (*make_udf)(Model&);
extern void (*init_udf)(impl&, int);
extern void (*delete_udf)(impl**);
} // namespace UnsafeUDF

struct Model
{
Model();
void init(int a);
~Model() ;
impl *pimpl = nullptr;
~Model();
impl* pimpl = nullptr;
};

using init_udf_ptr = decltype(UnsafeUDF::init_udf);
Expand All @@ -27,5 +29,4 @@ DEFINE_MODULE(MODULE_ITEM(init_udf) MODULE_ITEM(make_udf)

EXPORT_DEFAULT


#endif
15 changes: 10 additions & 5 deletions examples/advanced/modules/wrong.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
#include "modules.hpp"
#include <iostream>

struct impl {
struct impl
{
int a;
};

void _init_udf(impl &pimpl, int a) {
void _init_udf(impl& pimpl, int a)
{
std::cout << "A=" << pimpl.a << std::endl;
}

void _make_udf(Model &model) {
void _make_udf(Model& model)
{
model.pimpl = new impl;
model.pimpl->a = 56;
}

void _delete_udf(impl **pimpl) {
if (pimpl != nullptr) {
void _delete_udf(impl** pimpl)
{
if (pimpl != nullptr)
{
delete *pimpl;
*pimpl = nullptr;
}
Expand Down
16 changes: 10 additions & 6 deletions examples/basic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#include <dynlib/dynlib.hpp>
#include <iostream>

int exec(std::shared_ptr<DynamicLibrary> handle) {
int exec(std::shared_ptr<DynamicLibrary> handle)
{
// Try to retrieve the module from the loaded library.
// If the module is not found, use the default module provided by
// default_module.
Expand All @@ -13,11 +14,12 @@ int exec(std::shared_ptr<DynamicLibrary> handle) {
foo_ptr f = _mod._foo_m;

// Print the addresses of the current function and the default function.
std::cout << "Current function address " << (void *)f << std::endl;
std::cout << "Default function address " << (void *)default_module._foo_m
std::cout << "Current function address " << (void*)f << std::endl;
std::cout << "Default function address " << (void*)default_module._foo_m
<< std::endl;

if (f == default_module._foo_m) {
if (f == default_module._foo_m)
{
std::cout << "Using default implementation" << std::endl;
}
// Call the function pointer "f" with arguments 1234 and 1 and print the
Expand All @@ -26,11 +28,13 @@ int exec(std::shared_ptr<DynamicLibrary> handle) {
return (f(1, 1) == 20) ? 0 : -1;
}

int main(int argc, char **argv) {
int main(int argc, char** argv)
{

std::shared_ptr<DynamicLibrary> _handle;

if (argc == 2) {
if (argc == 2)
{
std::cout << "Loading " << argv[1] << std::endl;
_handle = DynamicLibrary::getLib(argv[1]);
}
Expand Down
24 changes: 16 additions & 8 deletions examples/basic/modules/default_lib.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
#include "modules.hpp"
#include "modules.hpp"
#include <dynlib/dyn_module.hpp>

// Declaration and implementation of default_foo function which returns the sum of its two arguments.
int default_foo(int a, int b) { return a + b; }
// Declaration and implementation of default_foo function which returns the sum
// of its two arguments.
int default_foo(int a, int b)
{
return a + b;
}

// Declaration and implementation of default_bar function which returns the product of its argument and 16.
int default_bar(int a) { return a * 16; }
// Declaration and implementation of default_bar function which returns the
// product of its argument and 16.
int default_bar(int a)
{
return a * 16;
}

#ifdef DEFAULT_MODULE
// If DEFAULT_MODULE is defined, create a Module object named default_module
// containing pointers to default_foo and default_bar functions.
EXPORT_MODULE(default_module,&default_foo, &default_bar);
EXPORT_MODULE(default_module, &default_foo, &default_bar);
#else
EXPORT_MODULE(module,&default_foo, &default_bar);
#endif
EXPORT_MODULE(module, &default_foo, &default_bar);
#endif
17 changes: 13 additions & 4 deletions examples/basic/modules/external_module.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
#include "dynlib/dyn_module.hpp"
#include "modules.hpp"
#include <dynlib/dyn_module.hpp>

// It's possible to declare "private" functions.
int baz() { return 10; }
int baz()
{
return 10;
}

// Declaration and implementation of the function to be exported.
int foo(int a, int b) { return baz() * (a + b); }
int foo(int a, int b)
{
return baz() * (a + b);
}

int bar(int a) { return a * 16 * 2; }
int bar(int a)
{
return a * 16 * 2;
}

// Don't forget to export the module.
EXPORT_MODULE(module, ._foo_m = &foo, ._bar_m = &bar);
Expand Down
Loading