shared_vector freeze and thaw

This commit is contained in:
Michael Davidsaver
2013-07-09 18:21:13 -04:00
parent cdcbfe7378
commit cff59487ae
2 changed files with 37 additions and 3 deletions

View File

@@ -670,6 +670,42 @@ const_shared_vector_cast(const shared_vector<const TYPE>& src)
src.dataCount());
}
/** @brief transform a shared_vector<T> to shared_vector<const T>
*
* Transform a reference to mutable data into a reference to read-only data.
* Throws an exception unless the reference to mutable data is unique.
* On success the reference to mutable data is cleared.
*/
template<typename SRC>
static FORCE_INLINE
shared_vector<typename meta::decorate_const<typename SRC::value_type>::type>
freeze(SRC& src)
{
if(!src.unique())
throw std::runtime_error("Can't freeze non-unique vector");
typedef typename meta::decorate_const<typename SRC::value_type>::type const_value;
shared_vector<const_value> ret(src);
src.clear();
return ret;
}
/** @brief transform a shared_vector<const T> to shared_vector<T>
*
* Transform a reference to read-only data into a unique reference to mutable data.
*
* The reference to read-only data is cleared.
*/
template<typename SRC>
static FORCE_INLINE
shared_vector<typename meta::strip_const<typename SRC::value_type>::type>
thaw(SRC& src)
{
typedef typename meta::strip_const<typename SRC::value_type>::type value;
shared_vector<value> ret(const_shared_vector_cast<value>(src));
src.clear();
ret.make_unique();
return ret;
}
namespace ScalarTypeFunc {

View File

@@ -133,9 +133,7 @@ static void testBasic()
testOk1(idata.at(1)==10);
PVIntArray::svector wdata(const_shared_vector_cast<int32>(idata));
idata.clear();
wdata.make_unique();
PVIntArray::svector wdata(thaw(idata));
wdata.at(1) = 42;