rippled
Loading...
Searching...
No Matches
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
27namespace ripple {
28
29namespace detail {
30
32{
33 explicit LocalValues() = default;
34
35 bool onCoro = true;
36
38 {
39 virtual ~BasicValue() = default;
40 virtual void*
41 get() = 0;
42 };
43
44 template <class T>
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.
63
64 static inline void
66 {
67 if (lvs && !lvs->onCoro)
68 delete lvs;
69 }
70};
71
72template <class = void>
73boost::thread_specific_ptr<detail::LocalValues>&
75{
76 static boost::thread_specific_ptr<detail::LocalValues> tsp(
78 return tsp;
79}
80
81} // namespace detail
82
83template <class T>
85{
86public:
87 template <class... Args>
88 LocalValue(Args&&... args) : t_(std::forward<Args>(args)...)
89 {
90 }
91
93 T&
94 operator*();
95
97 T*
99 {
100 return &**this;
101 }
102
103private:
104 T t_;
105};
106
107template <class T>
108T&
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
128 .first->second->get());
129}
130} // namespace ripple
131
132#endif
T & operator*()
Stores instance of T specific to the calling coroutine or thread.
Definition: LocalValue.h:109
LocalValue(Args &&... args)
Definition: LocalValue.h:88
T * operator->()
Stores instance of T specific to the calling coroutine or thread.
Definition: LocalValue.h:98
T make_unique(T... args)
boost::thread_specific_ptr< detail::LocalValues > & getLocalValues()
Definition: LocalValue.h:74
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
STL namespace.
std::unordered_map< void const *, std::unique_ptr< BasicValue > > values
Definition: LocalValue.h:62
static void cleanup(LocalValues *lvs)
Definition: LocalValue.h:65