Skip to content

Commit 691c817

Browse files
committed
Add util::Ref class as temporary alternative for c++17 std::any
This commit does not change behavior
1 parent 04c0955 commit 691c817

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

src/Makefile.am

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ BITCOIN_CORE_H = \
223223
util/message.h \
224224
util/moneystr.h \
225225
util/rbf.h \
226+
util/ref.h \
226227
util/settings.h \
227228
util/string.h \
228229
util/threadnames.h \

src/Makefile.test.include

+1
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ BITCOIN_TESTS =\
229229
test/prevector_tests.cpp \
230230
test/raii_event_tests.cpp \
231231
test/random_tests.cpp \
232+
test/ref_tests.cpp \
232233
test/reverselock_tests.cpp \
233234
test/rpc_tests.cpp \
234235
test/sanity_tests.cpp \

src/test/ref_tests.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#include <util/ref.h>
6+
7+
#include <boost/test/unit_test.hpp>
8+
9+
BOOST_AUTO_TEST_SUITE(ref_tests)
10+
11+
BOOST_AUTO_TEST_CASE(ref_test)
12+
{
13+
util::Ref ref;
14+
BOOST_CHECK(!ref.Has<int>());
15+
BOOST_CHECK_THROW(ref.Get<int>(), NonFatalCheckError);
16+
int value = 5;
17+
ref.Set(value);
18+
BOOST_CHECK(ref.Has<int>());
19+
BOOST_CHECK_EQUAL(ref.Get<int>(), 5);
20+
++ref.Get<int>();
21+
BOOST_CHECK_EQUAL(ref.Get<int>(), 6);
22+
BOOST_CHECK_EQUAL(value, 6);
23+
++value;
24+
BOOST_CHECK_EQUAL(value, 7);
25+
BOOST_CHECK_EQUAL(ref.Get<int>(), 7);
26+
BOOST_CHECK(!ref.Has<bool>());
27+
BOOST_CHECK_THROW(ref.Get<bool>(), NonFatalCheckError);
28+
ref.Clear();
29+
BOOST_CHECK(!ref.Has<int>());
30+
BOOST_CHECK_THROW(ref.Get<int>(), NonFatalCheckError);
31+
}
32+
33+
BOOST_AUTO_TEST_SUITE_END()

src/util/check.h

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
#ifndef BITCOIN_UTIL_CHECK_H
66
#define BITCOIN_UTIL_CHECK_H
77

8+
#if defined(HAVE_CONFIG_H)
9+
#include <config/bitcoin-config.h>
10+
#endif
11+
812
#include <tinyformat.h>
913

1014
#include <stdexcept>

src/util/ref.h

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Copyright (c) 2020 The Bitcoin Core developers
2+
// Distributed under the MIT software license, see the accompanying
3+
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4+
5+
#ifndef BITCOIN_UTIL_REF_H
6+
#define BITCOIN_UTIL_REF_H
7+
8+
#include <util/check.h>
9+
10+
#include <typeindex>
11+
12+
namespace util {
13+
14+
/**
15+
* Type-safe dynamic reference.
16+
*
17+
* This implements a small subset of the functionality in C++17's std::any
18+
* class, and can be dropped when the project updates to C++17
19+
* (https://github.com/bitcoin/bitcoin/issues/16684)
20+
*/
21+
class Ref
22+
{
23+
public:
24+
Ref() = default;
25+
template<typename T> Ref(T& value) { Set(value); }
26+
template<typename T> T& Get() const { CHECK_NONFATAL(Has<T>()); return *static_cast<T*>(m_value); }
27+
template<typename T> void Set(T& value) { m_value = &value; m_type = std::type_index(typeid(T)); }
28+
template<typename T> bool Has() const { return m_value && m_type == std::type_index(typeid(T)); }
29+
void Clear() { m_value = nullptr; m_type = std::type_index(typeid(void)); }
30+
31+
private:
32+
void* m_value = nullptr;
33+
std::type_index m_type = std::type_index(typeid(void));
34+
};
35+
36+
} // namespace util
37+
38+
#endif // BITCOIN_UTIL_REF_H

0 commit comments

Comments
 (0)