remove weak_vector

It seems that shared_ptr::use_count() does
not include weak_ptr instances.  Therefore
shared_ptr::use_count()==1 (aka unique())
does *not* ensure exclusive ownership!

This breaks the assumption used by
shared_vector::make_unique() to avoid
allocating a new array in some cases.
This commit is contained in:
Michael Davidsaver
2013-06-10 14:48:59 -04:00
parent b63c3da565
commit be4738f59c
2 changed files with 34 additions and 48 deletions
+7 -47
View File
@@ -232,6 +232,13 @@ namespace detail {
* std::vector are outlined in @ref vectordiff .
*
* Also see @ref vectormem
*
* @warning Due to the implementation of std::tr1::shared_ptr, use of
* shared_vector should not be combined with use of weak_ptr.
* shared_ptr::unique() and shared_ptr::use_count() do @b not
* include weak_ptr instances. This breaks the assumption made
* by make_unique() that unique()==true implies exclusive
* ownership.
*/
template<typename E>
class shared_vector : public detail::shared_vector_base<E>
@@ -527,53 +534,6 @@ public:
}
};
template<typename E>
class weak_vector {
std::tr1::weak_ptr<E> m_data;
//! Offset in the data array of first element
size_t m_offset;
//! Number of elements between m_offset and end of data
size_t m_count;
public:
typedef E element_type;
weak_vector()
:m_data()
,m_offset(0)
,m_count(0)
{}
weak_vector(const weak_vector& o)
:m_data(o.m_data)
,m_offset(o.m_offset)
,m_count(o.m_count)
{}
weak_vector(const shared_vector<E>& o)
:m_data(o.dataPtr())
,m_offset(o.dataOffset())
,m_count(o.dataCount())
{}
bool expired() const{return m_data.expired();}
shared_vector<E> lock() const
{
return shared_vector<E>(m_data.lock(), m_offset, m_count);
}
void reset()
{
m_data.reset();
m_offset = m_count = 0;
}
void swap(weak_vector& o)
{
m_data.swap(o);
std::swap(m_offset, o.m_offset);
std::swap(m_count, o.m_count);
}
};
namespace detail {
template<typename TO, typename FROM> struct shared_vector_caster {};
+27 -1
View File
@@ -382,9 +382,34 @@ static void testNonPOD()
testOk1(structs2[1].get()==temp);
}
static void testWeak()
{
testDiag("Test weak_ptr counting");
epics::pvData::shared_vector<int> data(6);
testOk1(data.unique());
std::tr1::shared_ptr<int> pdata(data.dataPtr());
testOk1(!data.unique());
pdata.reset();
testOk1(data.unique());
std::tr1::weak_ptr<int> wdata(data.dataPtr());
testOk1(data.unique()); // True, but I wish it wasn't!!!
pdata = wdata.lock();
testOk1(!data.unique());
}
MAIN(testSharedVector)
{
testPlan(116);
testPlan(121);
testDiag("Tests for shared_vector");
testDiag("sizeof(shared_vector<int>)=%lu",
@@ -399,5 +424,6 @@ MAIN(testSharedVector)
testSlice();
testVoid();
testNonPOD();
testWeak();
return testDone();
}