Skip to content

Commit ced64ba

Browse files
committed
Break NOP_TABLE into NOP_TABLE, NOP_TABLE_HASH, and NOP_TABLE_NS variants.
1 parent 45c2006 commit ced64ba

File tree

3 files changed

+45
-33
lines changed

3 files changed

+45
-33
lines changed

examples/table.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ using nop::StringToHex;
5050
// situations where requirements are expected to evolve over time.
5151
//
5252
// A table is a class or structure with members of type nop::Entry<T, Id>. These
53-
// members are called the table's entries. The macro NOP_TABLE() is used to
53+
// members are called the table's entries. The macro NOP_TABLE*() is used to
5454
// describe the table and its entries to the serialization engine. Each entry
5555
// has a type, which may be any serializable type, and a numeric id that is
5656
// unique among the entries of the same table. Entry ids should not change or be
@@ -77,7 +77,7 @@ namespace version1 {
7777
struct TableA {
7878
Entry<std::string, 0> a;
7979

80-
NOP_TABLE("TableA", TableA, a);
80+
NOP_TABLE_NS("TableA", TableA, a);
8181
};
8282

8383
} // namespace version1
@@ -89,7 +89,7 @@ struct TableA {
8989
Entry<std::string, 0> a;
9090
Entry<std::vector<int>, 1> b;
9191

92-
NOP_TABLE("TableA", TableA, a, b);
92+
NOP_TABLE_NS("TableA", TableA, a, b);
9393
};
9494

9595
} // namespace version2
@@ -101,7 +101,7 @@ struct TableA {
101101
Entry<std::string, 0> a;
102102
Entry<std::vector<int>, 1, DeletedEntry> b;
103103

104-
NOP_TABLE("TableA", TableA, a, b);
104+
NOP_TABLE_NS("TableA", TableA, a, b);
105105
};
106106

107107
} // namespace version3

include/nop/table.h

+37-25
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919

2020
#include <type_traits>
2121

22-
#include <nop/structure.h>
2322
#include <nop/base/macros.h>
23+
#include <nop/structure.h>
2424
#include <nop/types/optional.h>
2525
#include <nop/utility/sip_hash.h>
2626

@@ -46,7 +46,7 @@ namespace nop {
4646
// struct MyTable {
4747
// Entry<Address, 0> address;
4848
// Entry<PhoneNumber, 1> phone_number;
49-
// NOP_TABLE("MyTable", MyTable, address, phone_number);
49+
// NOP_TABLE(MyTable, address, phone_number);
5050
// };
5151
//
5252
// Example of handling empty values:
@@ -62,8 +62,8 @@ namespace nop {
6262
// HandlePhoneNumber(my_table.phone_number.get());
6363
//
6464
// Table entries may be public, private, or protected, depending on how the
65-
// table type will be used. If the entries are non-public, call NOP_TABLE() in a
66-
// private section to avoid exposing member pointers to arbitrary code.
65+
// table type will be used. If the entries are non-public, call NOP_TABLE*() in
66+
// a private section to avoid exposing member pointers to arbitrary code.
6767
//
6868
// Use the following rules to maximize compatability between different versions
6969
// of a table type:
@@ -74,10 +74,11 @@ namespace nop {
7474
// old entry id.
7575
// 3. Never change the Id for an entry. Doing so will break compatibility with
7676
// older versions of serialized data.
77-
// 4. Never change the string name passed as the first argument to the macro
78-
// NOP_TABLE. This is used to compute a hash used for sanity checking
79-
// during deserialization. Changing this string will break compatibility
80-
// with older versions of serialized data.
77+
// 4. Never change the namespace hash or namespace string passed as the first
78+
// argument to the macros NOP_TABLE_HASH / NOP_TABLE_NS. These values are
79+
// used for sanity checking during deserialization. Changing these
80+
// arguments will break compatibility with older versions of serialized
81+
// data.
8182
//
8283

8384
// Type tags to define used and deprecated entries.
@@ -173,23 +174,34 @@ enum : std::uint64_t {
173174
kNopTableKey1 = 0x0123456789abcdef,
174175
};
175176

176-
// Defines a table type, its hash, and its members. This macro should be call
177-
// once within a table struct or class to inform the serialization engine about
178-
// the table members and hash value. This is accomplished by befriending several
179-
// key classes and defining an internal type named NOP__ENTRIES that describes
180-
// the table type's Entry<T, Id> members.
181-
#define NOP_TABLE(string_name, type, ... /*entries*/) \
182-
template <typename, typename> \
183-
friend struct ::nop::Encoding; \
184-
template <typename, typename> \
185-
friend struct ::nop::HasEntryList; \
186-
template <typename> \
187-
friend struct ::nop::EntryListTraits; \
188-
using NOP__ENTRIES = ::nop::EntryList< \
189-
::nop::HashValue<::nop::SipHash::Compute( \
190-
string_name, ::nop::kNopTableKey0, ::nop::kNopTableKey1)>, \
191-
NOP_MEMBER_LIST(type, __VA_ARGS__)>
177+
// Defines a table type, its namespace hash, and its members. This macro must be
178+
// invoked once within a table struct or class to inform the serialization
179+
// engine about the table members and hash value. This is accomplished by
180+
// befriending several key classes and defining an internal type named
181+
// NOP__ENTRIES that describes the table type's Entry<T, Id> members.
182+
#define NOP_TABLE_HASH(hash, type, ... /*entries*/) \
183+
template <typename, typename> \
184+
friend struct ::nop::Encoding; \
185+
template <typename, typename> \
186+
friend struct ::nop::HasEntryList; \
187+
template <typename> \
188+
friend struct ::nop::EntryListTraits; \
189+
using NOP__ENTRIES = ::nop::EntryList<::nop::HashValue<hash>, \
190+
NOP_MEMBER_LIST(type, __VA_ARGS__)>
191+
192+
// Similar to NOP_TABLE_HASH except that the namespace hash is computed from a
193+
// compile-time hash of the given string literal that defines the namespace of
194+
// the table.
195+
#define NOP_TABLE_NS(string_name, type, ... /*entries*/) \
196+
NOP_TABLE_HASH(::nop::SipHash::Compute(string_name, ::nop::kNopTableKey0, \
197+
::nop::kNopTableKey1), \
198+
type, __VA_ARGS__)
199+
200+
// Similar to NOP_TABLE_HASH except that the namespace hash is set to zero,
201+
// which has a compact encoding compared to average 64bit hash values. This
202+
// saves space in the encoding when namespace checks are not desired.
203+
#define NOP_TABLE(type, ... /*entries*/) NOP_TABLE_HASH(0, type, __VA_ARGS__)
192204

193205
} // namespace nop
194206

195-
#endif // LIBNOP_INCLUDE_NOP_TABLE_H_
207+
#endif // LIBNOP_INCLUDE_NOP_TABLE_H_

test/fungible_tests.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -735,26 +735,26 @@ namespace {
735735
struct TableA0 {
736736
Entry<int, 0> a;
737737
Entry<std::vector<int>, 1> b;
738-
NOP_TABLE("TableA", TableA0, a, b);
738+
NOP_TABLE_NS("TableA", TableA0, a, b);
739739
};
740740

741741
struct TableA1 {
742742
Entry<int, 0> a;
743743
Entry<std::array<int, 10>, 1> b;
744-
NOP_TABLE("TableA", TableA1, a, b);
744+
NOP_TABLE_NS("TableA", TableA1, a, b);
745745
};
746746

747747
struct TableA2 {
748748
Entry<int, 0> a;
749749
Entry<std::vector<int>, 1> b;
750-
NOP_TABLE("DifferentLabel", TableA2, a, b);
750+
NOP_TABLE_NS("DifferentLabel", TableA2, a, b);
751751
};
752752

753753
struct TableA3 {
754754
Entry<int, 0> a;
755755
Entry<std::array<int, 10>, 1> b;
756756
Entry<float, 2> c;
757-
NOP_TABLE("TableA", TableA3, a, b, c);
757+
NOP_TABLE_NS("TableA", TableA3, a, b, c);
758758
};
759759

760760
} // anonymous namespace

0 commit comments

Comments
 (0)