[serializer] Accept pointer & reference in more methods
authorBehdad Esfahbod <behdad@behdad.org>
Wed, 8 May 2019 00:23:46 +0000 (17:23 -0700)
committerBehdad Esfahbod <behdad@behdad.org>
Wed, 8 May 2019 00:25:58 +0000 (17:25 -0700)
src/hb-serialize.hh

index bb26371..21545a8 100644 (file)
@@ -324,14 +324,11 @@ struct hb_serialize_context_t
   }
 
   template <typename Type>
-  Type *start_embed (const Type &_ HB_UNUSED) const
-  { return start_embed<Type> (); }
+  Type *start_embed (const Type *obj HB_UNUSED = nullptr) const
+  { return reinterpret_cast<Type *> (this->head); }
   template <typename Type>
-  Type *start_embed (const Type *_ HB_UNUSED = nullptr) const
-  {
-    Type *ret = reinterpret_cast<Type *> (this->head);
-    return ret;
-  }
+  Type *start_embed (const Type &obj) const
+  { return start_embed (hb_addressof (obj)); }
 
   /* Following two functions exist to allow setting breakpoint on. */
   void err_ran_out_of_room () { this->ran_out_of_room = true; }
@@ -391,25 +388,37 @@ struct hb_serialize_context_t
   template <typename Type, typename ...Ts>
   Type *copy (const Type &src, Ts &&...ds)
   { return _copy (src, hb_prioritize, hb_forward<Ts> (ds)...); }
+  template <typename Type, typename ...Ts>
+  Type *copy (const Type *src, Ts &&...ds)
+  { return copy (*src, hb_forward<Ts> (ds)...); }
 
   template <typename Type>
   hb_serialize_context_t& operator << (const Type &obj) & { embed (obj); return *this; }
 
   template <typename Type>
-  Type *extend_size (Type &obj, unsigned int size)
+  Type *extend_size (Type *obj, unsigned int size)
   {
-    assert (this->start <= (char *) &obj);
-    assert ((char *) &obj <= this->head);
-    assert ((char *) &obj + size >= this->head);
-    if (unlikely (!this->allocate_size<Type> (((char *) &obj) + size - this->head))) return nullptr;
-    return reinterpret_cast<Type *> (&obj);
+    assert (this->start <= (char *) obj);
+    assert ((char *) obj <= this->head);
+    assert ((char *) obj + size >= this->head);
+    if (unlikely (!this->allocate_size<Type> (((char *) obj) + size - this->head))) return nullptr;
+    return reinterpret_cast<Type *> (obj);
   }
+  template <typename Type>
+  Type *extend_size (Type &obj, unsigned int size)
+  { return extend_size (hb_addressof (obj), size); }
 
   template <typename Type>
-  Type *extend_min (Type &obj) { return extend_size (obj, obj.min_size); }
+  Type *extend_min (Type *obj) { return extend_size (obj, obj->min_size); }
+  template <typename Type>
+  Type *extend_min (Type &obj) { return extend_min (hb_addressof (obj)); }
 
   template <typename Type, typename ...Ts>
-  Type *extend (Type &obj, Ts &&...ds) { return extend_size (obj, obj.get_size (hb_forward<Ts> (ds)...)); }
+  Type *extend (Type *obj, Ts &&...ds)
+  { return extend_size (obj, obj->get_size (hb_forward<Ts> (ds)...)); }
+  template <typename Type, typename ...Ts>
+  Type *extend (Type &obj, Ts &&...ds)
+  { return extend (hb_addressof (obj), hb_forward<Ts> (ds)...); }
 
   /* Output routines. */
   hb_bytes_t copy_bytes () const