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.
This commit is contained in:
shassani 2018-07-02 09:34:18 -07:00 committed by Wouter van Oortmerssen
parent 3331805a1c
commit b2d69aacf4
1 changed files with 13 additions and 1 deletions

View File

@ -335,7 +335,7 @@ const Vector<Offset<T>> *VectorCast(const Vector<Offset<U>> *ptr) {
#endif #endif
// Convenient helper function to get the length of any vector, regardless // 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<typename T> static inline size_t VectorLength(const Vector<T> *v) { template<typename T> static inline size_t VectorLength(const Vector<T> *v) {
return v ? v->Length() : 0; return v ? v->Length() : 0;
} }
@ -357,6 +357,18 @@ struct String : public Vector<char> {
} }
}; };
// 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 // Allocator interface. This is flatbuffers-specific and meant only for
// `vector_downward` usage. // `vector_downward` usage.
class Allocator { class Allocator {