diff options
| -rw-r--r-- | libadhocutil/lazyPointer.h | 8 | ||||
| -rw-r--r-- | 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 <boost/function.hpp>  #include <boost/variant.hpp>  #include <boost/intrusive_ptr.hpp> +#include <boost/get_pointer.hpp> +#include <ostream>  namespace AdHoc { @@ -37,7 +39,8 @@ class LazyPointer {  		}  		/** Construct pointer with an instance value. */ -		LazyPointer(T * p) : +		template <typename TT = T> +		LazyPointer(T * p, typename std::enable_if<!std::is_same<TT *, P>::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 <typename R, typename T, typename P>  		R * dynamic_pointer_cast(const AdHoc::LazyPointer<T, P> & 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<Test> TestLazyPointer; +typedef LazyPointer<int, int *> 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; +} + | 
