mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-24 07:51:48 +00:00
[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:
@@ -3,9 +3,9 @@ native_include "native_type_test_impl.h";
|
|||||||
namespace Geometry;
|
namespace Geometry;
|
||||||
|
|
||||||
struct Vector3D (native_type:"Native::Vector3D") {
|
struct Vector3D (native_type:"Native::Vector3D") {
|
||||||
x:double;
|
x:float;
|
||||||
y:double;
|
y:float;
|
||||||
z:double;
|
z:float;
|
||||||
}
|
}
|
||||||
|
|
||||||
table ApplicationData {
|
table ApplicationData {
|
||||||
|
|||||||
@@ -19,11 +19,11 @@ inline const flatbuffers::TypeTable *Vector3DTypeTable();
|
|||||||
|
|
||||||
inline const flatbuffers::TypeTable *ApplicationDataTypeTable();
|
inline const flatbuffers::TypeTable *ApplicationDataTypeTable();
|
||||||
|
|
||||||
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vector3D FLATBUFFERS_FINAL_CLASS {
|
FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(4) Vector3D FLATBUFFERS_FINAL_CLASS {
|
||||||
private:
|
private:
|
||||||
double x_;
|
float x_;
|
||||||
double y_;
|
float y_;
|
||||||
double z_;
|
float z_;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
|
static const flatbuffers::TypeTable *MiniReflectTypeTable() {
|
||||||
@@ -32,31 +32,31 @@ FLATBUFFERS_MANUALLY_ALIGNED_STRUCT(8) Vector3D FLATBUFFERS_FINAL_CLASS {
|
|||||||
Vector3D() {
|
Vector3D() {
|
||||||
memset(static_cast<void *>(this), 0, sizeof(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)),
|
: x_(flatbuffers::EndianScalar(_x)),
|
||||||
y_(flatbuffers::EndianScalar(_y)),
|
y_(flatbuffers::EndianScalar(_y)),
|
||||||
z_(flatbuffers::EndianScalar(_z)) {
|
z_(flatbuffers::EndianScalar(_z)) {
|
||||||
}
|
}
|
||||||
double x() const {
|
float x() const {
|
||||||
return flatbuffers::EndianScalar(x_);
|
return flatbuffers::EndianScalar(x_);
|
||||||
}
|
}
|
||||||
void mutate_x(double _x) {
|
void mutate_x(float _x) {
|
||||||
flatbuffers::WriteScalar(&x_, _x);
|
flatbuffers::WriteScalar(&x_, _x);
|
||||||
}
|
}
|
||||||
double y() const {
|
float y() const {
|
||||||
return flatbuffers::EndianScalar(y_);
|
return flatbuffers::EndianScalar(y_);
|
||||||
}
|
}
|
||||||
void mutate_y(double _y) {
|
void mutate_y(float _y) {
|
||||||
flatbuffers::WriteScalar(&y_, _y);
|
flatbuffers::WriteScalar(&y_, _y);
|
||||||
}
|
}
|
||||||
double z() const {
|
float z() const {
|
||||||
return flatbuffers::EndianScalar(z_);
|
return flatbuffers::EndianScalar(z_);
|
||||||
}
|
}
|
||||||
void mutate_z(double _z) {
|
void mutate_z(float _z) {
|
||||||
flatbuffers::WriteScalar(&z_, _z);
|
flatbuffers::WriteScalar(&z_, _z);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
FLATBUFFERS_STRUCT_END(Vector3D, 24);
|
FLATBUFFERS_STRUCT_END(Vector3D, 12);
|
||||||
|
|
||||||
struct ApplicationDataT : public flatbuffers::NativeTable {
|
struct ApplicationDataT : public flatbuffers::NativeTable {
|
||||||
typedef ApplicationData TableType;
|
typedef ApplicationData TableType;
|
||||||
@@ -155,11 +155,11 @@ inline flatbuffers::Offset<ApplicationData> CreateApplicationData(flatbuffers::F
|
|||||||
|
|
||||||
inline const flatbuffers::TypeTable *Vector3DTypeTable() {
|
inline const flatbuffers::TypeTable *Vector3DTypeTable() {
|
||||||
static const flatbuffers::TypeCode type_codes[] = {
|
static const flatbuffers::TypeCode type_codes[] = {
|
||||||
{ flatbuffers::ET_DOUBLE, 0, -1 },
|
{ flatbuffers::ET_FLOAT, 0, -1 },
|
||||||
{ flatbuffers::ET_DOUBLE, 0, -1 },
|
{ flatbuffers::ET_FLOAT, 0, -1 },
|
||||||
{ flatbuffers::ET_DOUBLE, 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[] = {
|
static const char * const names[] = {
|
||||||
"x",
|
"x",
|
||||||
"y",
|
"y",
|
||||||
|
|||||||
@@ -1,15 +1,4 @@
|
|||||||
#include "native_type_test_impl.h"
|
#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"
|
#include "native_type_test_generated.h"
|
||||||
|
|
||||||
namespace flatbuffers {
|
namespace flatbuffers {
|
||||||
@@ -22,4 +11,3 @@ namespace flatbuffers {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|||||||
@@ -3,12 +3,12 @@
|
|||||||
|
|
||||||
namespace Native {
|
namespace Native {
|
||||||
struct Vector3D {
|
struct Vector3D {
|
||||||
double x;
|
float x;
|
||||||
double y;
|
float y;
|
||||||
double z;
|
float z;
|
||||||
|
|
||||||
Vector3D() { x = 0; y = 0; z = 0; };
|
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; }
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2844,37 +2844,26 @@ void FixedLengthArrayTest() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void NativeTypeTest() {
|
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;
|
const int N = 3;
|
||||||
|
|
||||||
Geometry::ApplicationDataT srcDataT;
|
Geometry::ApplicationDataT src_data;
|
||||||
srcDataT.vectors.reserve(N);
|
src_data.vectors.reserve(N);
|
||||||
|
|
||||||
for (int i = 0; i < N; ++i) {
|
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;
|
flatbuffers::FlatBufferBuilder fbb;
|
||||||
fbb.Finish(Geometry::ApplicationData::Pack(fbb, &srcDataT));
|
fbb.Finish(Geometry::ApplicationData::Pack(fbb, &src_data));
|
||||||
|
|
||||||
auto dstDataT = Geometry::UnPackApplicationData(fbb.GetBufferPointer());
|
auto dstDataT = Geometry::UnPackApplicationData(fbb.GetBufferPointer());
|
||||||
|
|
||||||
for (int i = 0; i < N; ++i) {
|
for (int i = 0; i < N; ++i) {
|
||||||
Native::Vector3D& v = dstDataT->vectors[i];
|
Native::Vector3D& v = dstDataT->vectors[i];
|
||||||
TEST_EQ(v.x, 10 * i + 0.1);
|
TEST_EQ(v.x, 10 * i + 0.1f);
|
||||||
TEST_EQ(v.y, 10 * i + 0.2);
|
TEST_EQ(v.y, 10 * i + 0.2f);
|
||||||
TEST_EQ(v.z, 10 * i + 0.3);
|
TEST_EQ(v.z, 10 * i + 0.3f);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FixedLengthArrayJsonTest(bool binary) {
|
void FixedLengthArrayJsonTest(bool binary) {
|
||||||
|
|||||||
Reference in New Issue
Block a user