Skip to content

Commit

Permalink
core: use nullptr in alist.h and dlist.h
Browse files Browse the repository at this point in the history
Also fix a compiler warning in dlist.h so pragmas can be reduced
  • Loading branch information
arogge committed May 25, 2020
1 parent 46d3187 commit b0257af
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 31 deletions.
39 changes: 20 additions & 19 deletions core/src/lib/alist.h
Expand Up @@ -44,39 +44,40 @@
for ((var) = list ? (typeof((var)))(list)->first() : 0; (var); \
(var) = (typeof(var))(list)->next())

#define foreach_alist_null(var, list) \
for ((var) = list ? (typeof((var)))(list)->first() : NULL; (var); \
#define foreach_alist_null(var, list) \
for ((var) = list ? (typeof((var)))(list)->first() : nullptr; (var); \
(var) = (typeof(var))(list)->next())

#define foreach_alist_index(inx, var, list) \
for ((inx) = 0; \
(list != NULL) ? ((var) = (typeof((var)))(list)->get((inx))) : 0; \
#define foreach_alist_index(inx, var, list) \
for ((inx) = 0; \
(list != nullptr) ? ((var) = (typeof((var)))(list)->get((inx))) : 0; \
(inx)++)

#define foreach_alist_rindex(inx, var, list) \
for ((list != NULL) ? (inx) = ((list)->size() - 1) : 0; \
(list != NULL) ? ((var) = (typeof((var)))(list)->get((inx))) : 0; \
#define foreach_alist_rindex(inx, var, list) \
for ((list != nullptr) ? (inx) = ((list)->size() - 1) : 0; \
(list != nullptr) ? ((var) = (typeof((var)))(list)->get((inx))) : 0; \
(inx)--)

#else
#define foreach_alist(var, list) \
for ((void)(list ? (*((void**)&(var)) = (void*)((list)->first())) : 0); \
(var); (*((void**)&(var)) = (void*)((list)->next())))

#define foreach_alist_null(var, list) \
for ((void)(list ? (*((void**)&(var)) = (void*)((list)->first())) : NULL); \
#define foreach_alist_null(var, list) \
for ((void)(list ? (*((void**)&(var)) = (void*)((list)->first())) \
: nullptr); \
(var); (*((void**)&(var)) = (void*)((list)->next())))

#define foreach_alist_index(inx, var, list) \
for ((inx) = 0; \
(list != NULL) ? ((*((void**)&(var)) = (void*)((list)->get((inx))))) \
: 0; \
#define foreach_alist_index(inx, var, list) \
for ((inx) = 0; \
(list != nullptr) ? ((*((void**)&(var)) = (void*)((list)->get((inx))))) \
: 0; \
(inx)++)

#define foreach_alist_rindex(inx, var, list) \
for ((list != NULL) ? (inx) = ((list)->size() - 1) : 0; \
(list != NULL) ? ((*((void**)&(var)) = (void*)((list)->get((inx))))) \
: 0; \
#define foreach_alist_rindex(inx, var, list) \
for ((list != nullptr) ? (inx) = ((list)->size() - 1) : 0; \
(list != nullptr) ? ((*((void**)&(var)) = (void*)((list)->get((inx))))) \
: 0; \
(inx)--)
#endif

Expand Down Expand Up @@ -133,7 +134,7 @@ class alist {
*/
inline void* alist::operator[](int index) const
{
if (index < 0 || index >= num_items) { return NULL; }
if (index < 0 || index >= num_items) { return nullptr; }
return items[index];
}

Expand Down
20 changes: 8 additions & 12 deletions core/src/lib/dlist.h
Expand Up @@ -49,18 +49,14 @@
* Loop var through each member of list
*/
#ifdef HAVE_TYPEOF
#define foreach_dlist(var, list) \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wconversion-null\"") for ( \
(var) = NULL; list ? ((var) = (typeof(var))(list)->next(var)) \
: NULL;) _Pragma("GCC diagnostic pop")
#define foreach_dlist(var, list) \
for ((var) = nullptr; (list ? ((var) = (typeof(var))(list)->next(var)) \
: nullptr) != nullptr;)
#else
#define foreach_dlist(var, list) \
_Pragma("GCC diagnostic push") \
_Pragma("GCC diagnostic ignored \"-Wconversion-null\"") for ( \
(var) = NULL; \
list ? (*((void**)&(var)) = (void*)((list)->next(var))) : NULL;) \
_Pragma("GCC diagnostic pop")
#define foreach_dlist(var, list) \
for ((var) = nullptr; \
(list ? (*((void**)&(var)) = (void*)((list)->next(var))) : nullptr) != \
nullptr;)
#endif

class dlist {
Expand Down Expand Up @@ -149,7 +145,7 @@ inline dlink* dlist::get_link(void* item)
}


inline bool dlist::empty() const { return head == NULL; }
inline bool dlist::empty() const { return head == nullptr; }

inline int dlist::size() const { return num_items; }

Expand Down

0 comments on commit b0257af

Please sign in to comment.