CppInterOp
C++ Language Interoperability Layer
Loading...
Searching...
No Matches
CppInterOpDispatch.cpp
Go to the documentation of this file.
1// Implementation of the dispatch symbol table. Maps function names to
2// Cpp:: addresses for dlopen consumers via CppGetProcAddress.
3// This is a library internal — it includes CppInterOp.h (not Dispatch.h)
4// because it needs the Cpp:: function declarations.
5
7
8#include <iostream>
9#include <string_view>
10#include <unordered_map>
11
12using namespace Cpp;
13using CppFnPtrTy = void (*)();
14
15// NOLINTBEGIN(cppcoreguidelines-pro-type-cstyle-cast)
16// Suppress deprecation: the dispatch table intentionally exposes all
17// overloads, including those marked [[deprecated]] in the public API.
18#if defined(__GNUC__) || defined(__clang__)
19#pragma GCC diagnostic push
20#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
21#elif defined(_MSC_VER)
22#pragma warning(push)
23#pragma warning(disable : 4996)
24#endif
25static const std::unordered_map<std::string_view, CppFnPtrTy> DispatchMap = {
26#define CPPINTEROP_API_FUNC(DN, CN, Ret, DeclArgs, CallArgs, RawTypes) \
27 {#DN, (CppFnPtrTy) static_cast<Ret(*) RawTypes>(&Cpp::CN)},
28#include "CppInterOp/CppInterOpAPI.inc"
29};
30#if defined(__GNUC__) || defined(__clang__)
31#pragma GCC diagnostic pop
32#elif defined(_MSC_VER)
33#pragma warning(pop)
34#endif
35// NOLINTEND(cppcoreguidelines-pro-type-cstyle-cast)
36
37extern "C" CPPINTEROP_API CppFnPtrTy CppGetProcAddress(const char* funcName) {
38 auto it = DispatchMap.find(funcName);
39 if (it == DispatchMap.end()) {
40 std::cerr << "[CppInterOp Dispatch] Failed to find API: " << funcName
41 << " May need to be ported to CppInterOp.td\n";
42 return nullptr;
43 }
44 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
45 return reinterpret_cast<CppFnPtrTy>(it->second);
46}
CppFnPtrTy CppGetProcAddress(const char *funcName)
static const std::unordered_map< std::string_view, CppFnPtrTy > DispatchMap
#define CPPINTEROP_API
void(*)() CppFnPtrTy
Definition Dispatch.h:44
Definition Box.h:70