Lines Matching full:context

1 //===--- Context.h - Mechanism for passing implicit data --------*- C++-*-===//
9 // Context for storing and retrieving implicit data. Useful for passing implicit
25 /// Values in a Context are indexed by typed keys.
27 /// - it provides a lookup key for the context (each Key is unique),
34 /// Context Ctx = Context::empty().derive(RequestID, 10).derive(Version, 3);
53 /// A context is an immutable container for per-request data that must be
57 /// Conceptually, a context is a heterogeneous map<Key<T>, T>. Each key has
60 /// There is an "ambient" context for each thread, Context::current().
62 /// WithContext to extend or replace the context within a block scope.
64 /// other Context objects.
66 /// You can't add data to an existing context, instead you create a new
67 /// immutable context derived from it with extra data added. When you retrieve
68 /// data, the context will walk up the parent chain until the key is found.
69 class Context {
71 /// Returns an empty root context that contains no data.
72 static Context empty();
73 /// Returns the context for the current thread, creating it if needed.
74 static const Context &current();
75 // Sets the current() context to Replacement, and returns the old context.
77 static Context swapCurrent(Context Replacement);
81 Context(std::shared_ptr<const Data> DataPtr);
84 /// Same as Context::empty(), please use Context::empty() instead.
85 Context() = default;
88 /// when you need a copy of the context instead.
89 Context(Context const &) = delete;
90 Context &operator=(const Context &) = delete;
92 Context(Context &&) = default;
93 Context &operator=(Context &&) = default;
115 /// Derives a child context
116 /// It is safe to move or destroy a parent context after calling derive().
119 Context derive(const Key<Type> &Key, std::decay_t<Type> Value) const & { in derive()
120 return Context(std::make_shared<Data>( in derive()
127 Context derive(const Key<Type> &Key, in derive()
129 return Context(std::make_shared<Data>( in derive()
135 /// Derives a child context, using an anonymous key.
137 template <class Type> Context derive(Type &&Value) const & { in derive()
142 template <class Type> Context derive(Type &&Value) && { in derive()
147 /// Clone this context object.
148 Context clone() const;
157 template <class T> class TypedAnyStorage : public Context::AnyStorage {
172 // is important. We do that to allow classes stored in Context's child
182 /// WithContext replaces Context::current() with a provided scope.
184 /// For extending the current context with new value, prefer WithContextValue.
187 WithContext(Context C) : Restore(Context::swapCurrent(std::move(C))) {} in WithContext()
188 ~WithContext() { Context::swapCurrent(std::move(Restore)); } in ~WithContext()
195 Context Restore;
198 /// WithContextValue extends Context::current() with a single value.
204 : Restore(Context::current().derive(K, std::move(V))) {} in WithContextValue()
209 : Restore(Context::current().derive(std::forward<T>(V))) {} in WithContextValue()