From 17faa144160c2091d910bd2092985af66f87692d Mon Sep 17 00:00:00 2001 From: Perttu Ahola Date: Tue, 28 Oct 2014 14:50:00 +0200 Subject: [PATCH] core/types.h: T* check(T *v) --- doc/conventions.txt | 3 +++ src/core/types.h | 16 ++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/doc/conventions.txt b/doc/conventions.txt index acd7d0c..f7374c3 100644 --- a/doc/conventions.txt +++ b/doc/conventions.txt @@ -69,6 +69,9 @@ Non-exception throwing and exception-throwing methods - find_x: Returns nullptr or equivalent if not found - check_x: Throws exception if not found +To check for nullptr returned by get_x() or find_x(), use the check() function +in core/types.h. + Directory structure ------------------- ├── 3rdparty << Bundled 3rd-party libraries diff --git a/src/core/types.h b/src/core/types.h index 5282c2a..b0c0112 100644 --- a/src/core/types.h +++ b/src/core/types.h @@ -146,4 +146,20 @@ template static inline cc_* cs(const T &v){ return dump(v).c_str(); } + +// check() + +template +static inline T* check(T *v){ + if(v == nullptr) + throw Exception("check(): nullptr"); + return v; +} +template +static inline const T* check(const T *v){ + if(v == nullptr) + throw Exception("check(): nullptr"); + return v; +} + // vim: set noet ts=4 sw=4: