[array] Add .copy()
authorBehdad Esfahbod <behdad@behdad.org>
Wed, 8 May 2019 23:37:38 +0000 (16:37 -0700)
committerBehdad Esfahbod <behdad@behdad.org>
Wed, 8 May 2019 23:37:38 +0000 (16:37 -0700)
src/hb-array.hh
src/hb-open-type.hh
src/hb-serialize.hh

index 2da8df0..8902e7a 100644 (file)
@@ -176,6 +176,17 @@ struct hb_array_t : hb_iter_with_fallback_t<hb_array_t<Type>, Type&>
   void free ()
   { ::free ((void *) arrayZ); arrayZ = nullptr; length = 0; }
 
+  template <typename hb_serialize_context_t>
+  hb_array_t copy (hb_serialize_context_t *c) const
+  {
+    TRACE_SERIALIZE (this);
+    auto* out = c->template start_embed (arrayZ);
+    if (unlikely (!c->extend_size (out, get_size ()))) return_trace (hb_array_t ());
+    for (unsigned i = 0; i < length; i++)
+      out[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */
+    return_trace (hb_array_t (out, length));
+  }
+
   template <typename hb_sanitize_context_t>
   bool sanitize (hb_sanitize_context_t *c) const
   { return c->check_array (arrayZ, length); }
index 9d388ff..cacb1a7 100644 (file)
@@ -436,9 +436,7 @@ struct UnsizedArrayOf
   {
     TRACE_SERIALIZE (this);
     auto *out = c->start_embed (this);
-    if (unlikely (!out->serialize (c, count))) return_trace (nullptr);
-    for (unsigned i = 0; i < count; i++)
-      out->arrayZ[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */
+    if (unlikely (!as_array (count).copy (c))) return_trace (nullptr);
     return_trace (out);
   }
 
@@ -618,9 +616,9 @@ struct ArrayOf
     TRACE_SERIALIZE (this);
     auto *out = c->start_embed (this);
     unsigned count = len;
-    if (unlikely (!out->serialize (c, count))) return_trace (nullptr);
-    for (unsigned i = 0; i < count; i++)
-      out->arrayZ[i] = arrayZ[i]; /* TODO: add version that calls c->copy() */
+    if (unlikely (!c->extend_min (out))) return_trace (nullptr);
+    c->check_assign (out->len, len);
+    if (unlikely (!as_array ().copy (c))) return_trace (nullptr);
     return_trace (out);
   }
 
index e0f7980..76f7016 100644 (file)
@@ -323,7 +323,7 @@ struct hb_serialize_context_t
       allocate_size<void> (alignment - l);
   }
 
-  template <typename Type>
+  template <typename Type = void>
   Type *start_embed (const Type *obj HB_UNUSED = nullptr) const
   { return reinterpret_cast<Type *> (this->head); }
   template <typename Type>