Skip to content

Commit dd4a6a5

Browse files
committed
Rename user-define structure annotation macros for better clarity.
NOP_MEMBERS -> NOP_STRUCTURE NOP_STRUCTURE -> NOP_EXTERNAL_STRUCTURE NOP_TEMPLATE -> NOP_EXTERNAL_TEMPLATE
1 parent ced64ba commit dd4a6a5

10 files changed

+87
-78
lines changed

examples/interface.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class PhoneNumber {
115115
std::string number_;
116116
Type type_{Type::Other};
117117

118-
NOP_MEMBERS(PhoneNumber, number_, type_);
118+
NOP_STRUCTURE(PhoneNumber, number_, type_);
119119
};
120120

121121
// A simple customer record with basic identity and contact information,
@@ -166,8 +166,8 @@ class Customer {
166166
std::string address_;
167167
std::vector<PhoneNumber> phone_numbers_;
168168

169-
NOP_MEMBERS(Customer, last_name_, first_name_, middle_name_, address_,
170-
phone_numbers_);
169+
NOP_STRUCTURE(Customer, last_name_, first_name_, middle_name_, address_,
170+
phone_numbers_);
171171
};
172172

173173
// Type for unique customer ids.

examples/pipe.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
#include <utility>
2222

2323
#include <nop/serializer.h>
24-
#include <nop/structure.h>
2524
#include <nop/status.h>
25+
#include <nop/structure.h>
2626
#include <nop/types/file_handle.h>
2727
#include <nop/types/result.h>
2828
#include <nop/utility/die.h>
@@ -71,7 +71,7 @@ struct Channel {
7171
// Defines the format of request messages sent from parent to child.
7272
struct Request {
7373
std::uint32_t request_bytes{0};
74-
NOP_MEMBERS(Request, request_bytes);
74+
NOP_STRUCTURE(Request, request_bytes);
7575
};
7676

7777
// Defines the errors that may be returned from child to parent.
@@ -176,7 +176,8 @@ int HandleParent(Channel channel) {
176176
std::cout << "Parent sending message..." << std::endl;
177177

178178
const std::uint32_t kRequestBytes = 32;
179-
channel.Write(Request{kRequestBytes}) || Die("Parent failed to write request");
179+
channel.Write(Request{kRequestBytes}) ||
180+
Die("Parent failed to write request");
180181

181182
Response response;
182183
channel.Read(&response) || Die("Parent failed to read response");

examples/simple_protocol.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ struct Header {
8080

8181
// Make this header type serializable by describing it to the serialization
8282
// engine..
83-
NOP_MEMBERS(Header, magic, version_major, version_minor);
83+
NOP_STRUCTURE(Header, magic, version_major, version_minor);
8484
};
8585

8686
// Protocol type for the header of the message.

examples/stream.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ using nop::StringToHex;
4141
//
4242

4343
// Here we describe struct tm from the standard library, which is code we don't
44-
// own and can't change to include an annotation. The NOP_STRUCTURE() macro
45-
// annotates types we don't own so that the serializer can understand how to
46-
// work with them. The annotation must be in the same namespace as the structure
47-
// is originally defined in.
48-
// NOTE: struct tm is orignally defined in the global namespace, even though the
49-
// header <ctime> also aliases it into the std namespace. The annotation must be
50-
// defined in the ORIGINAL namespace of the annotated type because the compiler
51-
// only considers the original namespace during name lookup.
52-
NOP_STRUCTURE(tm, tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year, tm_wday,
53-
tm_yday, tm_isdst);
44+
// own and can't change to include an annotation. The NOP_EXTERNAL_STRUCTURE()
45+
// macro annotates types we don't own so that the serializer can understand how
46+
// to work with them. The annotation must be in the same namespace as the
47+
// structure is originally defined in. NOTE: struct tm is orignally defined in
48+
// the global namespace, even though the header <ctime> also aliases it into the
49+
// std namespace. The annotation must be defined in the ORIGINAL namespace of
50+
// the annotated type because the compiler only considers the original namespace
51+
// during name lookup.
52+
NOP_EXTERNAL_STRUCTURE(tm, tm_sec, tm_min, tm_hour, tm_mday, tm_mon, tm_year,
53+
tm_wday, tm_yday, tm_isdst);
5454

5555
namespace {
5656

@@ -76,7 +76,7 @@ class UserDefinedA {
7676
std::string a_;
7777
std::vector<int> b_;
7878

79-
NOP_MEMBERS(UserDefinedA, a_, b_);
79+
NOP_STRUCTURE(UserDefinedA, a_, b_);
8080
};
8181

8282
// All enum and enum class types are serializable.
@@ -97,7 +97,7 @@ struct UserDefinedB {
9797
EnumA f;
9898
Optional<std::string> g;
9999

100-
NOP_MEMBERS(UserDefinedB, a, b, c, d, e, f, g);
100+
NOP_STRUCTURE(UserDefinedB, a, b, c, d, e, f, g);
101101
};
102102

103103
// Prints a struct tm to the given stream.

examples/variant.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ struct MessageA {
5050
std::uint32_t a;
5151
std::string b;
5252
std::vector<short> c;
53-
NOP_MEMBERS(MessageA, a, b, c);
53+
NOP_STRUCTURE(MessageA, a, b, c);
5454
};
5555

5656
// Second user-defined type.
5757
struct MessageB {
5858
std::uint64_t x;
5959
std::vector<int> y;
6060
std::pair<std::string, std::string> z;
61-
NOP_MEMBERS(MessageB, x, y, z);
61+
NOP_STRUCTURE(MessageB, x, y, z);
6262
};
6363

6464
// Utility to print MessageA to the debug stream.

include/nop/structure.h

+41-33
Original file line numberDiff line numberDiff line change
@@ -22,40 +22,15 @@
2222

2323
namespace nop {
2424

25-
// Generates a pair of template arguments (member pointer type and value) to be
26-
// passed to MemberPointer<MemberPointerType, MemberPointerValue, ...> from the
27-
// given type name and member name.
28-
#define _NOP_MEMBER_POINTER(type, member) decltype(&type::member), &type::member
29-
30-
// Generates a MemberPointer type definition, given a type name and a variable
31-
// number of member names. The first member name is handled here, while the
32-
// remaining member names are passed to _NOP_MEMBER_POINTER_NEXT for recursive
33-
// expansion.
34-
#define _NOP_MEMBER_POINTER_FIRST(type, ...) \
35-
::nop::MemberPointer<_NOP_MEMBER_POINTER(type, _NOP_FIRST_ARG(__VA_ARGS__)) \
36-
_NOP_MEMBER_POINTER_NEXT( \
37-
type, _NOP_REST_ARG(__VA_ARGS__))>
38-
39-
// Recursively handles the remaining member names in the template argument list
40-
// for MemberPointer.
41-
#define _NOP_MEMBER_POINTER_NEXT(type, ...) \
42-
_NOP_IF_ELSE(_NOP_HAS_ARGS(__VA_ARGS__)) \
43-
(, _NOP_MEMBER_POINTER(type, _NOP_FIRST_ARG(__VA_ARGS__)) \
44-
_NOP_DEFER2(__NOP_MEMBER_POINTER_NEXT)()( \
45-
type, _NOP_REST_ARG(__VA_ARGS__)))(/*done*/)
46-
47-
// Indirection to enable recursive macro expansion of _NOP_MEMBER_POINTER_NEXT.
48-
#define __NOP_MEMBER_POINTER_NEXT() _NOP_MEMBER_POINTER_NEXT
49-
50-
// Defines a list of MemberPointer types given a type and list of member names.
51-
#define NOP_MEMBER_LIST(type, ...) \
52-
NOP_MAP_ARGS(_NOP_MEMBER_POINTER_FIRST, (type), __VA_ARGS__)
25+
//
26+
// User-defined structures are structs or classes that have been annotated so
27+
// that the serialization engine understands how to read and write them.
5328

5429
// Defines the set of members belonging to a type that should be
55-
// serialized/deserialized. This macro should be called once within the
30+
// serialized/deserialized. This macro must be inkoked once within the
5631
// struct/class definition, preferrably in the private section for classes with
5732
// private data.
58-
#define NOP_MEMBERS(type, ... /*members*/) \
33+
#define NOP_STRUCTURE(type, ... /*members*/) \
5934
template <typename, typename> \
6035
friend struct ::nop::Encoding; \
6136
template <typename, typename> \
@@ -67,7 +42,7 @@ namespace nop {
6742
// Defines the set of members belonging to a type that should be
6843
// serialized/deserialized without changing the type itself. This is useful for
6944
// making external library types with public data serializable.
70-
#define NOP_STRUCTURE(type, ... /*members*/) \
45+
#define NOP_EXTERNAL_STRUCTURE(type, ... /*members*/) \
7146
template <typename> \
7247
struct NOP__MEMBER_TRAITS; \
7348
template <> \
@@ -77,8 +52,8 @@ namespace nop {
7752
NOP__MEMBER_TRAITS<type> __attribute__((used)) \
7853
NOP__GetExternalMemberTraits(type*)
7954

80-
// Similar to NOP_STRUCTURE but for template types.
81-
#define NOP_TEMPLATE(type, ... /*members*/) \
55+
// Similar to NOP_EXTERNAL_STRUCTURE but for template types.
56+
#define NOP_EXTERNAL_TEMPLATE(type, ... /*members*/) \
8257
template <typename> \
8358
struct NOP__MEMBER_TRAITS; \
8459
template <typename... Ts> \
@@ -90,6 +65,39 @@ namespace nop {
9065
NOP__MEMBER_TRAITS<type<Ts...>> __attribute__((used)) \
9166
NOP__GetExternalMemberTraits(type<Ts...>*)
9267

68+
//
69+
// Utility macros used by the macros above.
70+
//
71+
72+
// Generates a pair of template arguments (member pointer type and value) to be
73+
// passed to MemberPointer<MemberPointerType, MemberPointerValue, ...> from the
74+
// given type name and member name.
75+
#define _NOP_MEMBER_POINTER(type, member) decltype(&type::member), &type::member
76+
77+
// Generates a MemberPointer type definition, given a type name and a variable
78+
// number of member names. The first member name is handled here, while the
79+
// remaining member names are passed to _NOP_MEMBER_POINTER_NEXT for recursive
80+
// expansion.
81+
#define _NOP_MEMBER_POINTER_FIRST(type, ...) \
82+
::nop::MemberPointer<_NOP_MEMBER_POINTER(type, _NOP_FIRST_ARG(__VA_ARGS__)) \
83+
_NOP_MEMBER_POINTER_NEXT( \
84+
type, _NOP_REST_ARG(__VA_ARGS__))>
85+
86+
// Recursively handles the remaining member names in the template argument list
87+
// for MemberPointer.
88+
#define _NOP_MEMBER_POINTER_NEXT(type, ...) \
89+
_NOP_IF_ELSE(_NOP_HAS_ARGS(__VA_ARGS__)) \
90+
(, _NOP_MEMBER_POINTER(type, _NOP_FIRST_ARG(__VA_ARGS__)) \
91+
_NOP_DEFER2(__NOP_MEMBER_POINTER_NEXT)()( \
92+
type, _NOP_REST_ARG(__VA_ARGS__)))(/*done*/)
93+
94+
// Indirection to enable recursive macro expansion of _NOP_MEMBER_POINTER_NEXT.
95+
#define __NOP_MEMBER_POINTER_NEXT() _NOP_MEMBER_POINTER_NEXT
96+
97+
// Defines a list of MemberPointer types given a type and list of member names.
98+
#define NOP_MEMBER_LIST(type, ...) \
99+
NOP_MAP_ARGS(_NOP_MEMBER_POINTER_FIRST, (type), __VA_ARGS__)
100+
93101
} // namespace nop
94102

95103
#endif // LIBNOP_INCLUDE_NOP_STRUCTURE_H_

test/fungible_tests.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -567,54 +567,54 @@ namespace {
567567
struct UserDefinedA {
568568
std::array<int, 10> a;
569569
std::array<float, 20> b;
570-
NOP_MEMBERS(UserDefinedA, a, b);
570+
NOP_STRUCTURE(UserDefinedA, a, b);
571571
};
572572

573573
struct UserDefinedB1 {
574574
std::array<int, 10> a;
575575
float b[20];
576-
NOP_MEMBERS(UserDefinedB1, a, b);
576+
NOP_STRUCTURE(UserDefinedB1, a, b);
577577
};
578578

579579
struct UserDefinedB2 {
580580
int a[10];
581581
float b[20];
582-
NOP_MEMBERS(UserDefinedB2, a, b);
582+
NOP_STRUCTURE(UserDefinedB2, a, b);
583583
};
584584

585585
struct UserDefinedC {
586586
int a[10];
587587
std::string b;
588-
NOP_MEMBERS(UserDefinedC, a, b);
588+
NOP_STRUCTURE(UserDefinedC, a, b);
589589
};
590590

591591
struct UserDefinedD {
592592
int a[10];
593-
NOP_MEMBERS(UserDefinedD, a);
593+
NOP_STRUCTURE(UserDefinedD, a);
594594
};
595595

596596
struct UserDefinedE {
597597
int a[10];
598598
float b[20];
599599
int c;
600-
NOP_MEMBERS(UserDefinedE, a, b, c);
600+
NOP_STRUCTURE(UserDefinedE, a, b, c);
601601
};
602602

603603
struct UserDefinedF1 {
604604
std::vector<int> a;
605-
NOP_MEMBERS(UserDefinedF1, a);
605+
NOP_STRUCTURE(UserDefinedF1, a);
606606
};
607607

608608
struct UserDefinedF2 {
609609
int a[10];
610610
int b;
611-
NOP_MEMBERS(UserDefinedF2, (a, b));
611+
NOP_STRUCTURE(UserDefinedF2, (a, b));
612612
};
613613

614614
struct UserDefinedF3 {
615615
std::array<int, 10> a;
616616
int b;
617-
NOP_MEMBERS(UserDefinedF3, (a, b));
617+
NOP_STRUCTURE(UserDefinedF3, (a, b));
618618
};
619619

620620
} // anonymous namespace

test/interface_tests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ struct MessageA {
5555
int a;
5656
std::string b;
5757

58-
NOP_MEMBERS(MessageA, a, b);
58+
NOP_STRUCTURE(MessageA, a, b);
5959
};
6060

6161
struct MessageB {
6262
float a;
6363
std::vector<int> b;
6464

65-
NOP_MEMBERS(MessageB, a, b);
65+
NOP_STRUCTURE(MessageB, a, b);
6666
};
6767

6868
struct TestInterface : Interface<TestInterface> {

test/serializer_tests.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct TestA {
6363
}
6464

6565
private:
66-
NOP_MEMBERS(TestA, a, b);
66+
NOP_STRUCTURE(TestA, a, b);
6767
};
6868

6969
enum class EnumA : std::uint8_t {
@@ -82,11 +82,11 @@ struct TestB {
8282
}
8383

8484
private:
85-
NOP_MEMBERS(TestB, a, b);
85+
NOP_STRUCTURE(TestB, a, b);
8686
};
8787

8888
struct TestC {
89-
NOP_MEMBERS(TestC);
89+
NOP_STRUCTURE(TestC);
9090
};
9191

9292
struct TestD {
@@ -98,7 +98,7 @@ struct TestD {
9898
return a == other.a && b == other.b && c == other.c;
9999
}
100100
};
101-
NOP_STRUCTURE(TestD, a, b, c);
101+
NOP_EXTERNAL_STRUCTURE(TestD, a, b, c);
102102

103103
template <typename T>
104104
struct TestE {
@@ -109,7 +109,7 @@ struct TestE {
109109
return a == other.a && b == other.b;
110110
}
111111
};
112-
NOP_TEMPLATE(TestE, a, b);
112+
NOP_EXTERNAL_TEMPLATE(TestE, a, b);
113113

114114
template <typename T, typename U>
115115
struct TestF {
@@ -120,7 +120,7 @@ struct TestF {
120120
return a == other.a && b == other.b;
121121
}
122122
};
123-
NOP_TEMPLATE(TestF, a, b);
123+
NOP_EXTERNAL_TEMPLATE(TestF, a, b);
124124

125125
struct TestG {
126126
int a;
@@ -131,20 +131,20 @@ struct TestG {
131131
}
132132

133133
private:
134-
NOP_MEMBERS(TestG, a, b);
134+
NOP_STRUCTURE(TestG, a, b);
135135
};
136136

137137
struct TestH {
138138
char data[128];
139139
unsigned char size;
140140
};
141-
NOP_STRUCTURE(TestH, (data, size));
141+
NOP_EXTERNAL_STRUCTURE(TestH, (data, size));
142142

143143
struct TestH2 {
144144
std::array<char, 128> data;
145145
unsigned char size;
146146
};
147-
NOP_STRUCTURE(TestH2, (data, size));
147+
NOP_EXTERNAL_STRUCTURE(TestH2, (data, size));
148148

149149
struct TestI {
150150
std::string names[5];
@@ -160,7 +160,7 @@ struct TestI {
160160
return true;
161161
}
162162

163-
NOP_MEMBERS(TestI, (names, size));
163+
NOP_STRUCTURE(TestI, (names, size));
164164
};
165165

166166
} // anonymous namespace

0 commit comments

Comments
 (0)