diff options
author | gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3> | 2010-01-08 04:36:12 +0000 |
---|---|---|
committer | gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3> | 2010-01-08 04:36:12 +0000 |
commit | b2fa9805eb63d5daa1dd8fab1edf0c85fb7ebfc0 (patch) | |
tree | a07aae86ed120807b8f2d5f621e575f4257115c8 /DebugUtils | |
parent | 31ce8143b3739153e88bd6847e1fbaf7633f6233 (diff) |
[Author: dmaclach]
Added some basic casting debugging goodness to GTM.
Opinions? added to help catch a bug in QSB.
R=thomasvl
DELTA=55 (55 added, 0 deleted, 0 changed)
Diffstat (limited to 'DebugUtils')
-rw-r--r-- | DebugUtils/GTMTypeCasting.h | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/DebugUtils/GTMTypeCasting.h b/DebugUtils/GTMTypeCasting.h new file mode 100644 index 0000000..1e9ebb9 --- /dev/null +++ b/DebugUtils/GTMTypeCasting.h @@ -0,0 +1,81 @@ +// +// GTMTypeCasting.h +// +// Copyright 2010 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +// + +#import <Foundation/Foundation.h> +#import "GTMDefines.h" + +#ifdef __cplusplus +extern "C" { +#endif + +// These are some basic macros for making down-casting safer in Objective C. +// They are loosely based on the same cast types with similar names in C++. +// A typical usage would look like this: +// +// Bar* b = [[Bar alloc] init]; +// Foo* a = GTM_STATIC_CAST(Foo, b); +// +// Note that it's GTM_STATIC_CAST(Foo, b) and not GTM_STATIC_CAST(Foo*, b). +// +// GTM_STATIC_CAST runs only in debug mode, and will assert if and only if: +// - object is non nil +// - [object isKindOfClass:[cls class]] returns nil +// +// otherwise it returns object. +// +// GTM_DYNAMIC_CAST runs in both debug and release and will return nil if +// - object is nil +// - [object isKindOfClass:[cls class]] returns nil +// +// otherwise it returns object. +// + +// Support functions for dealing with casting. +GTM_INLINE id GTMDynamicCastSupport(Class cls, id object) { + id value = nil; + if (object) { + _GTMDevAssert(cls, @"Nil Class"); + if ([object isKindOfClass:cls]) { + value = object; + } + } + return value; +} + +GTM_INLINE id GTMStaticCastSupport(Class cls, id object) { + id value = nil; + if (object) { + value = GTMDynamicCastSupport(cls, object); + _GTMDevAssert(value, @"Could not cast %@ to class %@", object, cls); + } + return value; +} + +#ifndef GTM_STATIC_CAST + #ifdef DEBUG + #define GTM_STATIC_CAST(type, object) GTMStaticCastSupport([type class], \ + object) + #else + #define GTM_STATIC_CAST(type, object) ((type *) (object)) + #endif +#endif + +#ifndef GTM_DYNAMIC_CAST + #define GTM_DYNAMIC_CAST(type, object) GTMDynamicCastSupport([type class], \ + object) +#endif |