Skip to content

Commit c48a4a2

Browse files
committed
macro based element definitions
1 parent 8ed2275 commit c48a4a2

File tree

2 files changed

+112
-93
lines changed

2 files changed

+112
-93
lines changed

src/include/fastply/fastply_macros.h

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2019 David B. Adrian
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
#pragma once
21+
22+
#include <tuple>
23+
24+
#define FASTPLY_GENERATE_OPERATORS(name, args...) \
25+
\
26+
auto tie_internals_() const { return std::tie(args); } \
27+
\
28+
bool operator<(const name& rhs) const { \
29+
return tie_internals_() < rhs.tie_internals_(); \
30+
} \
31+
\
32+
bool operator>(const name& rhs) const { \
33+
return tie_internals_() > rhs.tie_internals_(); \
34+
} \
35+
\
36+
bool operator<=(const name& rhs) const { \
37+
return tie_internals_() <= rhs.tie_internals_(); \
38+
} \
39+
\
40+
bool operator>=(const name& rhs) const { \
41+
return tie_internals_() >= rhs.tie_internals_(); \
42+
} \
43+
\
44+
bool operator==(const name& rhs) const { \
45+
return tie_internals_() == rhs.tie_internals_(); \
46+
} \
47+
\
48+
bool operator!=(const name& rhs) const { \
49+
return tie_internals_() != rhs.tie_internals_(); \
50+
}
51+
52+
#if defined(_MSC_VER)
53+
#define FASTPLY_PACKED_(__FP_ELEMENT__) \
54+
__pragma(pack(push, 1)) __FP_ELEMENT__ __pragma(pack(pop))
55+
#elif defined(__GNUC__)
56+
#define FASTPLY_PACKED_(__FP_ELEMENT__) \
57+
__FP_ELEMENT__ __attribute__((__packed__))
58+
#endif
59+
60+
#define FASTPLY_ELEMENT(name, ...) FASTPLY_PACKED_(struct name{__VA_ARGS__});

test/DataLayout.h

+52-93
Original file line numberDiff line numberDiff line change
@@ -2,109 +2,68 @@
22
* Automatically generated. Please verify that types *
33
* used have the correct size on your platform. *
44
*****************************************************/
5+
#include "fastply/fastply_macros.h"
6+
57
#include <cstdint>
68
#include <cstring>
79

8-
struct __attribute__ ((packed)) Vertex {
9-
const float x;
10-
const float y;
11-
const float z;
12-
const float nx;
13-
const float ny;
14-
const float nz;
15-
const uint8_t red;
16-
const uint8_t green;
17-
const uint8_t blue;
10+
FASTPLY_ELEMENT(Vertex,
11+
const float x;
12+
const float y;
13+
const float z;
14+
const float nx;
15+
const float ny;
16+
const float nz;
17+
const uint8_t red;
18+
const uint8_t green;
19+
const uint8_t blue;
1820

19-
bool operator==(const Vertex& rhs) const {
20-
return x == rhs.x &&
21-
y == rhs.y &&
22-
z == rhs.z &&
23-
nx == rhs.nx &&
24-
ny == rhs.ny &&
25-
nz == rhs.nz &&
26-
red == rhs.red &&
27-
green == rhs.green &&
28-
blue == rhs.blue;
29-
}
30-
};
21+
FASTPLY_GENERATE_OPERATORS(Vertex, x, y, z, nx, ny, nz, red, green, blue)
22+
)
3123

32-
struct __attribute__ ((packed)) Camera {
33-
const float view_px;
34-
const float view_py;
35-
const float view_pz;
36-
const float x_axisx;
37-
const float x_axisy;
38-
const float x_axisz;
39-
const float y_axisx;
40-
const float y_axisy;
41-
const float y_axisz;
42-
const float z_axisx;
43-
const float z_axisy;
44-
const float z_axisz;
45-
const float focal;
46-
const float scalex;
47-
const float scaley;
48-
const float centerx;
49-
const float centery;
50-
const int32_t viewportx;
51-
const int32_t viewporty;
52-
const float k1;
53-
const float k2;
24+
FASTPLY_ELEMENT(Camera,
25+
const float view_px;
26+
const float view_py;
27+
const float view_pz;
28+
const float x_axisx;
29+
const float x_axisy;
30+
const float x_axisz;
31+
const float y_axisx;
32+
const float y_axisy;
33+
const float y_axisz;
34+
const float z_axisx;
35+
const float z_axisy;
36+
const float z_axisz;
37+
const float focal;
38+
const float scalex;
39+
const float scaley;
40+
const float centerx;
41+
const float centery;
42+
const int32_t viewportx;
43+
const int32_t viewporty;
44+
const float k1;
45+
const float k2;
5446

55-
bool operator==(const Camera& rhs) const {
56-
return view_px == rhs.view_px &&
57-
view_py == rhs.view_py &&
58-
view_pz == rhs.view_pz &&
59-
x_axisx == rhs.x_axisx &&
60-
x_axisy == rhs.x_axisy &&
61-
x_axisz == rhs.x_axisz &&
62-
y_axisx == rhs.y_axisx &&
63-
y_axisy == rhs.y_axisy &&
64-
y_axisz == rhs.y_axisz &&
65-
z_axisx == rhs.z_axisx &&
66-
z_axisy == rhs.z_axisy &&
67-
z_axisz == rhs.z_axisz &&
68-
focal == rhs.focal &&
69-
scalex == rhs.scalex &&
70-
scaley == rhs.scaley &&
71-
centerx == rhs.centerx &&
72-
centery == rhs.centery &&
73-
viewportx == rhs.viewportx &&
74-
viewporty == rhs.viewporty &&
75-
k1 == rhs.k1 &&
76-
k2 == rhs.k2;
77-
}
78-
};
47+
FASTPLY_GENERATE_OPERATORS(Camera, view_px, view_py, view_pz, x_axisx, x_axisy, x_axisz, y_axisx, y_axisy, y_axisz, z_axisx, z_axisy, z_axisz, focal, scalex, scaley, centerx, centery)
48+
)
7949

80-
struct __attribute__ ((packed)) Alltypes {
81-
const int8_t c;
82-
const uint8_t uc;
83-
const int16_t s;
84-
const uint16_t us;
85-
const int32_t i;
86-
const uint32_t ui;
87-
const float f;
88-
const double d;
50+
FASTPLY_ELEMENT(Alltypes,
51+
const int8_t c;
52+
const uint8_t uc;
53+
const int16_t s;
54+
const uint16_t us;
55+
const int32_t i;
56+
const uint32_t ui;
57+
const float f;
58+
const double d;
8959

90-
bool operator==(const Alltypes& rhs) const {
91-
return c == rhs.c &&
92-
uc == rhs.uc &&
93-
s == rhs.s &&
94-
us == rhs.us &&
95-
i == rhs.i &&
96-
ui == rhs.ui &&
97-
f == rhs.f &&
98-
d == rhs.d;
99-
}
100-
};
60+
FASTPLY_GENERATE_OPERATORS(Alltypes, c, uc, s, us, i, ui, f, d)
61+
)
10162

102-
struct __attribute__ ((packed)) Face {
63+
FASTPLY_ELEMENT(Face,
10364
const uint8_t vertex_index_length = 4;
10465
const int32_t vertex_index[4];
10566

106-
bool operator==(const Face& rhs) const {
107-
return !std::memcmp(&vertex_index, &rhs.vertex_index, sizeof(int32_t)*4);
108-
}
109-
};
67+
FASTPLY_GENERATE_OPERATORS(Face, vertex_index[0], vertex_index[1], vertex_index[2], vertex_index[3])
68+
)
11069

0 commit comments

Comments
 (0)