Switchboard Extension SDK
Loading...
Searching...
No Matches
ExtensionRegistration.hpp
1//
2// ExtensionRegistration.hpp
3// SwitchboardSDK
4//
5
6#pragma once
7
8// This header is intentionally free of any other switchboard_core includes beyond what the
9// macro's expansion needs to compile; extensions already include ExtensionManager.hpp (or a
10// header that does) wherever they define their `load()` method.
11
12#if defined(_WIN32)
13#define SWITCHBOARD_EXTENSION_ANCHOR_EXPORT __declspec(dllexport)
14#else
15#define SWITCHBOARD_EXTENSION_ANCHOR_EXPORT
16#endif
17
18#define SWITCHBOARD_EXTENSION_CONCAT_IMPL(a, b) a##b
19#define SWITCHBOARD_EXTENSION_CONCAT(a, b) SWITCHBOARD_EXTENSION_CONCAT_IMPL(a, b)
20
21// Defined by switchboard_add_extension() as a compile definition equal to the extension's own
22// CMake target name (e.g. SwitchboardSileroVAD). Falls back to a placeholder so this header
23// still compiles (with a non-unique anchor symbol) if included outside that build path.
24#ifndef SWITCHBOARD_EXTENSION_TARGET_NAME
25#define SWITCHBOARD_EXTENSION_TARGET_NAME UnknownSwitchboardExtension
26#endif
27
54#define SWITCHBOARD_REGISTER_EXTENSION(ClassName) \
55 namespace { \
56 struct SWITCHBOARD_EXTENSION_CONCAT(ClassName, AutoRegistrar) { \
57 SWITCHBOARD_EXTENSION_CONCAT(ClassName, AutoRegistrar)() { \
58 ClassName::load(); \
59 } \
60 }; \
61 static const SWITCHBOARD_EXTENSION_CONCAT(ClassName, AutoRegistrar) SWITCHBOARD_EXTENSION_CONCAT( \
62 ClassName, \
63 AutoRegistrarInstance \
64 ); \
65 } \
66 SWITCHBOARD_REGISTER_EXTENSION_PLATFORM_HOOKS()
67
68#if !defined(SWITCHBOARD_WEB)
69// `sb_extension_load` is kept exported (now an intentional no-op — the registrar above already
70// ran registration) purely so ExtensionLoader::loadExtensionLibrary's dlopen/dlsym contract, and
71// its "is this a valid Switchboard extension" check, keep working unmodified for extensions that
72// are dlopen'd at runtime instead of linked at build time.
73//
74// `sb_extension_anchor_<TargetName>` exists only to be referenced by switchboard_link_extensions()
75// via MSVC's `/INCLUDE` linker switch, forcing this extension's DLL into the consumer's import
76// table on Windows even though nothing in the consumer's C++ source calls anything from it.
77#define SWITCHBOARD_REGISTER_EXTENSION_PLATFORM_HOOKS() \
78 extern "C" void sb_extension_load() {} \
79 extern "C" SWITCHBOARD_EXTENSION_ANCHOR_EXPORT void \
80 SWITCHBOARD_EXTENSION_CONCAT(sb_extension_anchor_, SWITCHBOARD_EXTENSION_TARGET_NAME)() {}
81#else
82#define SWITCHBOARD_REGISTER_EXTENSION_PLATFORM_HOOKS()
83#endif