//
//  Swift-Backport.h
//  GlyphsKit
//
//  Created by Florian Pircher on 26.03.25.
//  Copyright © 2025 schriftgestaltung.de. All rights reserved.
//

#ifndef Swift_Backport_h
#define Swift_Backport_h

#define _gs_defer_block_name_with_prefix(prefix, suffix) prefix ## suffix
#define _gs_defer_block_name(suffix) _gs_defer_block_name_with_prefix(_gs_defer_, suffix)
/// `gs_defer` behaves similar to Swift’s `defer` where a block is provided that is then executed when the current scope is exited.
///
/// ## Examples
///
/// ```
/// [someObject beginContext];
/// gs_defer ^{
///     [someObject endContext];
/// };
///
/// // code executed between `beginContext` and `endContext`
/// ```
///
/// In the code above, `[someObject endContext]` is called at the end of the scope, at an early exit using `return`, or when an exception is thrown.
#define gs_defer __strong void(^_gs_defer_block_name(__LINE__))(void) __attribute__((cleanup(_gs_defer_cleanup_block), unused)) =
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
static void _gs_defer_cleanup_block(__strong void(^*block)(void)) {
	(*block)();
}
#pragma clang diagnostic pop

#endif /* Swift_Backport_h */
