From 0c74c8080dcc7a453339aea66e3ed9ba313d5dbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tam=C3=A1s=20B=C3=A1lint=20Misius?= Date: Wed, 20 Nov 2024 20:30:43 +0100 Subject: [PATCH] Make LuaSmartRef properly non-copyable Broken since 06e28367266e, which added LuaSmartRef. That it had absolutely not followed the rule of five at all didn't use to be a problem because it had only ever been in vectors that got resized only once throughout the lifetime of the program >_> --- src/lua/LuaSmartRef.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/lua/LuaSmartRef.h b/src/lua/LuaSmartRef.h index b00c455c0..b56df4e2d 100644 --- a/src/lua/LuaSmartRef.h +++ b/src/lua/LuaSmartRef.h @@ -1,12 +1,27 @@ #pragma once #include "LuaCompat.h" +#include class LuaSmartRef { int ref = LUA_REFNIL; public: + LuaSmartRef() = default; + LuaSmartRef(const LuaSmartRef &other) = delete; ~LuaSmartRef(); + + LuaSmartRef(LuaSmartRef &&other) noexcept : LuaSmartRef() + { + swap(*this, other); + } + + LuaSmartRef &operator =(LuaSmartRef other) + { + swap(*this, other); + return *this; + } + void Assign(lua_State *L, int index); // Copies the value before getting reference, stack unchanged. void Clear(); int Push(lua_State *L); // Always pushes exactly one value, possibly nil. @@ -20,4 +35,10 @@ public: { return ref != LUA_REFNIL; } + + friend void swap(LuaSmartRef &one, LuaSmartRef &other) + { + using std::swap; + swap(one.ref, other.ref); + } };