add template macro to "safely" perform casts w/o breaking strict-aliasing
fix aliasing warnings
git-svn-id: http://skia.googlecode.com/svn/trunk@674 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/core/SkTypes.h b/include/core/SkTypes.h
index 85f5421..17ed4c1 100644
--- a/include/core/SkTypes.h
+++ b/include/core/SkTypes.h
@@ -301,6 +301,20 @@
return cond ? bits | mask : bits & ~mask;
}
+///////////////////////////////////////////////////////////////////////////////
+
+/**
+ * Use to cast a pointer to a different type, and maintaining strict-aliasing
+ */
+template <typename Dst> Dst SkTCast(const void* ptr) {
+ union {
+ const void* src;
+ Dst dst;
+ } data;
+ data.src = ptr;
+ return data.dst;
+}
+
//////////////////////////////////////////////////////////////////////////////
/** \class SkNoncopyable
@@ -311,7 +325,7 @@
class SkNoncopyable {
public:
SkNoncopyable() {}
-
+
private:
SkNoncopyable(const SkNoncopyable&);
SkNoncopyable& operator=(const SkNoncopyable&);
@@ -322,7 +336,7 @@
SkAutoFree() : fPtr(NULL) {}
explicit SkAutoFree(void* ptr) : fPtr(ptr) {}
~SkAutoFree() { sk_free(fPtr); }
-
+
/** Return the currently allocate buffer, or null
*/
void* get() const { return fPtr; }
@@ -336,7 +350,7 @@
fPtr = ptr;
return prev;
}
-
+
/** Transfer ownership of the current ptr to the caller, setting the
internal reference to null. Note the caller is reponsible for calling
sk_free on the returned address.