From 5da4b3d122805d14818b3d07562b299cc7cf3c8a Mon Sep 17 00:00:00 2001 From: Matthew Chen Date: Tue, 24 Jan 2017 14:02:57 -0500 Subject: [PATCH] Add assert macro that can be used in free functions. // FREEBIE --- src/Util/Asserts.h | 13 ++++++++++--- src/Util/OWSDispatch.h | 2 ++ src/Util/OWSDispatch.m | 4 ++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Util/Asserts.h b/src/Util/Asserts.h index 6b94d56ac..d0cfbc728 100755 --- a/src/Util/Asserts.h +++ b/src/Util/Asserts.h @@ -1,7 +1,5 @@ // -// Asserts.h -// -// Copyright (c) 2016 Open Whisper Systems. All rights reserved. +// Copyright (c) 2017 Open Whisper Systems. All rights reserved. // #import @@ -21,9 +19,18 @@ NSLog(@"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \ NSAssert(0, @"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \ } +// OWSAssert() should be used in Obj-C and Swift methods. +// OWSCAssert() should be used in free functions. +#define OWSCAssert(X) \ +if (!(X)) { \ +NSLog(@"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \ +NSCAssert(0, @"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \ +} + #else #define OWSAssert(X) +#define OWSCAssert(X) #endif diff --git a/src/Util/OWSDispatch.h b/src/Util/OWSDispatch.h index 8d6b42985..7da5da39f 100644 --- a/src/Util/OWSDispatch.h +++ b/src/Util/OWSDispatch.h @@ -24,4 +24,6 @@ NS_ASSUME_NONNULL_BEGIN @end +void AssertIsOnMainThread(); + NS_ASSUME_NONNULL_END diff --git a/src/Util/OWSDispatch.m b/src/Util/OWSDispatch.m index 6d3353100..89a4aef96 100644 --- a/src/Util/OWSDispatch.m +++ b/src/Util/OWSDispatch.m @@ -40,4 +40,8 @@ NS_ASSUME_NONNULL_BEGIN @end +void AssertIsOnMainThread() { + OWSCAssert([NSThread isMainThread]); +} + NS_ASSUME_NONNULL_END