From 678ee3fe8942ade73a46a1f4ec45e2216471a3f7 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 24 Oct 2024 15:18:11 +0200 Subject: Improve Unique some more --- src/intercopycontext.cpp | 12 ++++++------ src/keeper.cpp | 2 +- src/unique.hpp | 31 +++++++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 7244ed8..06cafbd 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -671,7 +671,7 @@ LuaType InterCopyContext::processConversion() const [[nodiscard]] bool InterCopyContext::tryCopyClonable() const { - SourceIndex const _L1_i{ luaG_absindex(L1, L1_i) }; + SourceIndex const _L1_i{ luaG_absindex(L1, L1_i).value() }; void* const _source{ lua_touserdata(L1, _L1_i) }; STACK_CHECK_START_REL(L1, 0); @@ -740,7 +740,7 @@ LuaType InterCopyContext::processConversion() const // assign uservalues UserValueIndex _uvi{ _nuv.value() }; while (_uvi > 0) { - _c.L1_i = SourceIndex{ luaG_absindex(L1, kIdxTop) }; + _c.L1_i = SourceIndex{ luaG_absindex(L1, kIdxTop).value() }; if (_c.interCopyOne() != InterCopyResult::Success) { // L2: ... u uv raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); } @@ -798,7 +798,7 @@ LuaType InterCopyContext::processConversion() const STACK_GROW(L2, _nuv); UserValueIndex _uvi{ _nuv.value() }; while (_uvi) { - _c.L1_i = SourceIndex{ luaG_absindex(L1, kIdxTop) }; + _c.L1_i = SourceIndex{ luaG_absindex(L1, kIdxTop).value() }; if (_c.interCopyOne() != InterCopyResult::Success) { // L1: ... deep ... [uv]* L2: deep uv raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); } @@ -882,7 +882,7 @@ LuaType InterCopyContext::processConversion() const InterCopyContext _c{ *this }; UserValueIndex _uvi{ _nuv.value() }; while (_uvi > 0) { - _c.L1_i = SourceIndex{ luaG_absindex(L1, kIdxTop) }; + _c.L1_i = SourceIndex{ luaG_absindex(L1, kIdxTop).value() }; if (_c.interCopyOne() != InterCopyResult::Success) { // L2: ... mt u uv raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); } @@ -1301,10 +1301,10 @@ namespace { for (StackIndex _i{ (L1_i != 0) ? L1_i.value() : (_top_L1 - n_ + 1) }, _j{ 1 }; _j <= n_; ++_i, ++_j) { char _tmpBuf[16]; if (U->verboseErrors) { - sprintf(_tmpBuf, "arg_%d", _j.operator int()); + sprintf(_tmpBuf, "arg_%d", _j.value()); _c.name = _tmpBuf; } - _c.L1_i = SourceIndex{ _i }; + _c.L1_i = SourceIndex{ _i.value() }; _copyok = _c.interCopyOne(); // L2: ... cache {}n if (_copyok != InterCopyResult::Success) { break; diff --git a/src/keeper.cpp b/src/keeper.cpp index 4b1ae3d..8ab9681 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -928,7 +928,7 @@ void Keepers::initialize(Universe& U_, lua_State* L_, size_t const nbKeepers_, i // copy package.path and package.cpath from the source state if (luaG_getmodule(L, LUA_LOADLIBNAME) != LuaType::NIL) { // L_: settings package _K: // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately - InterCopyContext _c{ U, DestState{ _K.value() }, SourceState{ L }, {}, SourceIndex{ luaG_absindex(L, kIdxTop) }, {}, LookupMode::ToKeeper, {} }; + InterCopyContext _c{ U, DestState{ _K.value() }, SourceState{ L }, {}, SourceIndex{ luaG_absindex(L, kIdxTop).value() }, {}, LookupMode::ToKeeper, {} }; if (_c.interCopyPackage() != InterCopyResult::Success) { // L_: settings ... error_msg _K: // if something went wrong, the error message is at the top of the stack lua_remove(L, -2); // L_: settings error_msg diff --git a/src/unique.hpp b/src/unique.hpp index 27ea71e..c214dbc 100644 --- a/src/unique.hpp +++ b/src/unique.hpp @@ -26,11 +26,13 @@ class Unique constexpr Unique& operator=(Unique const&) = default; constexpr Unique& operator=(Unique&&) = default; - // Forbid construction with any other class, especially with types that convert naturally to UnderlyingType. - // For instance, this prevents construction with a float when UnderlyingType is an integer type. + // Forbid construction with any other class, especially with types that convert naturally to T. + // For instance, this prevents construction with a float when T is an integer type. // Conversion will have to be explicit and the developer will be aware of it. // However we want to keep the same-type copy constructors (including with an inherited class), hence the enable_if stuff. template >, bool> = true> + Unique(AnyOtherClass const&) = delete; + template >, bool> = true> Unique(AnyOtherClass&&) = delete; // can't implicitly affect from base type @@ -38,8 +40,8 @@ class Unique constexpr Unique& operator=(T&&) = delete; // cast - constexpr operator T() const noexcept { return val; } - constexpr T value() const noexcept { return val; } + constexpr operator T const&() const noexcept { return val; } + constexpr T const& value() const noexcept { return val; } // pre-increment auto& operator++() noexcept @@ -71,12 +73,33 @@ class Unique>> : public T { public: + using self = Unique; using type = T; using T::T; explicit Unique(T const& b_) : T{ b_ } { } + + // rule of 5 + constexpr Unique() = default; + constexpr Unique(Unique const&) = default; + constexpr Unique(Unique&&) = default; + constexpr Unique& operator=(Unique const&) = default; + constexpr Unique& operator=(Unique&&) = default; + + // Forbid construction with any other class, especially with types that convert naturally to T. + // For instance, this prevents construction with a float when T is an integer type. + // Conversion will have to be explicit and the developer will be aware of it. + // However we want to keep the same-type copy constructors (including with an inherited class), hence the enable_if stuff. + template >, bool> = true> + Unique(AnyOtherClass const&) = delete; + template >, bool> = true> + Unique(AnyOtherClass&&) = delete; + + // can't implicitly affect from base type + Unique& operator=(T const&) = delete; + constexpr Unique& operator=(T&&) = delete; }; #define DECLARE_UNIQUE_TYPE(_name, _type) using _name = Unique<_type, class Unique_##_name##_Tag> -- cgit v1.2.3-55-g6feb