rippled
Loading...
Searching...
No Matches
LocalValue.h
1#ifndef XRPL_BASICS_LOCALVALUE_H_INCLUDED
2#define XRPL_BASICS_LOCALVALUE_H_INCLUDED
3
4#include <boost/thread/tss.hpp>
5
6#include <memory>
7#include <unordered_map>
8
9namespace ripple {
10
11namespace detail {
12
14{
15 explicit LocalValues() = default;
16
17 bool onCoro = true;
18
20 {
21 virtual ~BasicValue() = default;
22 virtual void*
23 get() = 0;
24 };
25
26 template <class T>
28 {
29 T t_;
30
31 Value() = default;
32 explicit Value(T const& t) : t_(t)
33 {
34 }
35
36 void*
37 get() override
38 {
39 return &t_;
40 }
41 };
42
43 // Keys are the address of a LocalValue.
45
46 static inline void
48 {
49 if (lvs && !lvs->onCoro)
50 delete lvs;
51 }
52};
53
54template <class = void>
55boost::thread_specific_ptr<detail::LocalValues>&
57{
58 static boost::thread_specific_ptr<detail::LocalValues> tsp(
60 return tsp;
61}
62
63} // namespace detail
64
65template <class T>
67{
68public:
69 template <class... Args>
70 LocalValue(Args&&... args) : t_(std::forward<Args>(args)...)
71 {
72 }
73
75 T&
76 operator*();
77
79 T*
81 {
82 return &**this;
83 }
84
85private:
86 T t_;
87};
88
89template <class T>
90T&
92{
93 auto lvs = detail::getLocalValues().get();
94 if (!lvs)
95 {
96 lvs = new detail::LocalValues();
97 lvs->onCoro = false;
98 detail::getLocalValues().reset(lvs);
99 }
100 else
101 {
102 auto const iter = lvs->values.find(this);
103 if (iter != lvs->values.end())
104 return *reinterpret_cast<T*>(iter->second->get());
105 }
106
107 return *reinterpret_cast<T*>(
108 lvs->values
110 .first->second->get());
111}
112} // namespace ripple
113
114#endif
T & operator*()
Stores instance of T specific to the calling coroutine or thread.
Definition LocalValue.h:91
LocalValue(Args &&... args)
Definition LocalValue.h:70
T * operator->()
Stores instance of T specific to the calling coroutine or thread.
Definition LocalValue.h:80
T make_unique(T... args)
boost::thread_specific_ptr< detail::LocalValues > & getLocalValues()
Definition LocalValue.h:56
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
STL namespace.
std::unordered_map< void const *, std::unique_ptr< BasicValue > > values
Definition LocalValue.h:44
static void cleanup(LocalValues *lvs)
Definition LocalValue.h:47