From 1220dc7d3e3ce669e6db27b75339ec8a8e2f00e3 Mon Sep 17 00:00:00 2001 From: Michael Davidsaver Date: Sun, 19 Jan 2020 16:13:47 -0800 Subject: [PATCH] fix Value::as(T&) and add Value::as(fn&&) --- src/pvxs/data.h | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/pvxs/data.h b/src/pvxs/data.h index ec0cd88..a106f2d 100644 --- a/src/pvxs/data.h +++ b/src/pvxs/data.h @@ -418,8 +418,24 @@ public: template inline bool as(T& val) const { typedef impl::StorageMap::type> map_t; - typename map_t::store_t ret; - return tryCopyOut(&ret, map_t::code); + typename map_t::store_t temp; + auto ret = tryCopyOut(&temp, map_t::code); + if(ret) { + val = temp; + } + return ret; + } + + //! Attempt to extract value from field. + //! If possible, this value is cast to T and passed as the only argument + //! of the provided function. + template + void as(FN&& fn) { + typedef impl::StorageMap::type> map_t; + typename map_t::store_t val; + if(tryCopyOut(&val, map_t::code)) { + fn(val); + } } //! Attempt to assign to field.