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
25#include <memory>
26#include <unordered_map>
27
28namespace ripple {
29
30namespace detail {
31
33{
34 explicit LocalValues() = default;
35
36 bool onCoro = true;
37
39 {
40 virtual ~BasicValue() = default;
41 virtual void*
42 get() = 0;
43 };
44
45 template <class T>
47 {
48 T t_;
49
50 Value() = default;
51 explicit Value(T const& t) : t_(t)
52 {
53 }
54
55 void*
56 get() override
57 {
58 return &t_;
59 }
60 };
61
62 // Keys are the address of a LocalValue.
64
65 static inline void
67 {
68 if (lvs && !lvs->onCoro)
69 delete lvs;
70 }
71};
72
73template <class = void>
74boost::thread_specific_ptr<detail::LocalValues>&
76{
77 static boost::thread_specific_ptr<detail::LocalValues> tsp(
79 return tsp;
80}
81
82} // namespace detail
83
84template <class T>
86{
87public:
88 template <class... Args>
89 LocalValue(Args&&... args) : t_(std::forward<Args>(args)...)
90 {
91 }
92
94 T&
95 operator*();
96
98 T*
100 {
101 return &**this;
102 }
103
104private:
105 T t_;
106};
107
108template <class T>
109T&
111{
112 auto lvs = detail::getLocalValues().get();
113 if (!lvs)
114 {
115 lvs = new detail::LocalValues();
116 lvs->onCoro = false;
117 detail::getLocalValues().reset(lvs);
118 }
119 else
120 {
121 auto const iter = lvs->values.find(this);
122 if (iter != lvs->values.end())
123 return *reinterpret_cast<T*>(iter->second->get());
124 }
125
126 return *reinterpret_cast<T*>(
127 lvs->values
129 .first->second->get());
130}
131} // namespace ripple
132
133#endif
T & operator*()
Stores instance of T specific to the calling coroutine or thread.
Definition: LocalValue.h:110
LocalValue(Args &&... args)
Definition: LocalValue.h:89
T * operator->()
Stores instance of T specific to the calling coroutine or thread.
Definition: LocalValue.h:99
T make_unique(T... args)
boost::thread_specific_ptr< detail::LocalValues > & getLocalValues()
Definition: LocalValue.h:75
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:63
static void cleanup(LocalValues *lvs)
Definition: LocalValue.h:66