summaryrefslogtreecommitdiff
path: root/libadhocutil
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2020-12-06 15:37:45 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2020-12-06 15:37:45 +0000
commita7897ca0a8c5bafaa8352cefcdfc2c4f33559634 (patch)
tree580fc6325ea4856106c64e25acc91faa358a0872 /libadhocutil
parentRemove copy initialisation of locks in ScopeLock (diff)
downloadlibadhocutil-a7897ca0a8c5bafaa8352cefcdfc2c4f33559634.tar.bz2
libadhocutil-a7897ca0a8c5bafaa8352cefcdfc2c4f33559634.tar.xz
libadhocutil-a7897ca0a8c5bafaa8352cefcdfc2c4f33559634.zip
Simplified LazyPointer
Addresses cppcheck error.
Diffstat (limited to 'libadhocutil')
-rw-r--r--libadhocutil/lazyPointer.h26
1 files 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<typename T, typename P = std::shared_ptr<T>> class LazyPointer {
public:
+ static_assert(std::is_same_v<T &, decltype(*P())>);
/// @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<P>::value) {
- return deref();
- }
- else {
- return deref().get();
- }
+ return &*deref();
}
- [[nodiscard]] P
+ [[nodiscard]] const P &
deref() const
{
- if (Factory * f = std::get_if<Factory>(&source)) {
- P p = (*f)();
- source = p;
- return p;
- }
- else {
- return std::get<P>(source);
+ if (const auto * f = std::get_if<Factory>(&source)) {
+ source = (*f)();
}
+ return std::get<P>(source);
}
bool