Skip to content

Instantly share code, notes, and snippets.

@usagi
Last active January 3, 2017 17:39
Show Gist options
  • Save usagi/704a204484f4afcfa38c9db513f62012 to your computer and use it in GitHub Desktop.
Save usagi/704a204484f4afcfa38c9db513f62012 to your computer and use it in GitHub Desktop.
プラグインシステムを Boost.DLL で簡単に実装する紹介 ref: http://qiita.com/usagi/items/0722550dc5433bd07139
#pragma once
#include <string>
namespace usagi::example::boost_dll
{
class plugin_type
{
public:
virtual auto get_name() const -> std::string;
virtual auto operator()( const int a, const int b ) const -> int;
virtual ~plugin_type() { }
};
}
#include "plugin_type.hxx"
#include <boost/config.hpp>
namespace usagi::example::boost_dll
{
class plugin_a : public plugin_type
{
public:
auto get_name() const -> std::string override { return "adder"; }
auto operator()( const int a, const int b ) const -> int override { return a + b; };
};
extern "C" BOOST_SYMBOL_EXPORT plugin_a plugin;
plugin_a plugin;
}
#include "plugin_type.hxx"
#include <boost/dll/import.hpp>
#include <iostream>
auto main() -> int
{
using namespace std;
using namespace usagi::example::boost_dll;
// ↓ op は boost::shared_ptr< plugin_type > 型で与えられる。
// (std::shared_ptr へ変換したい場合は参考†を参照。)
auto op =
boost::dll::import< plugin_type >
// ↓ロードするプラグインのパス(実用の際は一般に決め打ちにしない事が多い)
( boost::filesystem::path( "plugin_a" )
// ↓インポートする対象のプラグインでエクスポートされているシンボル
, "plugin"
// ↓これを付けておくと、 .so や .dll をパスに勝手に追加してくれて便利
, boost::dll::load_mode::append_decorations
);
cout << op->get_name() << " " << (*op)( 1, 2 ) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment