Skip to content

Commit

Permalink
Reworked reflection.h to be more general.
Browse files Browse the repository at this point in the history
e.g. support generic reading/writing from structs/vectors etc.

Change-Id: I2eb6e24db088a72da444d5c8df7e506e53d5bc2d
Tested: on Linux.
Bug: 22660837
  • Loading branch information
Wouter van Oortmerssen committed Aug 3, 2015
1 parent 0e064e4 commit 7101224
Show file tree
Hide file tree
Showing 6 changed files with 733 additions and 439 deletions.
10 changes: 3 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ set(FlatBuffers_Library_SRCS
include/flatbuffers/reflection_generated.h
src/idl_parser.cpp
src/idl_gen_text.cpp
src/reflection.cpp
)

set(FlatBuffers_Compiler_SRCS
Expand All @@ -43,14 +44,9 @@ set(FlatHash_SRCS
)

set(FlatBuffers_Tests_SRCS
include/flatbuffers/flatbuffers.h
include/flatbuffers/hash.h
include/flatbuffers/idl.h
include/flatbuffers/util.h
src/idl_parser.cpp
src/idl_gen_general.cpp
src/idl_gen_text.cpp
${FlatBuffers_Library_SRCS}
src/idl_gen_fbs.cpp
src/idl_gen_general.cpp
tests/test.cpp
# file generate by running compiler on tests/monster_test.fbs
${CMAKE_CURRENT_BINARY_DIR}/tests/monster_test_generated.h
Expand Down
4 changes: 3 additions & 1 deletion android/jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ LOCAL_SRC_FILES := main.cpp \
../../tests/test.cpp \
../../src/idl_parser.cpp \
../../src/idl_gen_text.cpp \
../../src/idl_gen_fbs.cpp
../../src/idl_gen_fbs.cpp \
../../src/idl_gen_general.cpp \
../../src/reflection.cpp
LOCAL_LDLIBS := -llog -landroid
LOCAL_STATIC_LIBRARIES := android_native_app_glue flatbuffers
LOCAL_ARM_MODE := arm
Expand Down
30 changes: 29 additions & 1 deletion include/flatbuffers/flatbuffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,24 @@ template<typename T> class Vector {
}
};

// Represent a vector much like the template above, but in this case we
// don't know what the element types are (used with reflection.h).
class VectorOfAny {
public:
uoffset_t size() const { return EndianScalar(length_); }

const uint8_t *Data() const {
return reinterpret_cast<const uint8_t *>(&length_ + 1);
}
uint8_t *Data() {
return reinterpret_cast<uint8_t *>(&length_ + 1);
}
protected:
VectorOfAny();

uoffset_t length_;
};

// Convenient helper function to get the length of any vector, regardless
// of wether it is null or not (the field is not set).
template<typename T> static inline size_t VectorLength(const Vector<T> *v) {
Expand Down Expand Up @@ -995,6 +1013,9 @@ class Struct FLATBUFFERS_FINAL_CLASS {
return reinterpret_cast<T>(&data_[o]);
}

const uint8_t *GetAddressOf(uoffset_t o) const { return &data_[o]; }
uint8_t *GetAddressOf(uoffset_t o) { return &data_[o]; }

private:
uint8_t data_[1];
};
Expand Down Expand Up @@ -1027,7 +1048,6 @@ class Table {
? reinterpret_cast<P>(p + ReadScalar<uoffset_t>(p))
: nullptr;
}

template<typename P> P GetPointer(voffset_t field) const {
return const_cast<Table *>(this)->GetPointer<P>(field);
}
Expand All @@ -1052,6 +1072,14 @@ class Table {
return true;
}

uint8_t *GetAddressOf(voffset_t field) {
auto field_offset = GetOptionalFieldOffset(field);
return field_offset ? data_ + field_offset : nullptr;
}
const uint8_t *GetAddressOf(voffset_t field) const {
return const_cast<Table *>(this)->GetAddressOf(field);
}

uint8_t *GetVTable() { return data_ - ReadScalar<soffset_t>(data_); }

bool CheckField(voffset_t field) const {
Expand Down
Loading

0 comments on commit 7101224

Please sign in to comment.