Skip to content

Commit

Permalink
Add macros with plugin interface strings for easier updates.
Browse files Browse the repository at this point in the history
This means I (and people making their own plugins) don't need to go and
update each and every plugin once the version in the interface string
gets bumped after a (silent) ABI break. Such as when new virtual
functions get added, as those often lead to strange crashes if the
plugins don't get rebuilt after.

The plugins will now use this macro, which means they'll
automatically embed an interface string that was present in the base
class header at build time. However, when the base class updates, the
previous string is still embedded in the plugin binary, which will then
fail to load -- this being automatic doesn't mean the original purpose
is lost. Subsequently rebuilding the plugins from source will make them
pick up the updated interface string again.
  • Loading branch information
mosra committed Apr 9, 2023
1 parent 79c42fe commit 12fc59b
Show file tree
Hide file tree
Showing 19 changed files with 308 additions and 42 deletions.
9 changes: 9 additions & 0 deletions doc/changelog.dox
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,15 @@ See also:
@relativeref{Timeline,previousFrameTime()} and
@relativeref{Timeline,previousFrameDuration()} (see
[mosra/magnum#604](https://github.com/mosra/magnum/pull/604))
- Added @ref MAGNUM_AUDIO_ABSTRACTIMPORTER_PLUGIN_INTERFACE,
@ref MAGNUM_SHADERTOOLS_ABSTRACTCONVERTER_PLUGIN_INTERFACE,
@ref MAGNUM_TEXT_ABSTRACTFONT_PLUGIN_INTERFACE,
@ref MAGNUM_TEXT_ABSTRACTFONTCONVERTER_PLUGIN_INTERFACE,
@ref MAGNUM_TRADE_ABSTRACTIMPORTER_PLUGIN_INTERFACE,
@ref MAGNUM_TRADE_ABSTRACTIMAGECONVERTER_PLUGIN_INTERFACE and
@ref MAGNUM_TRADE_ABSTRACTSCENECONVERTER_PLUGIN_INTERFACE defines with
plugin interface strings to avoid having to update them by hand in every
plugin every time the interface version is bumped after an ABI break

@subsubsection changelog-latest-changes-animation Animation library

Expand Down
27 changes: 27 additions & 0 deletions doc/snippets/MagnumAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,38 @@
DEALINGS IN THE SOFTWARE.
*/

/* In order to have the CORRADE_PLUGIN_REGISTER() macro not a no-op. Doesn't
affect anything else. */
#define CORRADE_STATIC_PLUGIN

#include <Corrade/Containers/Array.h>

#include "Magnum/Audio/AbstractImporter.h"
#include "Magnum/Audio/Context.h"
#include "Magnum/Audio/Extensions.h"

using namespace Magnum;

namespace MyNamespace {

struct MyAudioImporter: Audio::AbstractImporter {
explicit MyAudioImporter(PluginManager::AbstractManager& manager, const Containers::StringView& plugin): Audio::AbstractImporter{manager, plugin} {}

Audio::ImporterFeatures doFeatures() const override { return {}; }
bool doIsOpened() const override { return false; }
void doClose() override {}
Audio::BufferFormat doFormat() const override { return {}; }
UnsignedInt doFrequency() const override { return {}; }
Containers::Array<char> doData() override { return {}; }
};

}

/* [MAGNUM_AUDIO_ABSTRACTIMPORTER_PLUGIN_INTERFACE] */
CORRADE_PLUGIN_REGISTER(MyAudioImporter, MyNamespace::MyAudioImporter,
MAGNUM_AUDIO_ABSTRACTIMPORTER_PLUGIN_INTERFACE)
/* [MAGNUM_AUDIO_ABSTRACTIMPORTER_PLUGIN_INTERFACE] */

int main() {

{
Expand Down
21 changes: 21 additions & 0 deletions doc/snippets/MagnumShaderTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
DEALINGS IN THE SOFTWARE.
*/

/* In order to have the CORRADE_PLUGIN_REGISTER() macro not a no-op. Doesn't
affect anything else. */
#define CORRADE_STATIC_PLUGIN

#include <string> /** @todo drop when file callbacks are <string>-free */
#include <unordered_map>
#include <Corrade/Containers/Array.h>
Expand All @@ -41,6 +45,23 @@

using namespace Magnum;

namespace MyNamespace {

struct MyShaderConverter: ShaderTools::AbstractConverter {
explicit MyShaderConverter(PluginManager::AbstractManager& manager, Containers::StringView plugin): ShaderTools::AbstractConverter{manager, plugin} {}

ShaderTools::ConverterFeatures doFeatures() const override { return {}; }
void doSetInputFormat(ShaderTools::Format, Containers::StringView) override {}
void doSetOutputFormat(ShaderTools::Format, Containers::StringView) override {}
};

}

/* [MAGNUM_SHADERTOOLS_ABSTRACTCONVERTER_PLUGIN_INTERFACE] */
CORRADE_PLUGIN_REGISTER(MyShaderConverter, MyNamespace::MyShaderConverter,
MAGNUM_SHADERTOOLS_ABSTRACTCONVERTER_PLUGIN_INTERFACE)
/* [MAGNUM_SHADERTOOLS_ABSTRACTCONVERTER_PLUGIN_INTERFACE] */

int main() {
{
/* [AbstractConverter-usage-validation] */
Expand Down
35 changes: 35 additions & 0 deletions doc/snippets/MagnumText.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
DEALINGS IN THE SOFTWARE.
*/

/* In order to have the CORRADE_PLUGIN_REGISTER() macro not a no-op. Doesn't
affect anything else. */
#define CORRADE_STATIC_PLUGIN

#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/StringView.h>
Expand All @@ -36,6 +40,7 @@
#include "Magnum/Math/Matrix3.h"
#include "Magnum/Shaders/VectorGL.h"
#include "Magnum/Text/AbstractFont.h"
#include "Magnum/Text/AbstractFontConverter.h"
#include "Magnum/Text/DistanceFieldGlyphCache.h"
#include "Magnum/Text/Renderer.h"

Expand All @@ -44,6 +49,36 @@
using namespace Magnum;
using namespace Magnum::Math::Literals;

namespace MyNamespace {

struct MyFont: Text::AbstractFont {
explicit MyFont(PluginManager::AbstractManager& manager, Containers::StringView plugin): Text::AbstractFont{manager, plugin} {}

Text::FontFeatures doFeatures() const override { return {}; }
bool doIsOpened() const override { return false; }
void doClose() override {}
UnsignedInt doGlyphId(char32_t) override { return {}; }
Vector2 doGlyphAdvance(UnsignedInt) override { return {}; }
Containers::Pointer<Text::AbstractLayouter> doLayout(const Text::AbstractGlyphCache&, Float, const std::string&) override { return {}; }
};
struct MyFontConverter: Text::AbstractFontConverter {
explicit MyFontConverter(PluginManager::AbstractManager& manager, Containers::StringView plugin): Text::AbstractFontConverter{manager, plugin} {}

Text::FontConverterFeatures doFeatures() const override { return {}; }
};

}

/* [MAGNUM_TEXT_ABSTRACTFONT_PLUGIN_INTERFACE] */
CORRADE_PLUGIN_REGISTER(MyFont, MyNamespace::MyFont,
MAGNUM_TEXT_ABSTRACTFONT_PLUGIN_INTERFACE)
/* [MAGNUM_TEXT_ABSTRACTFONT_PLUGIN_INTERFACE] */

/* [MAGNUM_TEXT_ABSTRACTFONTCONVERTER_PLUGIN_INTERFACE] */
CORRADE_PLUGIN_REGISTER(MyFontConverter, MyNamespace::MyFontConverter,
MAGNUM_TEXT_ABSTRACTFONTCONVERTER_PLUGIN_INTERFACE)
/* [MAGNUM_TEXT_ABSTRACTFONTCONVERTER_PLUGIN_INTERFACE] */

int main() {

{
Expand Down
41 changes: 41 additions & 0 deletions doc/snippets/MagnumTrade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
DEALINGS IN THE SOFTWARE.
*/

/* In order to have the CORRADE_PLUGIN_REGISTER() macro not a no-op. Doesn't
affect anything else. */
#define CORRADE_STATIC_PLUGIN

#include <unordered_map>
#include <Corrade/Containers/ArrayTuple.h>
#include <Corrade/Containers/Optional.h>
Expand Down Expand Up @@ -89,6 +93,43 @@
using namespace Magnum;
using namespace Magnum::Math::Literals;

namespace MyNamespace {

struct MyImporter: Trade::AbstractImporter {
explicit MyImporter(PluginManager::AbstractManager& manager, Containers::StringView plugin): Trade::AbstractImporter{manager, plugin} {}

Trade::ImporterFeatures doFeatures() const override { return {}; }
bool doIsOpened() const override { return false; }
void doClose() override {}
};
struct MyImageConverter: Trade::AbstractImageConverter {
explicit MyImageConverter(PluginManager::AbstractManager& manager, Containers::StringView plugin): Trade::AbstractImageConverter{manager, plugin} {}

Trade::ImageConverterFeatures doFeatures() const override { return {}; }
};
struct MySceneConverter: Trade::AbstractSceneConverter {
explicit MySceneConverter(PluginManager::AbstractManager& manager, Containers::StringView plugin): Trade::AbstractSceneConverter{manager, plugin} {}

Trade::SceneConverterFeatures doFeatures() const override { return {}; }
};

}

/* [MAGNUM_TRADE_ABSTRACTIMPORTER_PLUGIN_INTERFACE] */
CORRADE_PLUGIN_REGISTER(MyImporter, MyNamespace::MyImporter,
MAGNUM_TRADE_ABSTRACTIMPORTER_PLUGIN_INTERFACE)
/* [MAGNUM_TRADE_ABSTRACTIMPORTER_PLUGIN_INTERFACE] */

/* [MAGNUM_TRADE_ABSTRACTIMAGECONVERTER_PLUGIN_INTERFACE] */
CORRADE_PLUGIN_REGISTER(MyImageConverter, MyNamespace::MyImageConverter,
MAGNUM_TRADE_ABSTRACTIMAGECONVERTER_PLUGIN_INTERFACE)
/* [MAGNUM_TRADE_ABSTRACTIMAGECONVERTER_PLUGIN_INTERFACE] */

/* [MAGNUM_TRADE_ABSTRACTSCENECONVERTER_PLUGIN_INTERFACE] */
CORRADE_PLUGIN_REGISTER(MySceneConverter, MyNamespace::MySceneConverter,
MAGNUM_TRADE_ABSTRACTSCENECONVERTER_PLUGIN_INTERFACE)
/* [MAGNUM_TRADE_ABSTRACTSCENECONVERTER_PLUGIN_INTERFACE] */

int main() {

{
Expand Down
6 changes: 1 addition & 5 deletions src/Magnum/Audio/AbstractImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ namespace Magnum { namespace Audio {
using namespace Containers::Literals;

Containers::StringView AbstractImporter::pluginInterface() {
return
/* [interface] */
"cz.mosra.magnum.Audio.AbstractImporter/0.1"_s
/* [interface] */
;
return MAGNUM_AUDIO_ABSTRACTIMPORTER_PLUGIN_INTERFACE ""_s;
}

#ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT
Expand Down
25 changes: 24 additions & 1 deletion src/Magnum/Audio/AbstractImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ class MAGNUM_AUDIO_EXPORT AbstractImporter: public PluginManager::AbstractManagi
/**
* @brief Plugin interface
*
* @snippet Magnum/Audio/AbstractImporter.cpp interface
* @snippet Magnum/Audio/AbstractImporter.h interface
*
* @see @ref MAGNUM_AUDIO_ABSTRACTIMPORTER_PLUGIN_INTERFACE
*/
static Containers::StringView pluginInterface();

Expand Down Expand Up @@ -236,6 +238,27 @@ class MAGNUM_AUDIO_EXPORT AbstractImporter: public PluginManager::AbstractManagi
virtual Containers::Array<char> doData() = 0;
};

/**
@brief Audio importer plugin interface
@m_since_latest
Same string as returned by
@relativeref{Magnum::Audio,AbstractImporter::pluginInterface()}, meant to be
used inside @ref CORRADE_PLUGIN_REGISTER() to avoid having to update the
interface string by hand every time the version gets bumped:
@snippet MagnumAudio.cpp MAGNUM_AUDIO_ABSTRACTIMPORTER_PLUGIN_INTERFACE
The interface string version gets increased on every ABI break to prevent
silent crashes and memory corruption. Plugins built against the previous
version will then fail to load, a subsequent rebuild will make them pick up the
updated interface string.
*/
/* Silly indentation to make the string appear in pluginInterface() docs */
#define MAGNUM_AUDIO_ABSTRACTIMPORTER_PLUGIN_INTERFACE /* [interface] */ \
"cz.mosra.magnum.Audio.AbstractImporter/0.1"
/* [interface] */

}}

#endif
6 changes: 1 addition & 5 deletions src/Magnum/ShaderTools/AbstractConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ namespace Magnum { namespace ShaderTools {
using namespace Containers::Literals;

Containers::StringView AbstractConverter::pluginInterface() {
return
/* [interface] */
"cz.mosra.magnum.ShaderTools.AbstractConverter/0.1.1"_s
/* [interface] */
;
return MAGNUM_SHADERTOOLS_ABSTRACTCONVERTER_PLUGIN_INTERFACE ""_s;
}

#ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT
Expand Down
25 changes: 24 additions & 1 deletion src/Magnum/ShaderTools/AbstractConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ class MAGNUM_SHADERTOOLS_EXPORT AbstractConverter: public PluginManager::Abstrac
/**
* @brief Plugin interface
*
* @snippet Magnum/ShaderTools/AbstractConverter.cpp interface
* @snippet Magnum/ShaderTools/AbstractConverter.h interface
*
* @see @ref MAGNUM_SHADERTOOLS_ABSTRACTCONVERTER_PLUGIN_INTERFACE
*/
static Containers::StringView pluginInterface();

Expand Down Expand Up @@ -1186,6 +1188,27 @@ class MAGNUM_SHADERTOOLS_EXPORT AbstractConverter: public PluginManager::Abstrac
} _inputFileCallbackTemplate{nullptr, nullptr};
};

/**
@brief Shader converter plugin interface
@m_since_latest
Same string as returned by
@relativeref{Magnum::ShaderTools,AbstractConverter::pluginInterface()}, meant
to be used inside @ref CORRADE_PLUGIN_REGISTER() to avoid having to update the
interface string by hand every time the version gets bumped:
@snippet MagnumShaderTools.cpp MAGNUM_SHADERTOOLS_ABSTRACTCONVERTER_PLUGIN_INTERFACE
The interface string version gets increased on every ABI break to prevent
silent crashes and memory corruption. Plugins built against the previous
version will then fail to load, a subsequent rebuild will make them pick up the
updated interface string.
*/
/* Silly indentation to make the string appear in pluginInterface() docs */
#define MAGNUM_SHADERTOOLS_ABSTRACTCONVERTER_PLUGIN_INTERFACE /* [interface] */ \
"cz.mosra.magnum.ShaderTools.AbstractConverter/0.1.1"
/* [interface] */

#ifndef DOXYGEN_GENERATING_OUTPUT
template<class Callback, class T> void AbstractConverter::setInputFileCallback(Callback callback, T& userData) {
/* Don't try to wrap a null function pointer. Need to cast first because
Expand Down
6 changes: 1 addition & 5 deletions src/Magnum/Text/AbstractFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ namespace Magnum { namespace Text {
using namespace Containers::Literals;

Containers::StringView AbstractFont::pluginInterface() {
return
/* [interface] */
"cz.mosra.magnum.Text.AbstractFont/0.3"_s
/* [interface] */
;
return MAGNUM_TEXT_ABSTRACTFONT_PLUGIN_INTERFACE ""_s;
}

#ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT
Expand Down
25 changes: 24 additions & 1 deletion src/Magnum/Text/AbstractFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,9 @@ class MAGNUM_TEXT_EXPORT AbstractFont: public PluginManager::AbstractPlugin {
/**
* @brief Plugin interface
*
* @snippet Magnum/Text/AbstractFont.cpp interface
* @snippet Magnum/Text/AbstractFont.h interface
*
* @see @ref MAGNUM_TEXT_ABSTRACTFONT_PLUGIN_INTERFACE
*/
static Containers::StringView pluginInterface();

Expand Down Expand Up @@ -634,6 +636,27 @@ class MAGNUM_TEXT_EXPORT AbstractLayouter {
UnsignedInt _glyphCount;
};

/**
@brief Font plugin interface
@m_since_latest
Same string as returned by
@relativeref{Magnum::Text,AbstractFont::pluginInterface()}, meant to be used
inside @ref CORRADE_PLUGIN_REGISTER() to avoid having to update the interface
string by hand every time the version gets bumped:
@snippet MagnumText.cpp MAGNUM_TEXT_ABSTRACTFONT_PLUGIN_INTERFACE
The interface string version gets increased on every ABI break to prevent
silent crashes and memory corruption. Plugins built against the previous
version will then fail to load, a subsequent rebuild will make them pick up the
updated interface string.
*/
/* Silly indentation to make the string appear in pluginInterface() docs */
#define MAGNUM_TEXT_ABSTRACTFONT_PLUGIN_INTERFACE /* [interface] */ \
"cz.mosra.magnum.Text.AbstractFont/0.3"
/* [interface] */

#ifndef DOXYGEN_GENERATING_OUTPUT
template<class Callback, class T> void AbstractFont::setFileCallback(Callback callback, T& userData) {
/* Don't try to wrap a null function pointer. Need to cast first because
Expand Down
6 changes: 1 addition & 5 deletions src/Magnum/Text/AbstractFontConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ std::u32string uniqueUnicode(const std::string& characters)
}

Containers::StringView AbstractFontConverter::pluginInterface() {
return
/* [interface] */
"cz.mosra.magnum.Text.AbstractFontConverter/0.2"_s
/* [interface] */
;
return MAGNUM_TEXT_ABSTRACTFONTCONVERTER_PLUGIN_INTERFACE ""_s;
}

#ifndef CORRADE_PLUGINMANAGER_NO_DYNAMIC_PLUGIN_SUPPORT
Expand Down
Loading

0 comments on commit 12fc59b

Please sign in to comment.