rippled
ripple
basics
LocalValue.h
1
//------------------------------------------------------------------------------
2
/*
3
This file is part of rippled: https://github.com/ripple/rippled
4
Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6
Permission to use, copy, modify, and/or distribute this software for any
7
purpose with or without fee is hereby granted, provided that the above
8
copyright notice and this permission notice appear in all copies.
9
10
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
*/
18
//==============================================================================
19
20
#ifndef RIPPLE_BASICS_LOCALVALUE_H_INCLUDED
21
#define RIPPLE_BASICS_LOCALVALUE_H_INCLUDED
22
23
#include <boost/thread/tss.hpp>
24
#include <
memory
>
25
#include <
unordered_map
>
26
27
namespace
ripple
{
28
29
namespace
detail {
30
31
struct
LocalValues
32
{
33
explicit
LocalValues
() =
default
;
34
35
bool
onCoro
=
true
;
36
37
struct
BasicValue
38
{
39
virtual
~BasicValue
() =
default
;
40
virtual
void
*
get
() = 0;
41
};
42
43
template
<
class
T>
44
struct
Value
:
BasicValue
45
{
46
T
t_
;
47
48
Value
() =
default
;
49
explicit
Value
(T
const
& t) :
t_
(t) {}
50
51
void
*
get
()
override
52
{
53
return
&
t_
;
54
}
55
};
56
57
// Keys are the address of a LocalValue.
58
std::unordered_map<void const*, std::unique_ptr<BasicValue>
>
values
;
59
60
static
61
inline
62
void
63
cleanup
(
LocalValues
* lvs)
64
{
65
if
(lvs && ! lvs->
onCoro
)
66
delete
lvs;
67
}
68
};
69
70
template
<
class
=
void
>
71
boost::thread_specific_ptr<detail::LocalValues>&
72
getLocalValues
()
73
{
74
static
boost::thread_specific_ptr<
75
detail::LocalValues
> tsp(&
detail::LocalValues::cleanup
);
76
return
tsp;
77
}
78
79
}
// detail
80
81
template
<
class
T>
82
class
LocalValue
83
{
84
public
:
85
template
<
class
... Args>
86
LocalValue
(Args&&... args)
87
:
t_
(
std
::forward<Args>(args)...)
88
{
89
}
90
92
T&
operator*
();
93
95
T*
operator->
()
96
{
97
return
&**
this
;
98
}
99
100
private
:
101
T
t_
;
102
};
103
104
template
<
class
T>
105
T&
106
LocalValue<T>::operator*
()
107
{
108
auto
lvs =
detail::getLocalValues
().get();
109
if
(! lvs)
110
{
111
lvs =
new
detail::LocalValues
();
112
lvs->onCoro =
false
;
113
detail::getLocalValues
().reset(lvs);
114
}
115
else
116
{
117
auto
const
iter = lvs->values.find(
this
);
118
if
(iter != lvs->values.end())
119
return
*
reinterpret_cast<
T*
>
(iter->second->get());
120
}
121
122
return
*
reinterpret_cast<
T*
>
(lvs->values.emplace(
this
,
123
std::make_unique
<
detail::LocalValues::Value<T>
>(t_)).
124
first->second->get());
125
}
126
}
// ripple
127
128
#endif
ripple::LocalValue::operator*
T & operator*()
Stores instance of T specific to the calling coroutine or thread.
Definition:
LocalValue.h:106
ripple::detail::LocalValues::BasicValue::get
virtual void * get()=0
std::make_unique
T make_unique(T... args)
ripple::detail::getLocalValues
boost::thread_specific_ptr< detail::LocalValues > & getLocalValues()
Definition:
LocalValue.h:72
ripple::detail::LocalValues::LocalValues
LocalValues()=default
ripple::detail::LocalValues::cleanup
static void cleanup(LocalValues *lvs)
Definition:
LocalValue.h:63
ripple::detail::LocalValues::Value::Value
Value(T const &t)
Definition:
LocalValue.h:49
ripple::detail::LocalValues::Value
Definition:
LocalValue.h:44
ripple::LocalValue::LocalValue
LocalValue(Args &&... args)
Definition:
LocalValue.h:86
ripple::detail::LocalValues::onCoro
bool onCoro
Definition:
LocalValue.h:35
ripple::detail::LocalValues::Value::Value
Value()=default
memory
ripple::detail::LocalValues::Value::t_
T t_
Definition:
LocalValue.h:46
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
RCLCensorshipDetector.h:29
ripple::LocalValue
Definition:
LocalValue.h:82
ripple::detail::LocalValues::BasicValue::~BasicValue
virtual ~BasicValue()=default
std
STL namespace.
ripple::detail::LocalValues::Value::get
void * get() override
Definition:
LocalValue.h:51
ripple::detail::LocalValues::BasicValue
Definition:
LocalValue.h:37
ripple::LocalValue::t_
T t_
Definition:
LocalValue.h:101
ripple::detail::LocalValues::values
std::unordered_map< void const *, std::unique_ptr< BasicValue > > values
Definition:
LocalValue.h:58
ripple::detail::LocalValues
Definition:
LocalValue.h:31
unordered_map
ripple::LocalValue::operator->
T * operator->()
Stores instance of T specific to the calling coroutine or thread.
Definition:
LocalValue.h:95
Generated by
1.8.17