Index: core/cross/types.h |
=================================================================== |
--- core/cross/types.h (revision 19090) |
+++ core/cross/types.h (working copy) |
@@ -105,6 +105,30 @@ |
// We can add more sophisticated per-platform #defines as necessary here |
#define IS_LITTLE_ENDIAN 1 |
+// When you upcast (that is, cast a pointer from type Foo to type |
+// SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts |
+// always succeed. When you downcast (that is, cast a pointer from |
+// type Foo to type SubclassOfFoo), static_cast<> isn't safe, because |
+// how do you know the pointer is really of type SubclassOfFoo? It |
+// could be a bare Foo, or of type DifferentSubclassOfFoo. Thus, when |
+// you downcast, you should use this macro. We used to do a |
+// dynamic_cast there in debug mode just to make sure it was the right |
+// type, but now that RTTI is completely turned off, we just do the |
+// implicit_cast compile-time check. |
+ |
+template<typename To, typename From> // use like this: down_cast<T*>(foo); |
+inline To down_cast(From* f) { // so we only accept pointers |
+ // Ensures that To is a sub-type of From *. This test is here only |
+ // for compile-time type checking, and has no overhead in an |
+ // optimized build at run-time, as it will be optimized away |
+ // completely. |
+ if (false) { |
+ implicit_cast<From*, To>(0); |
+ } |
+ |
+ return static_cast<To>(f); |
+} |
+ |
namespace o3d { |
// String |