From efbba7ad26dda5930f5d1bd5292304835432f504 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 17 Apr 2019 11:00:08 -0400 Subject: [PATCH] [serializer] Add copy() Calls obj.copy() or obj.operator=() in that order. --- src/hb-serialize.hh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/hb-serialize.hh b/src/hb-serialize.hh index 0d908bd13..a8fd8d32e 100644 --- a/src/hb-serialize.hh +++ b/src/hb-serialize.hh @@ -358,6 +358,24 @@ struct hb_serialize_context_t memcpy (ret, &obj, size); return ret; } + + template auto + _copy (const Type &obj, hb_priority<1>) const HB_RETURN (Type *, obj.copy (this)) + + template auto + _copy (const Type &obj, hb_priority<0>) const -> decltype (&(obj = obj)) + { + Type *ret = this->allocate_size (sizeof (Type)); + if (unlikely (!ret)) return nullptr; + *ret = obj; + return ret; + } + + /* Like embed, but active: calls obj.operator=() or obj.copy() to transfer data + * instead of memcpy(). */ + template + Type *copy (const Type &obj) { return _copy (obj, hb_prioritize); } + template hb_serialize_context_t &operator << (const Type &obj) { embed (obj); return *this; }