welder 0.1.0
Bindings for annotated C++ types, from C++26 reflection
Loading...
Searching...
No Matches
metamethods.hpp
Go to the documentation of this file.
1#pragma once
2#include <meta>
3
4#include <welder/bind_traits.hpp> // is_unary_operator
5
35
36namespace welder::inline v0::rods::lua {
37
49consteval const char* lua_metamethod_name(std::meta::info f) {
50 using std::meta::operators;
51 const bool unary{::welder::detail::is_unary_operator(f)};
52 switch (std::meta::operator_of(f)) {
53 case operators::op_plus:
54 return unary ? nullptr : "__add"; // no unary +
55 case operators::op_minus:
56 return unary ? "__unm" : "__sub";
57 case operators::op_star:
58 return unary ? nullptr // unary * is dereference, not a Lua metamethod
59 : "__mul";
60 case operators::op_slash: return "__div";
61 case operators::op_percent: return "__mod";
62 case operators::op_equals_equals: return "__eq";
63 case operators::op_less: return "__lt";
64 case operators::op_less_equals: return "__le";
65 // Lua derives ~=, > and >= from __eq/__lt/__le, so these expose nothing.
66 case operators::op_exclamation_equals:
67 case operators::op_greater:
68 case operators::op_greater_equals: return nullptr;
69 case operators::op_parentheses: return "__call";
70 // operator[] -> Lua's __index (a fallback consulted after normal
71 // member/method lookup, so it coexists with fields and methods).
72 case operators::op_square_brackets: return "__index";
73#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 503
74 // Bitwise: Lua 5.3+ only. C++ operator^ is XOR (Lua __bxor), NOT power.
75 case operators::op_caret: return "__bxor";
76 case operators::op_tilde: return unary ? "__bnot" : nullptr;
77 case operators::op_ampersand:
78 return unary ? nullptr // unary & is address-of
79 : "__band";
80 case operators::op_pipe: return "__bor";
81 case operators::op_less_less: return "__shl";
82 case operators::op_greater_greater: return "__shr";
83#endif
84 default: return nullptr;
85 }
86}
87
88} // namespace welder::rods::lua
Backend-agnostic selection layer: the reflection predicates and selectors that decide what participat...
consteval bool is_unary_operator(std::meta::info f)
Whether a member operator is unary (0 parameters) vs binary (1 parameter).
consteval const char * lua_metamethod_name(std::meta::info f)
Map a member operator to its Lua metamethod __name, or nullptr if welder does not expose it (which al...