From 89041a16862ec702db63c312e8f0f7e88ed82c4e Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 27 Mar 2017 17:46:34 -0700 Subject: [PATCH] Add a VectorCast function that safely casts from Vector> to Vector> if U is a base class of T. This is useful for when you want to generically iterate over a vector of objects that derive from flatbuffers::Table. Change-Id: I59161e3b9f40501f72e02b46509be9dc8ab86c6b --- include/flatbuffers/flatbuffers.h | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 7b79891c4..140e176aa 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -483,6 +483,20 @@ private: VectorOfAny(const VectorOfAny&); }; +#ifndef FLATBUFFERS_CPP98_STL +template +Vector> *VectorCast(Vector> *ptr) { + static_assert(std::is_base_of::value, "Unrelated types"); + return reinterpret_cast> *>(ptr); +} + +template +const Vector> *VectorCast(const Vector> *ptr) { + static_assert(std::is_base_of::value, "Unrelated types"); + return reinterpret_cast> *>(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). template static inline size_t VectorLength(const Vector *v) {