From a7897ca0a8c5bafaa8352cefcdfc2c4f33559634 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 6 Dec 2020 15:37:45 +0000 Subject: Simplified LazyPointer Addresses cppcheck error. --- libadhocutil/lazyPointer.h | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/libadhocutil/lazyPointer.h b/libadhocutil/lazyPointer.h index baf5010..70fe61d 100644 --- a/libadhocutil/lazyPointer.h +++ b/libadhocutil/lazyPointer.h @@ -17,6 +17,7 @@ namespace AdHoc { */ template> class LazyPointer { public: + static_assert(std::is_same_v); /// @cond using element_type = T; using pointer_type = P; @@ -26,14 +27,14 @@ namespace AdHoc { /** Construct pointer with a factory function. */ // cppcheck-suppress noExplicitConstructor; NOLINTNEXTLINE(hicpp-explicit-conversions) - LazyPointer(Factory f) : source(f) { } + LazyPointer(Factory f) : source(std::move(f)) { } /** Construct pointer with an instance value. */ // cppcheck-suppress noExplicitConstructor; NOLINTNEXTLINE(hicpp-explicit-conversions) - LazyPointer(P p) : source(p) { } + LazyPointer(P p) : source(std::move(p)) { } /** Construct pointer with no factory or value. */ - LazyPointer() : source(P(NULL)) { } + LazyPointer() : source(P(nullptr)) { } // Getters /// @cond @@ -57,25 +58,16 @@ namespace AdHoc { [[nodiscard]] T * get() const { - if constexpr (std::is_pointer

::value) { - return deref(); - } - else { - return deref().get(); - } + return &*deref(); } - [[nodiscard]] P + [[nodiscard]] const P & deref() const { - if (Factory * f = std::get_if(&source)) { - P p = (*f)(); - source = p; - return p; - } - else { - return std::get

(source); + if (const auto * f = std::get_if(&source)) { + source = (*f)(); } + return std::get

(source); } bool -- cgit v1.2.3