|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r55300 - in sandbox/fmhess: boost/generic_ptr libs/generic_ptr/test
From: fmhess_at_[hidden]
Date: 2009-07-30 16:32:11
Author: fmhess
Date: 2009-07-30 16:23:14 EDT (Thu, 30 Jul 2009)
New Revision: 55300
URL: http://svn.boost.org/trac/boost/changeset/55300
Log:
Added "nonnull" and "asserting" generic pointers. Removed
some unnecessary copy constructors and assignment operators.
Added:
sandbox/fmhess/boost/generic_ptr/asserting.hpp (contents, props changed)
- copied, changed from r55297, /sandbox/fmhess/boost/generic_ptr/throwing.hpp
sandbox/fmhess/boost/generic_ptr/nonnull.hpp (contents, props changed)
- copied, changed from r55297, /sandbox/fmhess/boost/generic_ptr/throwing.hpp
Text files modified:
sandbox/fmhess/boost/generic_ptr/asserting.hpp | 95 ++++++++++++++++-------------------
sandbox/fmhess/boost/generic_ptr/cloning.hpp | 3 +
sandbox/fmhess/boost/generic_ptr/monitor.hpp | 8 --
sandbox/fmhess/boost/generic_ptr/nonnull.hpp | 107 ++++++++++++++++-----------------------
sandbox/fmhess/boost/generic_ptr/throwing.hpp | 9 +--
sandbox/fmhess/libs/generic_ptr/test/basic_generic_pointer_test.cpp | 18 ++++++
sandbox/fmhess/libs/generic_ptr/test/throwing_test.cpp | 26 +++++++++
7 files changed, 140 insertions(+), 126 deletions(-)
Copied: sandbox/fmhess/boost/generic_ptr/asserting.hpp (from r55297, /sandbox/fmhess/boost/generic_ptr/throwing.hpp)
==============================================================================
--- /sandbox/fmhess/boost/generic_ptr/throwing.hpp (original)
+++ sandbox/fmhess/boost/generic_ptr/asserting.hpp 2009-07-30 16:23:14 EDT (Thu, 30 Jul 2009)
@@ -1,5 +1,5 @@
//
-// generic_ptr/throwing.hpp
+// generic_ptr/asserting.hpp
//
// Copyright (c) 2009 Frank Mori Hess
// Copyright (c) 2001, 2002 Peter Dimov
@@ -11,22 +11,23 @@
// See http://www.boost.org/libs/generic_ptr for documentation.
//
-#ifndef BOOST_GENERIC_PTR_THROWING_HPP_INCLUDED
-#define BOOST_GENERIC_PTR_THROWING_HPP_INCLUDED
+#ifndef BOOST_GENERIC_PTR_ASSERTING_HPP_INCLUDED
+#define BOOST_GENERIC_PTR_ASSERTING_HPP_INCLUDED
+#include <boost/assert.hpp>
+#include <boost/generic_ptr/pointer_cast.hpp>
#include <boost/generic_ptr/pointer_traits.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/utility/swap.hpp>
-#include <stdexcept>
namespace boost
{
namespace generic_ptr
{
template<typename T>
- class throwing
+ class asserting
{
- typedef throwing this_type; // for detail/operator_bool.hpp
+ typedef asserting this_type; // for detail/operator_bool.hpp
public:
typedef typename pointer_traits<T>::value_type value_type;
typedef T pointer;
@@ -35,62 +36,58 @@
template<typename ValueType>
struct rebind
{
- typedef throwing<typename generic_ptr::rebind<pointer, ValueType>::other> other;
+ typedef asserting<typename generic_ptr::rebind<pointer, ValueType>::other> other;
};
- throwing(): px()
+ asserting(): px()
{}
template<typename U>
- throwing( U p ): px( p )
+ asserting( U p ): px( p )
{}
template<typename U>
- throwing(const throwing<U> & other): px(other.px)
+ asserting(const asserting<U> & other): px(other.px)
{}
#ifndef BOOST_NO_RVALUE_REFERENCES
- throwing(throwing && other): px(std::move(other.px))
+ asserting(asserting && other): px(std::move(other.px))
{}
template<typename U>
- throwing(throwing<U> && other): px(std::move(other.px))
+ asserting(asserting<U> && other): px(std::move(other.px))
{}
#endif
- void swap(throwing & other)
- {
- boost::swap(px, other.px);
- }
+ // default copy constructor and assignment operator are fine
- throwing & operator=(const throwing & other)
+ void swap(asserting & other)
{
- throwing(other).swap(*this);
- return *this;
+ boost::swap(px, other.px);
}
template<typename U>
- throwing & operator=(const throwing<U> & other)
+ asserting & operator=(const asserting<U> & other)
{
- throwing(other).swap(*this);
+ asserting(other).swap(*this);
return *this;
}
#ifndef BOOST_NO_RVALUE_REFERENCES
- throwing & operator=(throwing && other)
+ asserting & operator=(asserting && other)
{
- throwing(std::move(other)).swap(*this);
+ asserting(std::move(other)).swap(*this);
return *this;
}
template<typename U>
- throwing & operator=(throwing<U> && other)
+ asserting & operator=(asserting<U> && other)
{
- throwing(std::move(other)).swap(*this);
+ asserting(std::move(other)).swap(*this);
return *this;
}
#endif
void reset()
{
- throwing().swap(*this);
+ asserting().swap(*this);
}
template<typename U> void reset(U p)
{
- throwing(p).swap(*this);
+ asserting(p).swap(*this);
}
pointer get() const {return px;}
@@ -100,19 +97,13 @@
pointer operator->() const
{
- if(get_plain_old_pointer(px) == 0)
- {
- throw std::invalid_argument("Attempted to access object through null pointer.");
- }
+ BOOST_ASSERT(get_plain_old_pointer(px));
return px;
}
reference operator*() const
{
- if(get_plain_old_pointer(px) == 0)
- {
- throw std::invalid_argument("Attempted to dereference null pointer.");
- }
+ BOOST_ASSERT(get_plain_old_pointer(px));
return *px;
}
@@ -126,34 +117,34 @@
};
template<typename T>
- T get_pointer(const throwing<T> &p)
+ T get_pointer(const asserting<T> &p)
{
return p.get();
}
// casts
template<typename ToValueType, typename U>
- typename rebind<throwing<U>, ToValueType>::other static_pointer_cast
+ typename rebind<asserting<U>, ToValueType>::other static_pointer_cast
(
- throwing<U> const & p,
+ asserting<U> const & p,
mpl::identity<ToValueType> to_type_iden = mpl::identity<ToValueType>()
)
{
return static_pointer_cast(p.get(), to_type_iden);
}
template<typename ToValueType, typename U>
- typename rebind<throwing<U>, ToValueType>::other const_pointer_cast
+ typename rebind<asserting<U>, ToValueType>::other const_pointer_cast
(
- throwing<U> const & p,
+ asserting<U> const & p,
mpl::identity<ToValueType> to_type_iden = mpl::identity<ToValueType>()
)
{
return const_pointer_cast(p.get(), to_type_iden);
}
template<typename ToValueType, typename U>
- typename rebind<throwing<U>, ToValueType>::other dynamic_pointer_cast
+ typename rebind<asserting<U>, ToValueType>::other dynamic_pointer_cast
(
- throwing<U> const & p,
+ asserting<U> const & p,
mpl::identity<ToValueType> to_type_iden = mpl::identity<ToValueType>()
)
{
@@ -161,42 +152,42 @@
}
// comparisons
- template<class T, class U> inline bool operator==(throwing<T> const & a, throwing<U> const & b)
+ template<class T, class U> inline bool operator==(asserting<T> const & a, asserting<U> const & b)
{
return a.get() == b.get();
}
- template<class T, class U> inline bool operator!=(throwing<T> const & a, throwing<U> const & b)
+ template<class T, class U> inline bool operator!=(asserting<T> const & a, asserting<U> const & b)
{
return a.get() != b.get();
}
- template<class T, class U> inline bool operator==(throwing<T> const & a, U const & b)
+ template<class T, class U> inline bool operator==(asserting<T> const & a, U const & b)
{
return a.get() == b;
}
- template<class T, class U> inline bool operator!=(throwing<T> const & a, U const & b)
+ template<class T, class U> inline bool operator!=(asserting<T> const & a, U const & b)
{
return a.get() != b;
}
- template<class T, class U> inline bool operator==(T const & a, throwing<U> const & b)
+ template<class T, class U> inline bool operator==(T const & a, asserting<U> const & b)
{
return a == b.get();
}
- template<class T, class U> inline bool operator!=(T const & a, throwing<U> const & b)
+ template<class T, class U> inline bool operator!=(T const & a, asserting<U> const & b)
{
return a != b.get();
}
#if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
// Resolve the ambiguity between our op!= and the one in rel_ops
- template<class T> inline bool operator!=(throwing<T> const & a, throwing<T> const & b)
+ template<class T> inline bool operator!=(asserting<T> const & a, asserting<T> const & b)
{
return a.get() != b.get();
}
#endif
- template<class T> inline bool operator<(throwing<T> const & a, throwing<T> const & b)
+ template<class T> inline bool operator<(asserting<T> const & a, asserting<T> const & b)
{
- return std::less<typename throwing<T>::pointer>()(a.get(), b.get());
+ return std::less<typename asserting<T>::pointer>()(a.get(), b.get());
}
} // namespace generic_ptr
} // namespace boost
-#endif // #ifndef BOOST_GENERIC_PTR_THROWING_HPP_INCLUDED
+#endif // #ifndef BOOST_GENERIC_PTR_ASSERTING_HPP_INCLUDED
Modified: sandbox/fmhess/boost/generic_ptr/cloning.hpp
==============================================================================
--- sandbox/fmhess/boost/generic_ptr/cloning.hpp (original)
+++ sandbox/fmhess/boost/generic_ptr/cloning.hpp 2009-07-30 16:23:14 EDT (Thu, 30 Jul 2009)
@@ -17,6 +17,7 @@
#include <boost/assert.hpp>
#include <boost/config.hpp>
#include <boost/generic_ptr/detail/util.hpp>
+#include <boost/generic_ptr/pointer_cast.hpp>
#include <boost/generic_ptr/pointer_traits.hpp>
#include <boost/generic_ptr/shared.hpp>
#include <boost/mpl/identity.hpp>
@@ -48,6 +49,7 @@
{
public:
virtual ~clone_factory_impl_base() {}
+ //FIXME: this isn't adequate. We need to return a generic pointer to void instead
virtual void * get_pointer() = 0;
virtual clone_factory_impl_base* make_clone() = 0;
};
@@ -249,6 +251,7 @@
)
)
{
+ // reset _cloner if dynamic cast failed
if(get_plain_old_pointer(px) == 0)
{
detail::clone_factory().swap(_cloner);
Modified: sandbox/fmhess/boost/generic_ptr/monitor.hpp
==============================================================================
--- sandbox/fmhess/boost/generic_ptr/monitor.hpp (original)
+++ sandbox/fmhess/boost/generic_ptr/monitor.hpp 2009-07-30 16:23:14 EDT (Thu, 30 Jul 2009)
@@ -79,18 +79,14 @@
{}
#endif
+ // default copy constructor and assignment operator are fine
+
void swap(monitor & other)
{
boost::swap(px, other.px);
boost::swap(_mutex_p, other._mutex_p);
}
- monitor & operator=(const monitor & other)
- {
- monitor(other).swap(*this);
- return *this;
- }
-
template<typename U>
monitor & operator=(const monitor<U, mutex_type> & other)
{
Copied: sandbox/fmhess/boost/generic_ptr/nonnull.hpp (from r55297, /sandbox/fmhess/boost/generic_ptr/throwing.hpp)
==============================================================================
--- /sandbox/fmhess/boost/generic_ptr/throwing.hpp (original)
+++ sandbox/fmhess/boost/generic_ptr/nonnull.hpp 2009-07-30 16:23:14 EDT (Thu, 30 Jul 2009)
@@ -1,5 +1,5 @@
//
-// generic_ptr/throwing.hpp
+// generic_ptr/nonnull.hpp
//
// Copyright (c) 2009 Frank Mori Hess
// Copyright (c) 2001, 2002 Peter Dimov
@@ -11,22 +11,23 @@
// See http://www.boost.org/libs/generic_ptr for documentation.
//
-#ifndef BOOST_GENERIC_PTR_THROWING_HPP_INCLUDED
-#define BOOST_GENERIC_PTR_THROWING_HPP_INCLUDED
+#ifndef BOOST_GENERIC_PTR_NONNULL_HPP_INCLUDED
+#define BOOST_GENERIC_PTR_NONNULL_HPP_INCLUDED
+#include <boost/assert.hpp>
+#include <boost/generic_ptr/pointer_cast.hpp>
#include <boost/generic_ptr/pointer_traits.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/utility/swap.hpp>
-#include <stdexcept>
namespace boost
{
namespace generic_ptr
{
template<typename T>
- class throwing
+ class nonnull
{
- typedef throwing this_type; // for detail/operator_bool.hpp
+ typedef nonnull this_type; // for detail/operator_bool.hpp
public:
typedef typename pointer_traits<T>::value_type value_type;
typedef T pointer;
@@ -35,62 +36,52 @@
template<typename ValueType>
struct rebind
{
- typedef throwing<typename generic_ptr::rebind<pointer, ValueType>::other> other;
+ typedef nonnull<typename generic_ptr::rebind<pointer, ValueType>::other> other;
};
- throwing(): px()
- {}
- template<typename U>
- throwing( U p ): px( p )
- {}
template<typename U>
- throwing(const throwing<U> & other): px(other.px)
- {}
-#ifndef BOOST_NO_RVALUE_REFERENCES
- throwing(throwing && other): px(std::move(other.px))
- {}
+ nonnull( U p ): px( p )
+ {
+ if(get_plain_old_pointer(px) == 0)
+ {
+ throw std::invalid_argument("Attempted to create a generic_ptr::nonnull with a null pointer.");
+ }
+ }
template<typename U>
- throwing(throwing<U> && other): px(std::move(other.px))
+ nonnull(const nonnull<U> & other): px(other.px)
{}
-#endif
- void swap(throwing & other)
- {
- boost::swap(px, other.px);
- }
+ // no move constructors because they could violate the nonnull invariant of the moved-from object
- throwing & operator=(const throwing & other)
+ // default copy constructor and assignment operator are fine
+
+ void swap(nonnull & other)
{
- throwing(other).swap(*this);
- return *this;
+ boost::swap(px, other.px);
}
template<typename U>
- throwing & operator=(const throwing<U> & other)
+ nonnull & operator=(const nonnull<U> & other)
{
- throwing(other).swap(*this);
+ nonnull(other).swap(*this);
return *this;
}
#ifndef BOOST_NO_RVALUE_REFERENCES
- throwing & operator=(throwing && other)
+ nonnull & operator=(nonnull && other)
{
- throwing(std::move(other)).swap(*this);
+ other.swap(*this);
return *this;
}
template<typename U>
- throwing & operator=(throwing<U> && other)
+ nonnull & operator=(nonnull<U> && other)
{
- throwing(std::move(other)).swap(*this);
+ other.swap(*this);
return *this;
}
#endif
- void reset()
- {
- throwing().swap(*this);
- }
template<typename U> void reset(U p)
{
- throwing(p).swap(*this);
+ nonnull(p).swap(*this);
}
pointer get() const {return px;}
@@ -100,19 +91,11 @@
pointer operator->() const
{
- if(get_plain_old_pointer(px) == 0)
- {
- throw std::invalid_argument("Attempted to access object through null pointer.");
- }
return px;
}
reference operator*() const
{
- if(get_plain_old_pointer(px) == 0)
- {
- throw std::invalid_argument("Attempted to dereference null pointer.");
- }
return *px;
}
@@ -126,34 +109,34 @@
};
template<typename T>
- T get_pointer(const throwing<T> &p)
+ T get_pointer(const nonnull<T> &p)
{
return p.get();
}
// casts
template<typename ToValueType, typename U>
- typename rebind<throwing<U>, ToValueType>::other static_pointer_cast
+ typename rebind<nonnull<U>, ToValueType>::other static_pointer_cast
(
- throwing<U> const & p,
+ nonnull<U> const & p,
mpl::identity<ToValueType> to_type_iden = mpl::identity<ToValueType>()
)
{
return static_pointer_cast(p.get(), to_type_iden);
}
template<typename ToValueType, typename U>
- typename rebind<throwing<U>, ToValueType>::other const_pointer_cast
+ typename rebind<nonnull<U>, ToValueType>::other const_pointer_cast
(
- throwing<U> const & p,
+ nonnull<U> const & p,
mpl::identity<ToValueType> to_type_iden = mpl::identity<ToValueType>()
)
{
return const_pointer_cast(p.get(), to_type_iden);
}
template<typename ToValueType, typename U>
- typename rebind<throwing<U>, ToValueType>::other dynamic_pointer_cast
+ typename rebind<nonnull<U>, ToValueType>::other dynamic_pointer_cast
(
- throwing<U> const & p,
+ nonnull<U> const & p,
mpl::identity<ToValueType> to_type_iden = mpl::identity<ToValueType>()
)
{
@@ -161,42 +144,42 @@
}
// comparisons
- template<class T, class U> inline bool operator==(throwing<T> const & a, throwing<U> const & b)
+ template<class T, class U> inline bool operator==(nonnull<T> const & a, nonnull<U> const & b)
{
return a.get() == b.get();
}
- template<class T, class U> inline bool operator!=(throwing<T> const & a, throwing<U> const & b)
+ template<class T, class U> inline bool operator!=(nonnull<T> const & a, nonnull<U> const & b)
{
return a.get() != b.get();
}
- template<class T, class U> inline bool operator==(throwing<T> const & a, U const & b)
+ template<class T, class U> inline bool operator==(nonnull<T> const & a, U const & b)
{
return a.get() == b;
}
- template<class T, class U> inline bool operator!=(throwing<T> const & a, U const & b)
+ template<class T, class U> inline bool operator!=(nonnull<T> const & a, U const & b)
{
return a.get() != b;
}
- template<class T, class U> inline bool operator==(T const & a, throwing<U> const & b)
+ template<class T, class U> inline bool operator==(T const & a, nonnull<U> const & b)
{
return a == b.get();
}
- template<class T, class U> inline bool operator!=(T const & a, throwing<U> const & b)
+ template<class T, class U> inline bool operator!=(T const & a, nonnull<U> const & b)
{
return a != b.get();
}
#if __GNUC__ == 2 && __GNUC_MINOR__ <= 96
// Resolve the ambiguity between our op!= and the one in rel_ops
- template<class T> inline bool operator!=(throwing<T> const & a, throwing<T> const & b)
+ template<class T> inline bool operator!=(nonnull<T> const & a, nonnull<T> const & b)
{
return a.get() != b.get();
}
#endif
- template<class T> inline bool operator<(throwing<T> const & a, throwing<T> const & b)
+ template<class T> inline bool operator<(nonnull<T> const & a, nonnull<T> const & b)
{
- return std::less<typename throwing<T>::pointer>()(a.get(), b.get());
+ return std::less<typename nonnull<T>::pointer>()(a.get(), b.get());
}
} // namespace generic_ptr
} // namespace boost
-#endif // #ifndef BOOST_GENERIC_PTR_THROWING_HPP_INCLUDED
+#endif // #ifndef BOOST_GENERIC_PTR_NONNULL_HPP_INCLUDED
Modified: sandbox/fmhess/boost/generic_ptr/throwing.hpp
==============================================================================
--- sandbox/fmhess/boost/generic_ptr/throwing.hpp (original)
+++ sandbox/fmhess/boost/generic_ptr/throwing.hpp 2009-07-30 16:23:14 EDT (Thu, 30 Jul 2009)
@@ -14,6 +14,7 @@
#ifndef BOOST_GENERIC_PTR_THROWING_HPP_INCLUDED
#define BOOST_GENERIC_PTR_THROWING_HPP_INCLUDED
+#include <boost/generic_ptr/pointer_cast.hpp>
#include <boost/generic_ptr/pointer_traits.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/utility/swap.hpp>
@@ -54,17 +55,13 @@
{}
#endif
+ // default copy constructor and assignment operator are fine
+
void swap(throwing & other)
{
boost::swap(px, other.px);
}
- throwing & operator=(const throwing & other)
- {
- throwing(other).swap(*this);
- return *this;
- }
-
template<typename U>
throwing & operator=(const throwing<U> & other)
{
Modified: sandbox/fmhess/libs/generic_ptr/test/basic_generic_pointer_test.cpp
==============================================================================
--- sandbox/fmhess/libs/generic_ptr/test/basic_generic_pointer_test.cpp (original)
+++ sandbox/fmhess/libs/generic_ptr/test/basic_generic_pointer_test.cpp 2009-07-30 16:23:14 EDT (Thu, 30 Jul 2009)
@@ -10,9 +10,11 @@
#include <boost/config.hpp>
#include <boost/detail/lightweight_test.hpp>
+#include <boost/generic_ptr/asserting.hpp>
#include <boost/generic_ptr/cloning.hpp>
#include <boost/generic_ptr/intrusive.hpp>
#include <boost/generic_ptr/monitor.hpp>
+#include <boost/generic_ptr/nonnull.hpp>
#include <boost/generic_ptr/pointer_cast.hpp>
#include <boost/generic_ptr/pointer_traits.hpp>
#include <boost/generic_ptr/shared.hpp>
@@ -95,6 +97,14 @@
}
{
X x;
+ boost::generic_ptr::asserting<X*> p(&x);
+ member_access_test(p);
+ dereference_test(p);
+ rebind_test(p);
+ cast_test(p);
+ }
+ {
+ X x;
boost::generic_ptr::throwing<X*> p(&x);
member_access_test(p);
dereference_test(p);
@@ -103,6 +113,14 @@
}
{
X x;
+ boost::generic_ptr::nonnull<X*> p(&x);
+ member_access_test(p);
+ dereference_test(p);
+ rebind_test(p);
+ cast_test(p);
+ }
+ {
+ X x;
boost::generic_ptr::monitor<X*> p(&x);
member_access_test(p);
// dereference_test(p); // monitors don't support dereference
Modified: sandbox/fmhess/libs/generic_ptr/test/throwing_test.cpp
==============================================================================
--- sandbox/fmhess/libs/generic_ptr/test/throwing_test.cpp (original)
+++ sandbox/fmhess/libs/generic_ptr/test/throwing_test.cpp 2009-07-30 16:23:14 EDT (Thu, 30 Jul 2009)
@@ -23,8 +23,34 @@
BOOST_TEST(p == &x);
}
+class X
+{
+public:
+ void f() {}
+};
+
+void throw_test()
+{
+ boost::generic_ptr::throwing<X*> tp(static_cast<X*>(0));
+ try
+ {
+ *tp;
+ BOOST_TEST(false);
+ }catch(const std::invalid_argument &)
+ {
+ }
+ try
+ {
+ tp->f();
+ BOOST_TEST(false);
+ }catch(const std::invalid_argument &)
+ {
+ }
+}
+
int main()
{
implicit_conversion_test();
+ throw_test();
return 0;
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk