Skip to content

Commit c9818e9

Browse files
committed
Restore LLVM-style static registry instantiation
1 parent 50c9682 commit c9818e9

3 files changed

Lines changed: 54 additions & 32 deletions

File tree

‎include/stdcorelib/support/staticregistry.h‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,19 @@ namespace stdc {
271271
/// plugin arrangement is the host. \a EXPORT is what makes the list reachable from a plugin:
272272
/// \c __declspec(dllexport) on Windows, or nothing on the platforms where \c -rdynamic on the
273273
/// host is what does it.
274+
/// Since the macro defines the primary template's storage, use it only once in a translation
275+
/// unit; instantiate unrelated registry types in separate translation units.
274276
/// Both of these have to sit where they can name stdc, which is the global scope or inside stdc
275277
/// itself. Naming it rather than reopening it is what makes writing them anywhere else say so:
276278
/// the compiler answers "in namespace X, which does not enclose namespace stdc" instead of
277279
/// failing somewhere inside this header.
278280
#define STDC_INSTANTIATE_STATIC_REGISTRY_EXPORT(TYPE, EXPORT) \
279-
template <> \
280-
typename ::stdc::StaticRegistry<TYPE>::Node * ::stdc::StaticRegistry<TYPE>::_head = nullptr; \
281-
template <> \
282-
typename ::stdc::StaticRegistry<TYPE>::Node * ::stdc::StaticRegistry<TYPE>::_tail = nullptr; \
281+
template <class T, class Traits> \
282+
typename ::stdc::StaticRegistry<T, Traits>::Node * \
283+
::stdc::StaticRegistry<T, Traits>::_head = nullptr; \
284+
template <class T, class Traits> \
285+
typename ::stdc::StaticRegistry<T, Traits>::Node * \
286+
::stdc::StaticRegistry<T, Traits>::_tail = nullptr; \
283287
template class EXPORT ::stdc::StaticRegistry<TYPE>;
284288

285289
/// Defines the storage for a \c StaticRegistry over \a TYPE, undecorated.

‎tests/auto/support/test_staticregistry.cpp‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,28 +46,9 @@ namespace {
4646
int _channels;
4747
};
4848

49-
struct Descriptor {
50-
int value;
51-
};
52-
53-
}
54-
55-
namespace stdc {
56-
57-
template <>
58-
struct static_registry_traits<Descriptor> {
59-
using result_type = Descriptor;
60-
61-
template <class V>
62-
static result_type construct() {
63-
return V();
64-
}
65-
};
66-
6749
}
6850

6951
STDC_INSTANTIATE_STATIC_REGISTRY(Codec)
70-
STDC_INSTANTIATE_STATIC_REGISTRY(Descriptor)
7152

7253
namespace {
7354

@@ -84,10 +65,6 @@ namespace {
8465
return std::make_unique<PcmCodec>(44100, 2);
8566
});
8667

87-
using DescriptorRegistry = StaticRegistry<Descriptor>;
88-
DescriptorRegistry::AddFactory descriptor_entry("answer", "",
89-
[]() -> Descriptor { return {42}; });
90-
9168
}
9269

9370
BOOST_AUTO_TEST_SUITE(test_staticregistry)
@@ -138,9 +115,4 @@ BOOST_AUTO_TEST_CASE(test_factory_entry) {
138115
BOOST_CHECK_EQUAL(codec->id(), "pcm-44100-2"); // the arguments survived
139116
}
140117

141-
BOOST_AUTO_TEST_CASE(test_value_result) {
142-
const auto descriptor = DescriptorRegistry::begin()->instantiate();
143-
BOOST_CHECK_EQUAL(descriptor.value, 42);
144-
}
145-
146118
BOOST_AUTO_TEST_SUITE_END()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
#include <stdcorelib/support/staticregistry.h>
4+
5+
#include <boost/test/unit_test.hpp>
6+
7+
namespace {
8+
9+
struct Descriptor {
10+
int value;
11+
};
12+
13+
}
14+
15+
namespace stdc {
16+
17+
template <>
18+
struct static_registry_traits<Descriptor> {
19+
using result_type = Descriptor;
20+
21+
template <class V>
22+
static result_type construct() {
23+
return V();
24+
}
25+
};
26+
27+
}
28+
29+
STDC_INSTANTIATE_STATIC_REGISTRY(Descriptor)
30+
31+
namespace {
32+
33+
using DescriptorRegistry = stdc::StaticRegistry<Descriptor>;
34+
DescriptorRegistry::AddFactory descriptor_entry("answer", "",
35+
[]() -> Descriptor { return {42}; });
36+
37+
}
38+
39+
BOOST_AUTO_TEST_SUITE(test_staticregistry_traits)
40+
41+
BOOST_AUTO_TEST_CASE(test_value_result) {
42+
const auto descriptor = DescriptorRegistry::begin()->instantiate();
43+
BOOST_CHECK_EQUAL(descriptor.value, 42);
44+
}
45+
46+
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)