Convert the last set of cast macros to templates
authorBehdad Esfahbod <behdad@behdad.org>
Thu, 22 Apr 2010 22:29:09 +0000 (18:29 -0400)
committerBehdad Esfahbod <behdad@behdad.org>
Thu, 22 Apr 2010 22:42:01 +0000 (18:42 -0400)
src/hb-open-file-private.hh
src/hb-open-type-private.hh
src/hb-ot-layout-gpos-private.hh
src/hb-ot-layout-gsub-private.hh
src/hb-ot-layout-gsubgpos-private.hh

index 10de334..605d75c 100644 (file)
@@ -191,8 +191,8 @@ struct OpenTypeFontFile
     if (!SANITIZE_SELF ()) return false;
     switch (tag) {
     default: return true;
-    case TrueTypeTag: case CFFTag: return SANITIZE_THIS (CAST (OffsetTable, *this, 0));
-    case TTCTag: return SANITIZE (CAST (TTCHeader, *this, 0));
+    case TrueTypeTag: case CFFTag: return SANITIZE_THIS (Cast<OffsetTable> (*this));
+    case TTCTag: return SANITIZE (Cast<TTCHeader> (*this));
     }
   }
 
index 4e8f1e4..2ef124a 100644 (file)
 #define NO_INDEX               ((unsigned int) 0xFFFF)
 
 
+
 /*
  * Casts
  */
 
 /* Cast to "const char *" and "char *" */
-template <typename Type> inline const char * CharP (const Type* X) { return reinterpret_cast<const char *>(X); }
-template <typename Type> inline char * CharP (Type* X) { return reinterpret_cast<char *>(X); }
+template <typename Type>
+inline const char * CharP (const Type* X)
+{ return reinterpret_cast<const char *>(X); }
+template <typename Type>
+inline char * CharP (Type* X)
+{ return reinterpret_cast<char *>(X); }
 
-#define CONST_CAST(T,X,Ofs)    (*(reinterpret_cast<const T *>(CharP(&(X)) + Ofs)))
-#define CAST(T,X,Ofs)          (*(reinterpret_cast<T *>(CharP(&(X)) + Ofs)))
+/* Cast to struct T& */
+template<typename Type, typename TObject>
+inline const Type& Cast(const TObject &X)
+{ return reinterpret_cast<const Type&> (X); }
+template<typename Type, typename TObject>
+inline Type& Cast(TObject &X)
+{ return reinterpret_cast<Type&> (X); }
 
+/* StructAtOffset<T>(X,Ofs) returns the struct T& that is placed at memory
+ * location of X plus Ofs bytes. */
+template<typename Type, typename TObject>
+inline const Type& StructAtOffset(const TObject &X, unsigned int offset)
+{ return * reinterpret_cast<const Type*> (CharP (&X) + offset); }
+template<typename Type, typename TObject>
+inline Type& StructAtOffset(TObject &X, unsigned int offset)
+{ return * reinterpret_cast<Type*> (CharP (&X) + offset); }
 
 /* StructAfter<T>(X) returns the struct T& that is placed after X.
  * Works with X of variable size also.  X must implement get_size() */
 template<typename Type, typename TObject>
 inline const Type& StructAfter(const TObject &X)
-{
-  return * reinterpret_cast<const Type*> (CharP (&X) + X.get_size());
-}
+{ return StructAtOffset<Type>(X, X.get_size()); }
 template<typename Type, typename TObject>
 inline Type& StructAfter(TObject &X)
-{
-  return * reinterpret_cast<Type*> (CharP (&X) + X.get_size());
-}
+{ return StructAtOffset<Type>(X, X.get_size()); }
+
 
 
 /*
@@ -76,7 +91,7 @@ static const void *_NullPool[32 / sizeof (void *)];
 template <typename Type>
 static inline const Type& Null () {
   ASSERT_STATIC (sizeof (Type) <= sizeof (_NullPool));
-  return CONST_CAST (Type, *_NullPool, 0);
+  return Cast<Type> (*_NullPool);
 }
 
 /* Specializaiton for arbitrary-content arbitrary-sized Null objects. */
@@ -84,7 +99,7 @@ static inline const Type& Null () {
 static const char _Null##Type[size + 1] = data; /* +1 is for nul-termination in data */ \
 template <> \
 inline const Type& Null<Type> () { \
-  return CONST_CAST (Type, *_Null##Type, 0); \
+  return Cast<Type> (*_Null##Type); \
 } /* The following line really exists such that we end in a place needing semicolon */ \
 ASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type))
 
@@ -99,14 +114,14 @@ ASSERT_STATIC (sizeof (Type) + 1 <= sizeof (_Null##Type))
   static inline const Type& get_for_data (const char *data) \
   { \
     if (HB_UNLIKELY (data == NULL)) return Null(Type); \
-    return CONST_CAST (Type, *data, 0); \
+    return Cast<Type> (*data); \
   }
 /* Like get_for_data(), but checks major version first. */
 #define STATIC_DEFINE_GET_FOR_DATA_CHECK_MAJOR_VERSION(Type, MajorMin, MajorMax) \
   static inline const Type& get_for_data (const char *data) \
   { \
     if (HB_UNLIKELY (data == NULL)) return Null(Type); \
-    const Type& t = CONST_CAST (Type, *data, 0); \
+    const Type& t = Cast<Type> (*data); \
     if (HB_UNLIKELY (t.version.major < MajorMin || t.version.major > MajorMax)) return Null(Type); \
     return t; \
   }
@@ -285,7 +300,7 @@ struct Sanitizer
     _hb_sanitize_init (&context, blob);
 
     /* We drop const here */
-    Type *t = &CAST (Type, * (char *) CharP(context.start), 0);
+    Type *t = &Cast<Type> (* (char *) CharP(context.start));
 
     sane = t->sanitize (SANITIZE_ARG_INIT);
     if (sane) {
@@ -467,7 +482,7 @@ struct GenericOffsetTo : OffsetType
   {
     unsigned int offset = *this;
     if (HB_UNLIKELY (!offset)) return Null(Type);
-    return CONST_CAST(Type, *CharP(base), offset);
+    return StructAtOffset<Type> (*CharP(base), offset);
   }
 
   inline bool sanitize (SANITIZE_ARG_DEF, void *base) {
@@ -475,21 +490,21 @@ struct GenericOffsetTo : OffsetType
     if (!SANITIZE_SELF ()) return false;
     unsigned int offset = *this;
     if (HB_UNLIKELY (!offset)) return true;
-    return SANITIZE (CAST(Type, *CharP(base), offset)) || NEUTER (*this, 0);
+    return SANITIZE (StructAtOffset<Type> (*CharP(base), offset)) || NEUTER (*this, 0);
   }
   inline bool sanitize (SANITIZE_ARG_DEF, void *base, void *base2) {
     TRACE_SANITIZE ();
     if (!SANITIZE_SELF ()) return false;
     unsigned int offset = *this;
     if (HB_UNLIKELY (!offset)) return true;
-    return SANITIZE_BASE (CAST(Type, *CharP(base), offset), base2) || NEUTER (*this, 0);
+    return SANITIZE_BASE (StructAtOffset<Type> (*CharP(base), offset), base2) || NEUTER (*this, 0);
   }
   inline bool sanitize (SANITIZE_ARG_DEF, void *base, unsigned int user_data) {
     TRACE_SANITIZE ();
     if (!SANITIZE_SELF ()) return false;
     unsigned int offset = *this;
     if (HB_UNLIKELY (!offset)) return true;
-    return SANITIZE_BASE (CAST(Type, *CharP(base), offset), user_data) || NEUTER (*this, 0);
+    return SANITIZE_BASE (StructAtOffset<Type> (*CharP(base), offset), user_data) || NEUTER (*this, 0);
   }
 };
 template <typename Base, typename OffsetType, typename Type>
index 75dcd4c..c8b2b33 100644 (file)
@@ -591,7 +591,7 @@ struct PairPosFormat1
        buffer->in_pos = j;
        return true;
       }
-      record = &CONST_CAST (PairValueRecord, *record, record_size);
+      record = &StructAtOffset<PairValueRecord> (*record, record_size);
     }
 
     return false;
@@ -1361,7 +1361,7 @@ struct ExtensionPos : Extension
   {
     unsigned int offset = get_offset ();
     if (HB_UNLIKELY (!offset)) return Null(PosLookupSubTable);
-    return CONST_CAST (PosLookupSubTable, *this, offset);
+    return StructAtOffset<PosLookupSubTable> (*this, offset);
   }
 
   inline bool apply (APPLY_ARG_DEF) const;
@@ -1445,7 +1445,7 @@ struct PosLookupSubTable
 struct PosLookup : Lookup
 {
   inline const PosLookupSubTable& get_subtable (unsigned int i) const
-  { return this+CONST_CAST (OffsetArrayOf<PosLookupSubTable>, subTable, 0)[i]; }
+  { return this+Cast<OffsetArrayOf<PosLookupSubTable> > (subTable)[i]; }
 
   /* Like get_type(), but looks through extension lookups.
    * Never returns Extension */
@@ -1524,7 +1524,7 @@ struct PosLookup : Lookup
   inline bool sanitize (SANITIZE_ARG_DEF) {
     TRACE_SANITIZE ();
     if (HB_UNLIKELY (!Lookup::sanitize (SANITIZE_ARG))) return false;
-    OffsetArrayOf<PosLookupSubTable> &list = CAST (OffsetArrayOf<PosLookupSubTable>, subTable, 0);
+    OffsetArrayOf<PosLookupSubTable> &list = Cast<OffsetArrayOf<PosLookupSubTable> > (subTable);
     return SANITIZE_THIS (list);
   }
 };
@@ -1541,10 +1541,10 @@ struct GPOS : GSUBGPOS
   static const hb_tag_t Tag    = HB_OT_TAG_GPOS;
 
   static inline const GPOS& get_for_data (const char *data)
-  { return CONST_CAST(GPOS, GSUBGPOS::get_for_data (data), 0); }
+  { return Cast<GPOS> (GSUBGPOS::get_for_data (data)); }
 
   inline const PosLookup& get_lookup (unsigned int i) const
-  { return CONST_CAST(PosLookup, GSUBGPOS::get_lookup (i), 0); }
+  { return Cast<PosLookup> (GSUBGPOS::get_lookup (i)); }
 
   inline bool position_lookup (hb_ot_layout_context_t *context,
                               hb_buffer_t  *buffer,
@@ -1555,7 +1555,7 @@ struct GPOS : GSUBGPOS
   inline bool sanitize (SANITIZE_ARG_DEF) {
     TRACE_SANITIZE ();
     if (HB_UNLIKELY (!GSUBGPOS::sanitize (SANITIZE_ARG))) return false;
-    OffsetTo<PosLookupList> &list = CAST(OffsetTo<PosLookupList>, lookupList, 0);
+    OffsetTo<PosLookupList> &list = Cast<OffsetTo<PosLookupList> > (lookupList);
     return SANITIZE_THIS (list);
   }
 };
@@ -1583,7 +1583,7 @@ inline bool ExtensionPos::sanitize (SANITIZE_ARG_DEF)
 
   unsigned int offset = get_offset ();
   if (HB_UNLIKELY (!offset)) return true;
-  return SANITIZE (CAST (PosLookupSubTable, *this, offset));
+  return SANITIZE (StructAtOffset<PosLookupSubTable> (*this, offset));
 }
 
 static inline bool position_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
index 23150b6..fd44575 100644 (file)
@@ -566,7 +566,7 @@ struct ExtensionSubst : Extension
   {
     unsigned int offset = get_offset ();
     if (HB_UNLIKELY (!offset)) return Null(SubstLookupSubTable);
-    return CONST_CAST (SubstLookupSubTable, *this, offset);
+    return StructAtOffset<SubstLookupSubTable> (*this, offset);
   }
 
   inline bool apply (APPLY_ARG_DEF) const;
@@ -740,7 +740,7 @@ struct SubstLookupSubTable
 struct SubstLookup : Lookup
 {
   inline const SubstLookupSubTable& get_subtable (unsigned int i) const
-  { return this+CONST_CAST (OffsetArrayOf<SubstLookupSubTable>, subTable, 0)[i]; }
+  { return this+Cast<OffsetArrayOf<SubstLookupSubTable> > (subTable)[i]; }
 
   /* Like get_type(), but looks through extension lookups.
    * Never returns Extension */
@@ -833,7 +833,7 @@ struct SubstLookup : Lookup
   inline bool sanitize (SANITIZE_ARG_DEF) {
     TRACE_SANITIZE ();
     if (HB_UNLIKELY (!Lookup::sanitize (SANITIZE_ARG))) return false;
-    OffsetArrayOf<SubstLookupSubTable> &list = CAST (OffsetArrayOf<SubstLookupSubTable>, subTable, 0);
+    OffsetArrayOf<SubstLookupSubTable> &list = Cast<OffsetArrayOf<SubstLookupSubTable> > (subTable);
     return SANITIZE_THIS (list);
   }
 };
@@ -850,10 +850,10 @@ struct GSUB : GSUBGPOS
   static const hb_tag_t Tag    = HB_OT_TAG_GSUB;
 
   static inline const GSUB& get_for_data (const char *data)
-  { return CONST_CAST(GSUB, GSUBGPOS::get_for_data (data), 0); }
+  { return Cast<GSUB> (GSUBGPOS::get_for_data (data)); }
 
   inline const SubstLookup& get_lookup (unsigned int i) const
-  { return CONST_CAST(SubstLookup, GSUBGPOS::get_lookup (i), 0); }
+  { return Cast<SubstLookup> (GSUBGPOS::get_lookup (i)); }
 
   inline bool substitute_lookup (hb_ot_layout_context_t *context,
                                 hb_buffer_t  *buffer,
@@ -865,7 +865,7 @@ struct GSUB : GSUBGPOS
   inline bool sanitize (SANITIZE_ARG_DEF) {
     TRACE_SANITIZE ();
     if (HB_UNLIKELY (!GSUBGPOS::sanitize (SANITIZE_ARG))) return false;
-    OffsetTo<SubstLookupList> &list = CAST(OffsetTo<SubstLookupList>, lookupList, 0);
+    OffsetTo<SubstLookupList> &list = Cast<OffsetTo<SubstLookupList> > (lookupList);
     return SANITIZE_THIS (list);
   }
 };
@@ -893,7 +893,7 @@ inline bool ExtensionSubst::sanitize (SANITIZE_ARG_DEF)
 
   unsigned int offset = get_offset ();
   if (HB_UNLIKELY (!offset)) return true;
-  return SANITIZE (CAST (SubstLookupSubTable, *this, offset));
+  return SANITIZE (StructAtOffset<SubstLookupSubTable> (*this, offset));
 }
 
 static inline bool substitute_lookup (APPLY_ARG_DEF, unsigned int lookup_index)
index 3e4adff..aceb3b6 100644 (file)
@@ -299,10 +299,10 @@ struct Rule
   inline bool apply (APPLY_ARG_DEF, ContextLookupContext &lookup_context) const
   {
     TRACE_APPLY ();
-    const LookupRecord *lookupRecord = &CONST_CAST (LookupRecord, input, input[0].get_size () * (inputCount ? inputCount - 1 : 0));
+    const LookupRecord &lookupRecord = StructAtOffset<LookupRecord> (input, input[0].get_size () * (inputCount ? inputCount - 1 : 0));
     return context_lookup (APPLY_ARG,
                           inputCount, input,
-                          lookupCount, lookupRecord,
+                          lookupCount, &lookupRecord,
                           lookup_context);
   }
 
@@ -448,14 +448,14 @@ struct ContextFormat3
     if (HB_LIKELY (index == NOT_COVERED))
       return false;
 
-    const LookupRecord *lookupRecord = &CONST_CAST(LookupRecord, coverage, coverage[0].get_size () * glyphCount);
+    const LookupRecord &lookupRecord = StructAtOffset<LookupRecord> (coverage, coverage[0].get_size () * glyphCount);
     struct ContextLookupContext lookup_context = {
       {match_coverage, apply_func},
        CharP(this)
     };
     return context_lookup (APPLY_ARG,
                           glyphCount, (const USHORT *) (coverage + 1),
-                          lookupCount, lookupRecord,
+                          lookupCount, &lookupRecord,
                           lookup_context);
   }
 
@@ -466,8 +466,8 @@ struct ContextFormat3
     if (!SANITIZE_ARRAY (coverage, OffsetTo<Coverage>::get_size (), glyphCount)) return false;
     for (unsigned int i = 0; i < count; i++)
       if (!SANITIZE_THIS (coverage[i])) return false;
-    LookupRecord *lookupRecord = &CAST(LookupRecord, coverage, OffsetTo<Coverage>::get_size () * glyphCount);
-    return SANITIZE_ARRAY (lookupRecord, LookupRecord::get_size (), lookupCount);
+    LookupRecord &lookupRecord = StructAtOffset<LookupRecord> (coverage, OffsetTo<Coverage>::get_size () * glyphCount);
+    return SANITIZE_ARRAY (&lookupRecord, LookupRecord::get_size (), lookupCount);
   }
 
   private: