From 15554ac00930ab4ecc5f1dc34815a3555b460615 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Fri, 6 Apr 2018 11:30:10 +0100 Subject: C++17 Remove all boost things now in the standard library from lazyPointer. --- libadhocutil/lazyPointer.h | 26 +++++++++++++++----------- libadhocutil/unittests/testLazyPointer.cpp | 16 +++++++--------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/libadhocutil/lazyPointer.h b/libadhocutil/lazyPointer.h index bcc5217..71ad29a 100644 --- a/libadhocutil/lazyPointer.h +++ b/libadhocutil/lazyPointer.h @@ -1,10 +1,9 @@ #ifndef ADHOCUTIL_LAZYPOINTER_H #define ADHOCUTIL_LAZYPOINTER_H -#include -#include -#include -#include +#include +#include +#include #include namespace AdHoc { @@ -16,14 +15,14 @@ namespace AdHoc { * an attempt to dereference the pointer. All such operations will call * this factory function as required prior to evaluating the pointer's value. */ -template > +template > class LazyPointer { public: /// @cond typedef T element_type; typedef P pointer_type; - typedef boost::function0

Factory; - typedef boost::variant Source; + typedef std::function Factory; + typedef std::variant Source; /// @endcond /** Construct pointer with a factory function. */ @@ -70,18 +69,23 @@ class LazyPointer { T * get() const { - return boost::get_pointer(deref()); + if constexpr (std::is_pointer

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

(source); + return std::get

(source); } } @@ -128,7 +132,7 @@ class LazyPointer { /** Does the lazy pointer have a value? (as opposed to a factory). */ bool hasValue() const { - return boost::get

(&source); + return std::get_if

(&source); } private: diff --git a/libadhocutil/unittests/testLazyPointer.cpp b/libadhocutil/unittests/testLazyPointer.cpp index 4ecdd0f..a747f17 100644 --- a/libadhocutil/unittests/testLazyPointer.cpp +++ b/libadhocutil/unittests/testLazyPointer.cpp @@ -1,13 +1,11 @@ #define BOOST_TEST_MODULE LazyPointer #include -#include -#include "intrusivePtrBase.h" #include "lazyPointer.h" using namespace AdHoc; -class Test : public IntrusivePtrBase { +class Test { public: Test(int v) : val(v) @@ -23,19 +21,19 @@ static TestLazyPointer::pointer_type factory() { - return new Test(3); + return std::make_shared(3); } static TestLazyPointer::pointer_type paramFactory(const std::string & str) { - return new Test(str.length()); + return std::make_shared(str.length()); } BOOST_AUTO_TEST_CASE ( islazy ) { - TestLazyPointer p(boost::bind(&factory)); + TestLazyPointer p(std::bind(&factory)); BOOST_REQUIRE_EQUAL(false, p.hasValue()); Test * t = p.get(); BOOST_REQUIRE(t); @@ -65,7 +63,7 @@ BOOST_AUTO_TEST_CASE ( reset ) p = nullptr; BOOST_REQUIRE_EQUAL(true, p.hasValue()); BOOST_REQUIRE_EQUAL(true, !p); - p = boost::bind(&factory); + p = std::bind(&factory); BOOST_REQUIRE_EQUAL(false, p.hasValue()); p.get(); BOOST_REQUIRE_EQUAL(true, p.hasValue()); @@ -74,7 +72,7 @@ BOOST_AUTO_TEST_CASE ( reset ) BOOST_AUTO_TEST_CASE ( nondefault ) { - TestLazyPointer p(boost::bind(¶mFactory, "some string")); + TestLazyPointer p(std::bind(¶mFactory, "some string")); BOOST_REQUIRE_EQUAL(false, p.hasValue()); BOOST_REQUIRE_EQUAL(11, (*p).val); BOOST_REQUIRE_EQUAL(true, p.hasValue()); @@ -108,7 +106,7 @@ rawFactory(const std::string & s) BOOST_AUTO_TEST_CASE( rawPointerFactory ) { - RawLazyPointer value(boost::bind(&rawFactory, std::string("four"))); + RawLazyPointer value(std::bind(&rawFactory, std::string("four"))); BOOST_REQUIRE(!value.hasValue()); BOOST_REQUIRE_EQUAL(*value, 4); BOOST_REQUIRE(value.hasValue()); -- cgit v1.2.3