Skip to content

Instantly share code, notes, and snippets.

@zwaldowski
Created March 26, 2014 19:07
Show Gist options
  • Save zwaldowski/9790870 to your computer and use it in GitHub Desktop.
Save zwaldowski/9790870 to your computer and use it in GitHub Desktop.
//
// ZWMacros.h
// Aieeeee
//
// Created by Zach Waldowski on 3/26/14.
// Copyright (c) 2014 Big Nerd Ranch. All rights reserved.
//
#ifdef __OBJC__
#import <Foundation/Foundation.h>
#ifndef __has_attribute
#define __has_attribute(x) 0
#endif
#ifndef __has_feature
#define __has_feature(x) 0
#endif
/// Use for declaring a macro-style function in a header
#if !defined(ZW_ALWAYS_INLINE)
# if __has_attribute(always_inline) || defined(__GNUC__)
# define ZW_ALWAYS_INLINE static __inline__ __attribute__((always_inline))
# elif defined(__MWERKS__) || defined(__cplusplus)
# define ZW_ALWAYS_INLINE static inline
# elif defined(_MSC_VER)
# define ZW_ALWAYS_INLINE static __inline
# elif TARGET_OS_WIN32
# define ZW_ALWAYS_INLINE static __inline__
# endif
#endif
///-------------------------
/// @name Generic exceptions
///-------------------------
ZW_ALWAYS_INLINE NS_FORMAT_FUNCTION(3, 4) NSException *_ZWException(NSString *name, NSString *prefix, NSString *format, ...) {
va_list args; va_start(args, format);
NSString *suffix = [[NSString alloc] initWithFormat:format arguments:args];
NSString *string = prefix ? [prefix stringByAppendingString:suffix] : suffix;
NSException *ret = [NSException exceptionWithName:name reason:string userInfo:nil];
va_end(args);
return ret;
}
#define ZWException(name, format...) _ZWException(name, nil, ## format)
ZW_ALWAYS_INLINE NSString *_ZWTaggedExceptionPrefix(id obj, SEL selector) {
return [NSString stringWithFormat:@"-[%@ %@]: ", NSStringFromClass([obj class]), NSStringFromSelector(selector)];
}
// Yields exception with description "-[MyClass methodName:]: exception format"
#define ZWTaggedException(name, format...) _ZWException(name, _ZWTaggedExceptionPrefix(self, _cmd), ## format)
///--------------------------
/// @name Specific exceptions
///--------------------------
extern NSString *const ZWBadInit;
extern NSString *const ZWAbstractClass;
extern NSString *const ZWAbstractMethod;
/// "-[MyClass init]: Bad init. Use -initWithFoo: instead."
#define ZWBadInitException(initializer) _ZWException(ZWBadInit, _ZWTaggedExceptionPrefix(self, _cmd), @"Bad init. Use %@ instead.", NSStringFromSelector(initializer))
/// "-[MyAbstractClass tableView:numberOfRowsInSection:]: Abstract method. Reimplement it on MyConcreteSubclass."
#define ZWAbstractClassException(abstractClass) _ZWException(ZWAbstractClass, _ZWTaggedExceptionPrefix(abstractClass, _cmd), @"Abstract method. Reimplement it on %@.", NSStringFromClass([self class]))
/// "-[MyClassCluster setContentViewController:animated:]: Abstract method."
#define ZWAbstractMethodException() ZWTaggedException(ZWAbstractMethod, @"Abstract method.")
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment