From b2d69aacf445740828ee907bd84c138f309d9517 Mon Sep 17 00:00:00 2001 From: shassani Date: Mon, 2 Jul 2018 09:34:18 -0700 Subject: [PATCH] Helper function to get empty string on nullptr (#4800) Adds helper function to get empty string when String is nullptr. This is to get over the fact that flat buffer builders will record null when data is not present. --- include/flatbuffers/flatbuffers.h | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 8b350cd90..4e2008f9f 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -335,7 +335,7 @@ const Vector> *VectorCast(const Vector> *ptr) { #endif // Convenient helper function to get the length of any vector, regardless -// of wether it is null or not (the field is not set). +// of whether it is null or not (the field is not set). template static inline size_t VectorLength(const Vector *v) { return v ? v->Length() : 0; } @@ -357,6 +357,18 @@ struct String : public Vector { } }; +// Convenience function to get std::string from a String returning an empty +// string on null pointer. +static inline std::string GetString(const String * str) { + return str ? str->str() : ""; +} + +// Convenience function to get char* from a String returning an empty string on +// null pointer. +static inline const char * GetCstring(const String * str) { + return str ? str->c_str() : ""; +} + // Allocator interface. This is flatbuffers-specific and meant only for // `vector_downward` usage. class Allocator {