Skip to content

Commit

Permalink
Add workaround for Objective-C compatibility
Browse files Browse the repository at this point in the history
Protect against Objective C's `#define Nil nullptr` by temporarily
suppressing the Nil define and providing an alias for Nil ("Nill") to
support calling from Objective C contexts.

See #1
  • Loading branch information
mqudsi committed Mar 11, 2023
1 parent fb822e5 commit 22ca1db
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/uuidxx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
#define _CRT_SECURE_NO_WARNINGS
#endif

// ObjC compatibility issue
#undef Nil

#include "uuidxx.h"
#include <random>
#include <stdio.h>
Expand Down
8 changes: 8 additions & 0 deletions src/uuidxx.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

// ObjC compatibility
#pragma push_macro("Nil")
#undef Nil

#include <stdexcept>
#include <cstdint>
#include <string>
Expand All @@ -9,6 +13,7 @@ namespace uuidxx
enum class Variant
{
Nil,
Nill, // for ObjC compatibility
Version1,
Version2,
Version3,
Expand Down Expand Up @@ -61,6 +66,7 @@ namespace uuidxx
switch (v)
{
case Variant::Nil:
case Variant::Nill:
return uuid(nullptr); // Special-cased
case Variant::Version1:
case Variant::Version2:
Expand All @@ -79,3 +85,5 @@ namespace uuidxx

static_assert(sizeof(uuid) == 2 * sizeof(int64_t), "Check uuid type declaration/padding!");
}

#pragma pop_macro("Nil")

0 comments on commit 22ca1db

Please sign in to comment.