Bitcoin Core
0.21.1
P2P Digital Currency
Main Page
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Modules
src
util
ref.h
Go to the documentation of this file.
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
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
check.h
util
Definition:
httprpc.h:8
CHECK_NONFATAL
#define CHECK_NONFATAL(condition)
Throw a NonFatalCheckError when the condition evaluates to false.
Definition:
check.h:32
util::Ref::Get
T & Get() const
Definition:
ref.h:26
util::Ref::Set
void Set(T &value)
Definition:
ref.h:27
util::Ref::m_value
void * m_value
Definition:
ref.h:32
util::Ref::Has
bool Has() const
Definition:
ref.h:28
util::Ref::m_type
std::type_index m_type
Definition:
ref.h:33
util::Ref::Ref
Ref(T &value)
Definition:
ref.h:25
util::Ref
Type-safe dynamic reference.
Definition:
ref.h:21
util::Ref::Ref
Ref()=default
util::Ref::Clear
void Clear()
Definition:
ref.h:29
Generated on Fri Apr 30 2021 13:53:46 for Bitcoin Core by
1.8.8