From 05a41bc929985d391c2fd8a701d7c8437f71e0c9 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 15 Sep 2017 13:12:53 +0100 Subject: Add compatibility with raw pointers to LazyPointer --- libadhocutil/lazyPointer.h | 8 ++++--- libadhocutil/unittests/testLazyPointer.cpp | 36 ++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/libadhocutil/lazyPointer.h b/libadhocutil/lazyPointer.h index 9b680c4..bcc5217 100644 --- a/libadhocutil/lazyPointer.h +++ b/libadhocutil/lazyPointer.h @@ -4,6 +4,8 @@ #include #include #include +#include +#include namespace AdHoc { @@ -37,7 +39,8 @@ class LazyPointer { } /** Construct pointer with an instance value. */ - LazyPointer(T * p) : + template + LazyPointer(T * p, typename std::enable_if::value>::type * = NULL) : source(P(p)) { } @@ -67,7 +70,7 @@ class LazyPointer { T * get() const { - return deref().get(); + return boost::get_pointer(deref()); } P deref() const @@ -133,7 +136,6 @@ class LazyPointer { }; } - namespace boost { template R * dynamic_pointer_cast(const AdHoc::LazyPointer & p) { diff --git a/libadhocutil/unittests/testLazyPointer.cpp b/libadhocutil/unittests/testLazyPointer.cpp index b376cc5..4ecdd0f 100644 --- a/libadhocutil/unittests/testLazyPointer.cpp +++ b/libadhocutil/unittests/testLazyPointer.cpp @@ -17,6 +17,7 @@ class Test : public IntrusivePtrBase { }; typedef LazyPointer TestLazyPointer; +typedef LazyPointer RawLazyPointer; static TestLazyPointer::pointer_type @@ -79,3 +80,38 @@ BOOST_AUTO_TEST_CASE ( nondefault ) BOOST_REQUIRE_EQUAL(true, p.hasValue()); } +BOOST_AUTO_TEST_CASE( rawPointerNull ) +{ + RawLazyPointer null; + BOOST_REQUIRE(null.hasValue()); + BOOST_REQUIRE(!null); + BOOST_REQUIRE(!null.get()); +} + +BOOST_AUTO_TEST_CASE( rawPointerNonNull ) +{ + RawLazyPointer value(new int(3)); + BOOST_REQUIRE(value.hasValue()); + BOOST_REQUIRE(value); + BOOST_REQUIRE(value.get()); + BOOST_REQUIRE_EQUAL(*value, 3); + int * x = value; + BOOST_REQUIRE_EQUAL(*x, 3); + delete value; +} + +int * +rawFactory(const std::string & s) +{ + return new int(s.length()); +} + +BOOST_AUTO_TEST_CASE( rawPointerFactory ) +{ + RawLazyPointer value(boost::bind(&rawFactory, std::string("four"))); + BOOST_REQUIRE(!value.hasValue()); + BOOST_REQUIRE_EQUAL(*value, 4); + BOOST_REQUIRE(value.hasValue()); + delete value; +} + -- cgit v1.2.3