Commit b2d69aac authored by shassani's avatar shassani Committed by Wouter van Oortmerssen

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.
parent 3331805a
...@@ -335,7 +335,7 @@ const Vector<Offset<T>> *VectorCast(const Vector<Offset<U>> *ptr) { ...@@ -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> { ...@@ -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 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment