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
*
41
get
() = 0;
42
};
43
44
template
<
class
T>
45
struct
Value
:
BasicValue
46
{
47
T
t_
;
48
49
Value
() =
default
;
50
explicit
Value
(T
const
& t) :
t_
(t)
51
{
52
}
53
54
void
*
55
get
()
override
56
{
57
return
&
t_
;
58
}
59
};
60
61
// Keys are the address of a LocalValue.
62
std::unordered_map<void const*, std::unique_ptr<BasicValue>
>
values
;
63
64
static
inline
void
65
cleanup
(
LocalValues
* lvs)
66
{
67
if
(lvs && !lvs->
onCoro
)
68
delete
lvs;
69
}
70
};
71
72
template
<
class
=
void
>
73
boost::thread_specific_ptr<detail::LocalValues>&
74
getLocalValues
()
75
{
76
static
boost::thread_specific_ptr<detail::LocalValues> tsp(
77
&
detail::LocalValues::cleanup
);
78
return
tsp;
79
}
80
81
}
// namespace detail
82
83
template
<
class
T>
84
class
LocalValue
85
{
86
public
:
87
template
<
class
... Args>
88
LocalValue
(Args&&... args) :
t_
(
std
::forward<Args>(args)...)
89
{
90
}
91
93
T&
94
operator*
();
95
97
T*
98
operator->
()
99
{
100
return
&**
this
;
101
}
102
103
private
:
104
T
t_
;
105
};
106
107
template
<
class
T>
108
T&
109
LocalValue<T>::operator*
()
110
{
111
auto
lvs =
detail::getLocalValues
().get();
112
if
(!lvs)
113
{
114
lvs =
new
detail::LocalValues
();
115
lvs->onCoro =
false
;
116
detail::getLocalValues
().reset(lvs);
117
}
118
else
119
{
120
auto
const
iter = lvs->values.find(
this
);
121
if
(iter != lvs->values.end())
122
return
*
reinterpret_cast<
T*
>
(iter->second->get());
123
}
124
125
return
*
reinterpret_cast<
T*
>
(
126
lvs->values
127
.emplace(
this
,
std::make_unique
<
detail::LocalValues::Value<T>
>(t_))
128
.first->second->get());
129
}
130
}
// namespace ripple
131
132
#endif
ripple::LocalValue::operator*
T & operator*()
Stores instance of T specific to the calling coroutine or thread.
Definition:
LocalValue.h:109
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:74
ripple::detail::LocalValues::LocalValues
LocalValues()=default
ripple::detail::LocalValues::cleanup
static void cleanup(LocalValues *lvs)
Definition:
LocalValue.h:65
ripple::detail::LocalValues::Value::Value
Value(T const &t)
Definition:
LocalValue.h:50
ripple::detail::LocalValues::Value
Definition:
LocalValue.h:45
ripple::LocalValue::LocalValue
LocalValue(Args &&... args)
Definition:
LocalValue.h:88
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:47
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition:
RCLCensorshipDetector.h:29
ripple::LocalValue
Definition:
LocalValue.h:84
ripple::detail::LocalValues::BasicValue::~BasicValue
virtual ~BasicValue()=default
std
STL namespace.
ripple::detail::LocalValues::Value::get
void * get() override
Definition:
LocalValue.h:55
ripple::detail::LocalValues::BasicValue
Definition:
LocalValue.h:37
ripple::LocalValue::t_
T t_
Definition:
LocalValue.h:104
ripple::detail::LocalValues::values
std::unordered_map< void const *, std::unique_ptr< BasicValue > > values
Definition:
LocalValue.h:62
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:98
Generated by
1.8.17