From d7ac3788e81724d92d29364d719769a034f87829 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Wed, 10 May 2017 14:55:11 -0700 Subject: [PATCH] Clarified the semantics of the Offset type. Change-Id: Iccc36d24321ac4d556692ac715c0cc69a2c9e09e Tested: on Linux. --- include/flatbuffers/flatbuffers.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index c42781619..198f7efc8 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -158,11 +158,13 @@ typedef std::unique_ptr> #endif // Wrapper for uoffset_t to allow safe template specialization. +// Value is allowed to be 0 to indicate a null object (see e.g. AddOffset). template struct Offset { uoffset_t o; Offset() : o(0) {} Offset(uoffset_t _o) : o(_o) {} Offset Union() const { return Offset(o); } + bool IsNull() const { return !o; } }; inline void EndianCheck() { @@ -855,7 +857,7 @@ FLATBUFFERS_FINAL_CLASS } template void AddOffset(voffset_t field, Offset off) { - if (!off.o) return; // An offset of 0 means NULL, don't store. + if (off.IsNull()) return; // Don't store. AddElement(field, ReferTo(off.o), static_cast(0)); }