From cff59487ae60fe5c8e509a4ee0fc373ba18604a8 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Tue, 9 Jul 2013 18:21:13 -0400 Subject: [PATCH] shared_vector freeze and thaw --- pvDataApp/misc/sharedVector.h | 36 ++++++++++++++++++++++++++++++++ testApp/pv/testPVScalarArray.cpp | 4 +--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/pvDataApp/misc/sharedVector.h b/pvDataApp/misc/sharedVector.h index d932b19..bc90ae8 100644 --- a/pvDataApp/misc/sharedVector.h +++ b/pvDataApp/misc/sharedVector.h @@ -670,6 +670,42 @@ const_shared_vector_cast(const shared_vector& src) src.dataCount()); } +/** @brief transform a shared_vector to shared_vector + * + * 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 +static FORCE_INLINE +shared_vector::type> +freeze(SRC& src) +{ + if(!src.unique()) + throw std::runtime_error("Can't freeze non-unique vector"); + typedef typename meta::decorate_const::type const_value; + shared_vector ret(src); + src.clear(); + return ret; +} + +/** @brief transform a shared_vector to shared_vector + * + * Transform a reference to read-only data into a unique reference to mutable data. + * + * The reference to read-only data is cleared. + */ +template +static FORCE_INLINE +shared_vector::type> +thaw(SRC& src) +{ + typedef typename meta::strip_const::type value; + shared_vector ret(const_shared_vector_cast(src)); + src.clear(); + ret.make_unique(); + return ret; +} namespace ScalarTypeFunc { diff --git a/testApp/pv/testPVScalarArray.cpp b/testApp/pv/testPVScalarArray.cpp index 54f3fb8..e5de290 100644 --- a/testApp/pv/testPVScalarArray.cpp +++ b/testApp/pv/testPVScalarArray.cpp @@ -133,9 +133,7 @@ static void testBasic() testOk1(idata.at(1)==10); - PVIntArray::svector wdata(const_shared_vector_cast(idata)); - idata.clear(); - wdata.make_unique(); + PVIntArray::svector wdata(thaw(idata)); wdata.at(1) = 42;