diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-06 11:30:10 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2018-04-06 12:17:49 +0100 |
commit | 15554ac00930ab4ecc5f1dc34815a3555b460615 (patch) | |
tree | fee32938796e860e1e4841321278788adc353d2d | |
parent | C++17 (diff) | |
download | libadhocutil-15554ac00930ab4ecc5f1dc34815a3555b460615.tar.bz2 libadhocutil-15554ac00930ab4ecc5f1dc34815a3555b460615.tar.xz libadhocutil-15554ac00930ab4ecc5f1dc34815a3555b460615.zip |
C++17
Remove all boost things now in the standard library from lazyPointer.
-rw-r--r-- | libadhocutil/lazyPointer.h | 26 | ||||
-rw-r--r-- | 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 <boost/function.hpp> -#include <boost/variant.hpp> -#include <boost/intrusive_ptr.hpp> -#include <boost/get_pointer.hpp> +#include <functional> +#include <variant> +#include <memory> #include <ostream> 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 <typename T, typename P = boost::intrusive_ptr<T>> +template <typename T, typename P = std::shared_ptr<T>> class LazyPointer { public: /// @cond typedef T element_type; typedef P pointer_type; - typedef boost::function0<P> Factory; - typedef boost::variant<P, Factory> Source; + typedef std::function<P()> Factory; + typedef std::variant<P, Factory> 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<P>::value) { + return deref(); + } + else { + return deref().get(); + } } P deref() const { - if (Factory * f = boost::get<Factory>(&source)) { + if (Factory * f = std::get_if<Factory>(&source)) { P p = (*f)(); source = p; return p; } else { - return boost::get<P>(source); + return std::get<P>(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<P>(&source); + return std::get_if<P>(&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 <boost/test/unit_test.hpp> -#include <boost/bind.hpp> -#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<Test>(3); } static TestLazyPointer::pointer_type paramFactory(const std::string & str) { - return new Test(str.length()); + return std::make_shared<Test>(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()); |