Skip to content

Instantly share code, notes, and snippets.

@a2
Created October 15, 2011 00:11
Show Gist options
  • Save a2/1288743 to your computer and use it in GitHub Desktop.
Save a2/1288743 to your computer and use it in GitHub Desktop.
A2Common
//
// A2Casting.h
//
// Created by Alexsander Akers on 10/6/11.
// Copyright (c) 2011 Pandamonia LLC. All rights reserved.
//
// 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 = A2_STATIC_CAST(Foo, b);
//
// Note that it's A2_STATIC_CAST(Foo, b) and not A2_STATIC_CAST(Foo*, b).
//
// A2_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.
//
// A2_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.
//
NS_INLINE id A2DynamicCastSupport(Class cls, id object)
{
NSCAssert(cls, @"Nil class");
return [object isKindOfClass: cls] ? object : nil;
}
NS_INLINE id A2StaticCastSupport(Class cls, id object)
{
id value = nil;
if (object)
{
value = A2DynamicCastSupport(cls, object);
NSCAssert2(value, @"Could not cast %@ to class %@", object, NSStringFromClass(cls));
}
return value;
}
#ifndef A2_STATIC_CAST
#ifdef DEBUG
#define A2_STATIC_CAST(type, object) ((type *) A2StaticCastSupport([type class], object))
#else
#define A2_STATIC_CAST(type, object) ((type *) (object))
#endif
#endif
#ifndef A2_DYNAMIC_CAST
#define A2_DYNAMIC_CAST(type, object) A2DynamicCastSupport([type class], object)
#endif
//
// A2Common.h
//
// Created by Alexsander Akers on 9/4/11.
// Copyright (c) 2011 Pandamonia LLC. All rights reserved.
//
#define A2Log(format, ...) NSLog(@"%s <line %d> " format, __PRETTY_FUNCTION__, __LINE__, ## __VA_ARGS__)
// NSLog(@"%s:%d %s " format, strrchr("/" __FILE__, '/') + 1, __LINE__, __PRETTY_FUNCTION__, ## __VA_ARGS__)
#define A2LogError(error) A2Log(@"Error = %@", (error))
#define A2LogException(exception) A2Log(@"Exception = %@", (exception))
#ifdef __IPHONE_OS_VERSION_MIN_REQUIRED
#define A2DependsDevice(iphone, ipad) (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone ? (iphone) : (ipad))
#define A2DependsOrient(io, port, land) (UIInterfaceOrientationIsPortrait(io) ? (port) : (land))
#define A2DependsBoth(io, iphonePort, iphoneLand, ipadPort, ipadLand) (A2DependsOrient(io, A2DependsDevice((iphonePort), (ipadPort)), A2DependsDevice((iphoneLand), (ipadLand))))
#define NSLogAffineTransform(xform) NSLog(@#xform @" = %@", NSStringFromCGAffineTransform(xform))
#define NSLogEdgeInsets(insets) NSLog(@#insets @" = %@", NSStringFromUIEdgeInsets(insets))
#define NSLogPoint(point) NSLog(@#point @" = %@", NSStringFromCGPoint(point))
#define NSLogRect(rect) NSLog(@#rect @" = %@", NSStringFromCGRect(rect))
#define NSLogSize(size) NSLog(@#size @" = %@", NSStringFromCGSize(size))
#elif defined(__MAC_OS_X_VERSION_MIN_REQUIRED)
#define NSLogPoint(point) NSLog(@#point @" = %@", NSStringFromPoint(point))
#define NSLogRect(rect) NSLog(@#rect @" = %@", NSStringFromRect(rect))
#define NSLogSize(size) NSLog(@#size @" = %@", NSStringFromSize(size))
#endif
#define NSTrue ((__bridge id) kCFBooleanTrue)
#define NSFalse ((__bridge id) kCFBooleanFalse)
#define NSBool(x) ((x) ? NSTrue : NSFalse)
#define NSLogBool(val) NSLog(@#val @" = %s", (val) ? "YES" : "NO")
#define NSLogBoolValue(val) NSLog(@#val @" = %s", ([val boolValue]) ? "YES" : "NO")
#define NSLogChar(val) NSLogChar8(val)
#define NSLogChar8(val) NSLog(@#val @" = %c", (val))
#define NSLogChar16(val) NSLog(@#val @" = %C", (val))
#define NSLogCharCode(val) { int c = (val); NSLog(@#val @" = %c%c%c%c", (c >> 24) & 0xFF, (c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF); }
#define NSLogClass(cls) NSLog(@#cls @" = %@", NSStringFromClass(cls))
#define NSLogCString(val) NSLogCString8(val)
#define NSLogCString8(val) NSLog(@#val @" = %S", (cString))
#define NSLogCString16(val) NSLog(@#val @" = %S", (cString))
#define NSLogFloat(val) NSLog(@#val @" = %f", (val))
#define NSLogInteger(val) NSLog(@#val @" = %qi", (long long) val)
#define NSLogIntegerHex(val) NSLog(@#val @" = %qX", (long long) val)
#define NSLogObj(obj) NSLog(@#obj @" = %@", (obj))
#define NSLogObjAndClass(obj) NSLog(@#obj @" (%@) = %@", NSStringFromClass([obj class]), obj)
#define NSLogPointer(ptr) NSLog(@#ptr @" --> %p", (ptr))
#define NSLogProtocol(protocol) NSLog(@#protocol @" = %@", NSStringFromProtocol(protocol))
#define NSLogRange(range) NSLog(@#range @" = %@", NSStringFromRange(range))
#define NSLogSelector(sel) NSLog(@#sel @" = %@", NSStringFromSelector(sel))
#define NSLogUInteger(uint) NSLog(@#uint @" = %qu", (unsigned long long) (uint))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment