File tree 5 files changed +77
-0
lines changed
5 files changed +77
-0
lines changed Original file line number Diff line number Diff line change @@ -223,6 +223,7 @@ BITCOIN_CORE_H = \
223
223
util/message.h \
224
224
util/moneystr.h \
225
225
util/rbf.h \
226
+ util/ref.h \
226
227
util/settings.h \
227
228
util/string.h \
228
229
util/threadnames.h \
Original file line number Diff line number Diff line change @@ -229,6 +229,7 @@ BITCOIN_TESTS =\
229
229
test/prevector_tests.cpp \
230
230
test/raii_event_tests.cpp \
231
231
test/random_tests.cpp \
232
+ test/ref_tests.cpp \
232
233
test/reverselock_tests.cpp \
233
234
test/rpc_tests.cpp \
234
235
test/sanity_tests.cpp \
Original file line number Diff line number Diff line change
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 ()
Original file line number Diff line number Diff line change 5
5
#ifndef BITCOIN_UTIL_CHECK_H
6
6
#define BITCOIN_UTIL_CHECK_H
7
7
8
+ #if defined(HAVE_CONFIG_H)
9
+ #include < config/bitcoin-config.h>
10
+ #endif
11
+
8
12
#include < tinyformat.h>
9
13
10
14
#include < stdexcept>
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments