[C++] Change Vector3D fields from double to float, to have alignment eq. 4 (to support MSVC 2010), plus minor review fix

This commit is contained in:
Alexey Geraskin 2019-08-01 16:40:35 +03:00
parent 7f275324c1
commit 04c16df8f7
5 changed files with 30 additions and 53 deletions

View File

@ -3,9 +3,9 @@ native_include "native_type_test_impl.h";
namespace Geometry;
struct Vector3D (native_type:"Native::Vector3D") {
x:double;
y:double;
z:double;
x:float;
y:float;
z:float;
}
table ApplicationData {

View File

@ -19,11 +19,11 @@ inline const flatbuffers::TypeTable *Vector3DTypeTable();
inline const flatbuffers::TypeTable *ApplicationDataTypeTable();
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vector3D FLATBUFFERS_FINAL_CLASS {
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Vector3D FLATBUFFERS_FINAL_CLASS {
private:
double x_;
double y_;
double z_;
float x_;
float y_;
float z_;
public:
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
@ -32,31 +32,31 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vector3D FLATBUFFERS_FINAL_CLASS {
Vector3D() {
memset(static_cast<void *>(this), 0, sizeof(Vector3D));
}
Vector3D(double _x, double _y, double _z)
Vector3D(float _x, float _y, float _z)
: x_(flatbuffers::EndianScalar(_x)),
y_(flatbuffers::EndianScalar(_y)),
z_(flatbuffers::EndianScalar(_z)) {
}
double x() const {
float x() const {
return flatbuffers::EndianScalar(x_);
}
void mutate_x(double _x) {
void mutate_x(float _x) {
flatbuffers::WriteScalar(&x_, _x);
}
double y() const {
float y() const {
return flatbuffers::EndianScalar(y_);
}
void mutate_y(double _y) {
void mutate_y(float _y) {
flatbuffers::WriteScalar(&y_, _y);
}
double z() const {
float z() const {
return flatbuffers::EndianScalar(z_);
}
void mutate_z(double _z) {
void mutate_z(float _z) {
flatbuffers::WriteScalar(&z_, _z);
}
};
FLATBUFFERS_STRUCT_END(Vector3D, 24);
FLATBUFFERS_STRUCT_END(Vector3D, 12);
struct ApplicationDataT : public flatbuffers::NativeTable {
typedef ApplicationData TableType;
@ -155,11 +155,11 @@ inline flatbuffers::Offset<ApplicationData> CreateApplicationData(flatbuffers::F
inline const flatbuffers::TypeTable *Vector3DTypeTable() {
static const flatbuffers::TypeCode type_codes[] = {
{ flatbuffers::ET_DOUBLE, 0, -1 },
{ flatbuffers::ET_DOUBLE, 0, -1 },
{ flatbuffers::ET_DOUBLE, 0, -1 }
{ flatbuffers::ET_FLOAT, 0, -1 },
{ flatbuffers::ET_FLOAT, 0, -1 },
{ flatbuffers::ET_FLOAT, 0, -1 }
};
static const int64_t values[] = { 0, 8, 16, 24 };
static const int64_t values[] = { 0, 4, 8, 12 };
static const char * const names[] = {
"x",
"y",

View File

@ -1,15 +1,4 @@
#include "native_type_test_impl.h"
// looks like VS10 does not support std::vector with explicit alignment
// From stackoverflow (https://stackoverflow.com/questions/8456236/how-is-a-vectors-data-aligned):
//
// Visual C++ version 2010 will not work with an std::vector with classes whose alignment are specified.
// The reason is std::vector::resize.
// When compiling, next error appears:
// c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector(870): error C2719:
// '_Val': formal parameter with __declspec(align('8')) won't be aligned [C:\projects\flatbuffers\flattests.vcxproj]
#if !defined(_MSC_VER) || _MSC_VER >= 1700
#include "native_type_test_generated.h"
namespace flatbuffers {
@ -22,4 +11,3 @@ namespace flatbuffers {
}
}
#endif

View File

@ -3,12 +3,12 @@
namespace Native {
struct Vector3D {
double x;
double y;
double z;
float x;
float y;
float z;
Vector3D() { x = 0; y = 0; z = 0; };
Vector3D(double x, double y, double z) { this->x = x; this->y = y; this->z = z; }
Vector3D(float x, float y, float z) { this->x = x; this->y = y; this->z = z; }
};
}

View File

@ -2844,37 +2844,26 @@ void FixedLengthArrayTest() {
}
void NativeTypeTest() {
// looks like VS10 does not support std::vector with explicit alignment
// From stackoverflow (https://stackoverflow.com/questions/8456236/how-is-a-vectors-data-aligned):
//
// Visual C++ version 2010 will not work with an std::vector with classes whose alignment are specified.
// The reason is std::vector::resize.
// When compiling, next error appears:
// c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\vector(870): error C2719:
// '_Val': formal parameter with __declspec(align('8')) won't be aligned [C:\projects\flatbuffers\flattests.vcxproj]
#if !defined(_MSC_VER) || _MSC_VER >= 1700
const int N = 3;
Geometry::ApplicationDataT srcDataT;
srcDataT.vectors.reserve(N);
Geometry::ApplicationDataT src_data;
src_data.vectors.reserve(N);
for (int i = 0; i < N; ++i) {
srcDataT.vectors.push_back (Native::Vector3D(10 * i + 0.1, 10 * i + 0.2, 10 * i + 0.3));
src_data.vectors.push_back (Native::Vector3D(10 * i + 0.1f, 10 * i + 0.2f, 10 * i + 0.3f));
}
flatbuffers::FlatBufferBuilder fbb;
fbb.Finish(Geometry::ApplicationData::Pack(fbb, &srcDataT));
fbb.Finish(Geometry::ApplicationData::Pack(fbb, &src_data));
auto dstDataT = Geometry::UnPackApplicationData(fbb.GetBufferPointer());
for (int i = 0; i < N; ++i) {
Native::Vector3D& v = dstDataT->vectors[i];
TEST_EQ(v.x, 10 * i + 0.1);
TEST_EQ(v.y, 10 * i + 0.2);
TEST_EQ(v.z, 10 * i + 0.3);
TEST_EQ(v.x, 10 * i + 0.1f);
TEST_EQ(v.y, 10 * i + 0.2f);
TEST_EQ(v.z, 10 * i + 0.3f);
}
#endif
}
void FixedLengthArrayJsonTest(bool binary) {