| 1 |
//===--- Context.h - Mechanism for passing implicit data --------*- C++-*-===// |
| 2 |
// |
| 3 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 |
// See https://llvm.org/LICENSE.txt for license information. |
| 5 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 |
// |
| 7 |
//===----------------------------------------------------------------------===// |
| 8 |
// |
| 9 |
// Context for storing and retrieving implicit data. Useful for passing implicit |
| 10 |
// parameters on a per-request basis. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#pragma once |
| 15 |
|
| 16 |
#include <memory> |
| 17 |
#include <type_traits> |
| 18 |
|
| 19 |
namespace lsp |
| 20 |
{ |
| 21 |
|
| 22 |
/// Values in a Context are indexed by typed keys. |
| 23 |
/// Key<T> serves two purposes: |
| 24 |
/// - it provides a lookup key for the context (each Key is unique), |
| 25 |
/// - it makes lookup type-safe: a Key<T> can only map to a T (or nothing). |
| 26 |
/// |
| 27 |
/// Example: |
| 28 |
/// Key<int> RequestID; |
| 29 |
/// Key<int> Version; |
| 30 |
/// |
| 31 |
/// Context Ctx = Context::empty().derive(RequestID, 10).derive(Version, 3); |
| 32 |
/// assert(*Ctx.get(RequestID) == 10); |
| 33 |
/// assert(*Ctx.get(Version) == 3); |
| 34 |
/// |
| 35 |
/// Keys are typically used across multiple functions, so most of the time you |
| 36 |
/// would want to make them static class members or global variables. |
| 37 |
template<class Type> |
| 38 |
class Key |
| 39 |
{ |
| 40 |
public: |
| 41 |
static_assert(!std::is_reference<Type>::value, "Reference arguments to Key<> are not allowed"); |
| 42 |
|
| 43 |
constexpr Key() = default; |
| 44 |
|
| 45 |
Key(Key const&) = delete; |
| 46 |
Key& operator=(Key const&) = delete; |
| 47 |
Key(Key&&) = delete; |
| 48 |
Key& operator=(Key&&) = delete; |
| 49 |
}; |
| 50 |
|
| 51 |
/// A context is an immutable container for per-request data that must be |
| 52 |
/// propagated through layers that don't care about it. An example is a request |
| 53 |
/// ID that we may want to use when logging. |
| 54 |
/// |
| 55 |
/// Conceptually, a context is a heterogeneous map<Key<T>, T>. Each key has |
| 56 |
/// an associated value type, which allows the map to be typesafe. |
| 57 |
/// |
| 58 |
/// There is an "ambient" context for each thread, Context::current(). |
| 59 |
/// Most functions should read from this, and use WithContextValue or |
| 60 |
/// WithContext to extend or replace the context within a block scope. |
| 61 |
/// Only code dealing with threads and extension points should need to use |
| 62 |
/// other Context objects. |
| 63 |
/// |
| 64 |
/// You can't add data to an existing context, instead you create a new |
| 65 |
/// immutable context derived from it with extra data added. When you retrieve |
| 66 |
/// data, the context will walk up the parent chain until the key is found. |
| 67 |
class Context |
| 68 |
{ |
| 69 |
public: |
| 70 |
/// Returns an empty root context that contains no data. |
| 71 |
static Context empty(); |
| 72 |
/// Returns the context for the current thread, creating it if needed. |
| 73 |
static Context const& current(); |
| 74 |
// Sets the current() context to Replacement, and returns the old context. |
| 75 |
// Prefer to use WithContext or WithContextValue to do this safely. |
| 76 |
static Context swapCurrent(Context Replacement); |
| 77 |
|
| 78 |
private: |
| 79 |
struct Data; |
| 80 |
Context(std::shared_ptr<Data const> DataPtr); |
| 81 |
|
| 82 |
public: |
| 83 |
/// Same as Context::empty(), please use Context::empty() instead. |
| 84 |
Context() = default; |
| 85 |
|
| 86 |
/// Copy operations for this class are deleted, use an explicit clone() method |
| 87 |
/// when you need a copy of the context instead. |
| 88 |
Context(Context const&) = delete; |
| 89 |
Context& operator=(Context const&) = delete; |
| 90 |
|
| 91 |
Context(Context&&) = default; |
| 92 |
Context& operator=(Context&&) = default; |
| 93 |
|
| 94 |
/// Get data stored for a typed \p Key. If values are not found |
| 95 |
/// \returns Pointer to the data associated with \p Key. If no data is |
| 96 |
/// specified for \p Key, return null. |
| 97 |
template<class Type> |
| 98 |
Type const* get(Key<Type> const& Key) const |
| 99 |
{ |
| 100 |
for (Data const* DataPtr = this->dataPtr.get(); DataPtr != nullptr; DataPtr = DataPtr->parent.get()) |
| 101 |
{ |
| 102 |
if (DataPtr->KeyPtr == &Key) |
| 103 |
{ |
| 104 |
return static_cast<Type const*>(DataPtr->value->getValuePtr()); |
| 105 |
} |
| 106 |
} |
| 107 |
return nullptr; |
| 108 |
} |
| 109 |
|
| 110 |
/// A helper to get a reference to a \p Key that must exist in the map. |
| 111 |
/// Must not be called for keys that are not in the map. |
| 112 |
template<class Type> |
| 113 |
Type const& getExisting(Key<Type> const& Key) const |
| 114 |
{ |
| 115 |
auto Val = get(Key); |
| 116 |
assert(Val && "Key does not exist"); |
| 117 |
return *Val; |
| 118 |
} |
| 119 |
|
| 120 |
/// Derives a child context |
| 121 |
/// It is safe to move or destroy a parent context after calling derive(). |
| 122 |
/// The child will keep its parent alive, and its data remains accessible. |
| 123 |
template<class Type> |
| 124 |
Context derive(Key<Type> const& Key, typename std::decay<Type>::type Value) const& |
| 125 |
{ |
| 126 |
return Context(std::make_shared<Data>(Data { |
| 127 |
/*parent=*/dataPtr, &Key, |
| 128 |
std::make_unique<TypedAnyStorage<typename std::decay<Type>::type>>(std::move(Value)) |
| 129 |
})); |
| 130 |
} |
| 131 |
|
| 132 |
template<class Type> |
| 133 |
Context derive(Key<Type> const& Key, typename std::decay<Type>::type Value) && /* takes ownership */ |
| 134 |
{ |
| 135 |
return Context(std::make_shared<Data>(Data { |
| 136 |
/*parent=*/std::move(dataPtr), &Key, |
| 137 |
std::make_unique<TypedAnyStorage<typename std::decay<Type>::type>>(std::move(Value)) |
| 138 |
})); |
| 139 |
} |
| 140 |
|
| 141 |
/// Derives a child context, using an anonymous key. |
| 142 |
/// Intended for objects stored only for their destructor's side-effect. |
| 143 |
template<class Type> |
| 144 |
Context derive(Type&& Value) const& |
| 145 |
{ |
| 146 |
static Key<typename std::decay<Type>::type> Private; |
| 147 |
return derive(Private, std::forward<Type>(Value)); |
| 148 |
} |
| 149 |
|
| 150 |
template<class Type> |
| 151 |
Context derive(Type&& Value) && |
| 152 |
{ |
| 153 |
static Key<typename std::decay<Type>::type> Private; |
| 154 |
return std::move(*this).derive(Private, std::forward<Type>(Value)); |
| 155 |
} |
| 156 |
|
| 157 |
/// Clone this context object. |
| 158 |
Context clone() const; |
| 159 |
|
| 160 |
private: |
| 161 |
class AnyStorage |
| 162 |
{ |
| 163 |
public: |
| 164 |
virtual ~AnyStorage() = default; |
| 165 |
virtual void* getValuePtr() = 0; |
| 166 |
}; |
| 167 |
|
| 168 |
template<class T> |
| 169 |
class TypedAnyStorage : public Context::AnyStorage |
| 170 |
{ |
| 171 |
static_assert( |
| 172 |
std::is_same<typename std::decay<T>::type, T>::value, "Argument to TypedAnyStorage must be decayed" |
| 173 |
); |
| 174 |
|
| 175 |
public: |
| 176 |
TypedAnyStorage(T&& Value) : value(std::move(Value)) |
| 177 |
{ |
| 178 |
} |
| 179 |
|
| 180 |
void* getValuePtr() override |
| 181 |
{ |
| 182 |
return &value; |
| 183 |
} |
| 184 |
|
| 185 |
private: |
| 186 |
T value; |
| 187 |
}; |
| 188 |
|
| 189 |
struct Data |
| 190 |
{ |
| 191 |
// We need to make sure parent outlives the value, so the order of members |
| 192 |
// is important. We do that to allow classes stored in Context's child |
| 193 |
// layers to store references to the data in the parent layers. |
| 194 |
std::shared_ptr<Data const> parent; |
| 195 |
void const* KeyPtr; |
| 196 |
std::unique_ptr<AnyStorage> value; |
| 197 |
}; |
| 198 |
|
| 199 |
std::shared_ptr<Data const> dataPtr; |
| 200 |
}; |
| 201 |
|
| 202 |
/// WithContext replaces Context::current() with a provided scope. |
| 203 |
/// When the WithContext is destroyed, the original scope is restored. |
| 204 |
/// For extending the current context with new value, prefer WithContextValue. |
| 205 |
class WithContext |
| 206 |
{ |
| 207 |
public: |
| 208 |
WithContext(Context C) : restore(Context::swapCurrent(std::move(C))) |
| 209 |
{ |
| 210 |
} |
| 211 |
~WithContext() |
| 212 |
{ |
| 213 |
Context::swapCurrent(std::move(restore)); |
| 214 |
} |
| 215 |
WithContext(WithContext const&) = delete; |
| 216 |
WithContext& operator=(WithContext const&) = delete; |
| 217 |
WithContext(WithContext&&) = delete; |
| 218 |
WithContext& operator=(WithContext&&) = delete; |
| 219 |
|
| 220 |
private: |
| 221 |
Context restore; |
| 222 |
}; |
| 223 |
|
| 224 |
/// WithContextValue extends Context::current() with a single value. |
| 225 |
/// When the WithContextValue is destroyed, the original scope is restored. |
| 226 |
class WithContextValue |
| 227 |
{ |
| 228 |
public: |
| 229 |
template<typename T> |
| 230 |
WithContextValue(Key<T> const& K, typename std::decay<T>::type V) |
| 231 |
: restore(Context::current().derive(K, std::move(V))) |
| 232 |
{ |
| 233 |
} |
| 234 |
|
| 235 |
// Anonymous values can be used for the destructor side-effect. |
| 236 |
template<typename T> |
| 237 |
WithContextValue(T&& V) : restore(Context::current().derive(std::forward<T>(V))) |
| 238 |
{ |
| 239 |
} |
| 240 |
|
| 241 |
private: |
| 242 |
WithContext restore; |
| 243 |
}; |
| 244 |
|
| 245 |
} // namespace lsp |