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 {