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