Skip to content

Commit

Permalink
fix: rename nullptr to lpl_nullptr and update related macros in singl…
Browse files Browse the repository at this point in the history
…eton files
  • Loading branch information
MasterLaplace committed Nov 8, 2024
1 parent 29665ea commit c21c08f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ int main()
```c
#define SINGLETON_NO_THREAD_SAFETY
#define SINGLETON_IMPLEMENTATION
#define SINGLETON_H_IMPLEMENTATION
#include "Singleton/singleton.h"
typedef struct {
Expand Down
4 changes: 2 additions & 2 deletions Singleton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class Singleton {
/**
* @brief Pointer to the Singleton instance.
*/
static SINGLETON_CPP17(inline) T *_instance SINGLETON_CPP17(= nullptr);
static SINGLETON_CPP17(inline) T *_instance SINGLETON_CPP17(= lpl_nullptr);

#ifndef SINGLETON_NO_THREAD_SAFETY
/**
Expand Down Expand Up @@ -229,7 +229,7 @@ inline void Singleton<T>::DestroyInstance()
SINGLETON_THREAD_SAFE(std::lock_guard<std::mutex> lock(_mutex));

delete _instance;
_instance = nullptr;
_instance = lpl_nullptr;
}

#endif /* !SINGLETON_CPP_ONCE */
Expand Down
10 changes: 5 additions & 5 deletions singleton.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ extern inline void instance_destroy();
////////////////////////////////////////////////////////////////////////////////
/* IMPLEMENTATION */
////////////////////////////////////////////////////////////////////////////////
#ifdef SINGLETON_IMPLEMENTATION
#ifdef SINGLETON_H_IMPLEMENTATION
#ifndef SINGLETON_C_ONCE
#define SINGLETON_C_ONCE

static singleton_t *instance = nullptr;
static singleton_t *instance = lpl_nullptr;

#ifndef SINGLETON_NO_THREAD_SAFETY
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
Expand All @@ -133,7 +133,7 @@ inline void instance_create(void *(*create)(va_list args), void (*destroy)(void
if (!instance->data)
{
free(instance);
instance = NULL;
instance = lpl_nullptr;
}

SINGLETON_THREAD_SAFE(pthread_mutex_unlock(&mutex));
Expand Down Expand Up @@ -163,11 +163,11 @@ inline void instance_destroy()

instance->destroy(instance->data);
free(instance);
instance = NULL;
instance = lpl_nullptr;

SINGLETON_THREAD_SAFE(pthread_mutex_unlock(&mutex));
}

#endif /* !SINGLETON_C_ONCE */
#endif /* SINGLETON_IMPLEMENTATION */
#endif /* SINGLETON_H_IMPLEMENTATION */
// clang-format on
60 changes: 30 additions & 30 deletions singleton_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,32 +37,32 @@
#endif


#ifndef CONFIG_UTILS
#define CONFIG_UTILS
#ifndef LAPLACE_CONFIG_UTILS
#define LAPLACE_CONFIG_UTILS

////////////////////////////////////////////////////////////
// Define shared portable macros for various compilers
////////////////////////////////////////////////////////////
#define NEED_COMMA struct _
#define ATTRIBUTE(key) __attribute__((key))
#define UNUSED_ATTRIBUTE ATTRIBUTE(unused)
#define UNUSED(x) (void)(x)
#define LPL_NEED_COMMA struct _
#define LPL_ATTRIBUTE(key) __attribute__((key))
#define LPL_UNUSED_ATTRIBUTE LPL_ATTRIBUTE(unused)
#define LPL_UNUSED(x) (void)(x)

////////////////////////////////////////////////////////////
// Define portable NULL pointer using C++11 nullptr keyword
////////////////////////////////////////////////////////////
#if defined(__cplusplus) && __cplusplus >= 201103L
#define lpl_nullptr nullptr
#elif !defined(NULL)
#define nullptr ((void*)0)
#define lpl_nullptr ((void*)0)
#else
#define nullptr NULL
#define lpl_nullptr NULL
#endif

////////////////////////////////////////////////////////////
// Define boolean type and values
////////////////////////////////////////////////////////////
#if defined(__cplusplus)
#elif !defined(__bool_true_false_are_defined)
#if !defined(__bool_true_false_are_defined) && !defined(__cplusplus)
#define bool _Bool
#define true 1
#define false 0
Expand All @@ -72,7 +72,7 @@
#if defined(__GNUC__) && defined(__GNUC_MINOR__)
# define __GNUC_PREREQ(maj, min) \
((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
#else
#elif !defined(__GNUC_PREREQ)
# define __GNUC_PREREQ(maj, min) 0
#endif

Expand All @@ -81,34 +81,34 @@
////////////////////////////////////////////////////////////
/** Usage:
* @example
* PACKED(struct MyStruct
* LPL_PACKED(struct MyStruct
* {
* int a;
* char b;
* ...
* });
\**********************************************************/
#if defined(__GNUC__) || defined(__GNUG__)
#define PACKED( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#define PACKED_START _Pragma("pack(1)")
#define PACKED_END _Pragma("pack()")
#elif _MSC_VER
#define PACKED( __Declaration__ ) __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop))
#define PACKED_START __pragma(pack(push, 1))
#define PACKED_END __pragma(pack(pop))
#if defined(_MSC_VER) || defined(_MSVC_LANG)
#define LPL_PACKED( __Declaration__ ) __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop))
#define LPL_PACKED_START __pragma(pack(push, 1))
#define LPL_PACKED_END __pragma(pack(pop))
#elif defined(__GNUC__) || defined(__GNUG__)
#define LPL_PACKED( __Declaration__ ) __Declaration__ __attribute__((__packed__))
#define LPL_PACKED_START _Pragma("pack(1)")
#define LPL_PACKED_END _Pragma("pack()")
#else
#define PACKED( __Declaration__ ) __Declaration__
#define PACKED_START _Pragma("pack(1)")
#define PACKED_END _Pragma("pack()")
#define LPL_PACKED( __Declaration__ ) __Declaration__
#define LPL_PACKED_START
#define LPL_PACKED_END
#endif

////////////////////////////////////////////////////////////
// Helper macro to convert a macro to a string
////////////////////////////////////////////////////////////
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define LPL_STRINGIFY(x) #x
#define LPL_TOSTRING(x) LPL_STRINGIFY(x)

#endif /* !CONFIG_UTILS */
#endif /* !LAPLACE_CONFIG_UTILS */


#ifndef SINGLETON_DISTRIBUTION_H_
Expand Down Expand Up @@ -475,10 +475,10 @@
// Define the SINGLETON version string
////////////////////////////////////////////////////////////
#define SINGLETON_VERSION_STRING \
TOSTRING(SINGLETON_VERSION_MAJOR) "." \
TOSTRING(SINGLETON_VERSION_MINOR) "." \
TOSTRING(SINGLETON_VERSION_PATCH) "." \
TOSTRING(SINGLETON_VERSION_TWEAK)
LPL_TOSTRING(SINGLETON_VERSION_MAJOR) "." \
LPL_TOSTRING(SINGLETON_VERSION_MINOR) "." \
LPL_TOSTRING(SINGLETON_VERSION_PATCH) "." \
LPL_TOSTRING(SINGLETON_VERSION_TWEAK)

#endif /* !SINGLETON_VERSION_H_ */

Expand Down

0 comments on commit c21c08f

Please sign in to comment.