[#MRDOCS_PLUGIN_MAIN] = MRDOCS_PLUGIN_MAIN :mrdocs: Define the entry point of a plugin. == Synopsis Declared in `<mrdocs/Plugin.hpp>` [source,cpp,subs="verbatim,replacements,macros,-callouts"] ---- #define MRDOCS_PLUGIN_MAIN(plugin) ---- == Description Use it once per plugin library, in exactly one translation unit, at namespace scope. It is the only thing a plugin has to define. A plugin may instead define the three `extern "C"` functions itself, in C++, with the signatures xref:mrdocs/PluginBuildIdFn.adoc[`mrdocs::PluginBuildIdFn`], xref:mrdocs/PluginStdlibTagFn.adoc[`mrdocs::PluginStdlibTagFn`] and xref:mrdocs/PluginMainFn.adoc[`mrdocs::PluginMainFn`], each exported with xref:MRDOCS_PLUGIN_EXPORT.adoc[`MRDOCS_PLUGIN_EXPORT`]. The macro defines `mrdocs_plugin_build_id` and `mrdocs_plugin_stdlib_tag`, which return xref:MRDOCS_PLUGIN_BUILD_ID.adoc[`MRDOCS_PLUGIN_BUILD_ID`] and xref:MRDOCS_PLUGIN_STDLIB_TAG.adoc[`MRDOCS_PLUGIN_STDLIB_TAG`] as the plugin's compiler saw them, and `mrdocs_plugin_main`, which calls the body that follows the macro. The body has the signature `static mrdocs::Expected<void> installMrdocsPlugin(mrdocs::PluginContext& plugin)`, with `plugin` named by the macro argument. The parameter is a xref:mrdocs/PluginContext.adoc[`mrdocs::PluginContext`]. Through it the body reads `config()` and calls `registerGenerator` and `registerTransform`. The reference is valid during the call only. The body returns `{}` on success. To refuse, it returns `mrdocs::Unexpected(mrdocs::formatError(...))`, which stops the run with `the plugin "<name>" failed: <message>`. `MRDOCS_TRY` works in the body, because the error of a failed registration is returned the same way. The entry point runs once per xref:mrdocs/ExtensionRegistry/LibraryRegistry.adoc[`mrdocs::ExtensionRegistry::LibraryRegistry`], before the corpus is built. The tool has one per process, and a program that runs more than once shares one among its sessions. Work that depends on the run belongs in xref:mrdocs/Transform/apply.adoc[`mrdocs::Transform::apply`] or xref:mrdocs/Generator/build.adoc[`mrdocs::Generator::build`]. The body must not keep the context reference or a pointer to the configuration. It must not throw either: the macro does not catch exceptions, and the `Expected` the body returns is the only channel, so turn failures into an xref:mrdocs/Error.adoc[`mrdocs::Error`]. Register a generator: [,cpp] ---- MRDOCS_PLUGIN_MAIN(plugin) { return plugin.registerGenerator(std::make_unique()); } ---- Register a transform and a generator. A registration that fails, such as an id that is already taken, refuses the plugin with its message: [,cpp] ---- MRDOCS_PLUGIN_MAIN(plugin) { mrdocs::Expected added = plugin.registerTransform( std::make_unique()); if (!added) { return added; } added = plugin.registerGenerator(std::make_unique()); if (!added) { return added; } return {}; } ---- == Parameters [cols="1,4"] |=== | Name| Description | *plugin* | The name the body uses for the context it receives. |=== [.small]#Created with https://www.mrdocs.com[MrDocs]#