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
53consteval const char* lua_metamethod_name(std::meta::info f) {
54 using std::meta::operators;
55 const bool unary{::welder::detail::is_unary_operator(f)};
56 switch (std::meta::operator_of(f)) {
57 case operators::op_plus:
58 return unary ? nullptr : "__add"; // no unary +
59 case operators::op_minus:
60 return unary ? "__unm" : "__sub";
61 case operators::op_star:
62 return unary ? nullptr // unary * is dereference, not a Lua metamethod
63 : "__mul";
64 case operators::op_slash: return "__div";
65 case operators::op_percent: return "__mod";
66 case operators::op_equals_equals: return "__eq";
67 case operators::op_less: return "__lt";
68 case operators::op_less_equals: return "__le";
69 // Lua derives ~=, > and >= from __eq/__lt/__le, so these expose nothing.
70 case operators::op_exclamation_equals:
71 case operators::op_greater:
72 case operators::op_greater_equals: return nullptr;
73 case operators::op_parentheses: return "__call";
74 // operator[] -> Lua's __index (a fallback consulted after normal
75 // member/method lookup, so it coexists with fields and methods).
76 case operators::op_square_brackets: return "__index";
77#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 503
78 // Bitwise: Lua 5.3+ only. C++ operator^ is XOR (Lua __bxor), NOT power.
79 case operators::op_caret: return "__bxor";
80 case operators::op_tilde: return unary ? "__bnot" : nullptr;
81 case operators::op_ampersand:
82 return unary ? nullptr // unary & is address-of
83 : "__band";
84 case operators::op_pipe: return "__bor";
85 case operators::op_less_less: return "__shl";
86 case operators::op_greater_greater: return "__shr";
87#endif
88 default: return nullptr;
89 }
90}
91
92} // 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 an operator function is unary vs binary.
consteval const char * lua_metamethod_name(std::meta::info f)
Map an operator (member or anchored free) to its Lua metamethod __name, or nullptr if welder does not...