malena
Why malena
Quick start
Tutorials
API docs
GitHub
GitHub
Tutorials
Loading...
Searching...
No Matches
Core
MLExport.h
Go to the documentation of this file.
1
#pragma once
2
// ============================================================
3
// include/Malena/Core/MLExport.h
4
// ============================================================
5
//
6
// MLExport<T> is an alternative to ML_EXPORT for users who prefer
7
// explicit C++ syntax over macros. Place it in a .cpp file after
8
// the class definition:
9
//
10
// template class ml::MLExport<ScoreFireable>;
11
//
12
// This forces instantiation of MLExport<T>::Registrar, whose
13
// constructor calls Fireable::_register exactly once.
14
//
15
// NOTE: Unlike ML_EXPORT, this cannot be placed in a header —
16
// explicit template instantiation violates ODR if the header is
17
// included in multiple translation units. Use ML_EXPORT for
18
// header-compatible registration.
19
//
20
// This header must be included AFTER Fireable.h so that Fireable
21
// is a complete type. In practice this is guaranteed because
22
// Fireable.h includes this file at its bottom.
23
//
24
// ============================================================
25
26
#include <Malena/Events/Fireable.h>
27
#include <type_traits>
28
29
namespace
ml
30
{
72
template
<
typename
T>
73
struct
MLExport
74
{
75
private
:
80
struct
Registrar
81
{
82
Registrar()
83
{
84
if
constexpr
(std::is_base_of_v<Fireable, T>)
85
{
86
// Calls private Fireable::_register —
87
// accessible via: template<typename U> friend struct MLExport;
88
// declared in Fireable.h
89
Fireable::_register(
new
T());
90
}
91
else
if
constexpr
(!std::is_base_of_v<Plugin, T>)
92
{
93
// Neither Fireable nor Plugin — unsupported type
94
static_assert
(!
sizeof
(T),
95
"[Malena] MLExport: type is not a recognised exportable "
96
"Malena type. Must derive from ml::Fireable. "
97
"For ml::Plugin types use ML_EXPORT instead."
);
98
}
99
// Plugin case: extern "C" symbols are emitted by ML_EXPORT —
100
// MLExport does not handle plugins. Use ML_EXPORT for plugins.
101
}
102
};
104
105
// Constructed once when MLExport<T> is explicitly instantiated.
106
// inline static ensures a single instance across translation units.
107
inline
static
Registrar _registrar{};
108
109
// Fireable::_register is private — MLExport accesses it via:
110
// template<typename U> friend struct MLExport; (declared in Fireable.h)
111
};
112
113
}
// namespace ml
ml
Definition
Component.h:18
ml::MLExport
Template alternative to ML_EXPORT for registering Malena types in .cpp files.
Definition
MLExport.h:74
Generated by
1.16.1