clang-tools 20.0.0git
Classes | Public Member Functions | Static Public Member Functions | List of all members
clang::clangd::Context Class Reference

A context is an immutable container for per-request data that must be propagated through layers that don't care about it. More...

#include <Context.h>

Public Member Functions

 Context ()=default
 Same as Context::empty(), please use Context::empty() instead.
 
 Context (Context const &)=delete
 Copy operations for this class are deleted, use an explicit clone() method when you need a copy of the context instead.
 
Contextoperator= (const Context &)=delete
 
 Context (Context &&)=default
 
Contextoperator= (Context &&)=default
 
template<class Type >
const Typeget (const Key< Type > &Key) const
 Get data stored for a typed Key.
 
template<class Type >
const TypegetExisting (const Key< Type > &Key) const
 A helper to get a reference to a Key that must exist in the map.
 
template<class Type >
Context derive (const Key< Type > &Key, std::decay_t< Type > Value) const &
 Derives a child context It is safe to move or destroy a parent context after calling derive().
 
template<class Type >
Context derive (const Key< Type > &Key, std::decay_t< Type > Value) &&
 
template<class Type >
Context derive (Type &&Value) const &
 Derives a child context, using an anonymous key.
 
template<class Type >
Context derive (Type &&Value) &&
 
Context clone () const
 Clone this context object.
 

Static Public Member Functions

static Context empty ()
 Returns an empty root context that contains no data.
 
static const Contextcurrent ()
 Returns the context for the current thread, creating it if needed.
 
static Context swapCurrent (Context Replacement)
 

Detailed Description

A context is an immutable container for per-request data that must be propagated through layers that don't care about it.

An example is a request ID that we may want to use when logging.

Conceptually, a context is a heterogeneous map<Key<T>, T>. Each key has an associated value type, which allows the map to be typesafe.

There is an "ambient" context for each thread, Context::current(). Most functions should read from this, and use WithContextValue or WithContext to extend or replace the context within a block scope. Only code dealing with threads and extension points should need to use other Context objects.

You can't add data to an existing context, instead you create a new immutable context derived from it with extra data added. When you retrieve data, the context will walk up the parent chain until the key is found.

Definition at line 69 of file Context.h.

Constructor & Destructor Documentation

◆ Context() [1/3]

clang::clangd::Context::Context ( )
default

Same as Context::empty(), please use Context::empty() instead.

Referenced by clone(), derive(), and empty().

◆ Context() [2/3]

clang::clangd::Context::Context ( Context const &  )
delete

Copy operations for this class are deleted, use an explicit clone() method when you need a copy of the context instead.

◆ Context() [3/3]

clang::clangd::Context::Context ( Context &&  )
default

Member Function Documentation

◆ clone()

Context clang::clangd::Context::clone ( ) const

◆ current()

const Context & clang::clangd::Context::current ( )
static

◆ derive() [1/4]

template<class Type >
Context clang::clangd::Context::derive ( const Key< Type > &  Key,
std::decay_t< Type Value 
) &&
inline

Definition at line 127 of file Context.h.

References Context(), and clang::clangd::Value.

◆ derive() [2/4]

template<class Type >
Context clang::clangd::Context::derive ( const Key< Type > &  Key,
std::decay_t< Type Value 
) const &
inline

Derives a child context It is safe to move or destroy a parent context after calling derive().

The child will keep its parent alive, and its data remains accessible.

Definition at line 119 of file Context.h.

References Context(), and clang::clangd::Value.

Referenced by clang::clangd::cancelableTask(), derive(), clang::clangd::TUScheduler::runWithPreamble(), clang::clangd::TEST(), and clang::clangd::TEST_F().

◆ derive() [3/4]

template<class Type >
Context clang::clangd::Context::derive ( Type &&  Value) &&
inline

Definition at line 142 of file Context.h.

References clang::clangd::Value.

◆ derive() [4/4]

template<class Type >
Context clang::clangd::Context::derive ( Type &&  Value) const &
inline

Derives a child context, using an anonymous key.

Intended for objects stored only for their destructor's side-effect.

Definition at line 137 of file Context.h.

References derive(), and clang::clangd::Value.

◆ empty()

Context clang::clangd::Context::empty ( )
static

Returns an empty root context that contains no data.

Definition at line 15 of file Context.cpp.

References Context().

Referenced by clang::clangd::currentContext(), and clang::clangd::TEST().

◆ get()

template<class Type >
const Type * clang::clangd::Context::get ( const Key< Type > &  Key) const
inline

Get data stored for a typed Key.

If values are not found

Returns
Pointer to the data associated with Key. If no data is specified for Key, return null.

Definition at line 98 of file Context.h.

References clang::clangd::Type.

Referenced by clang::clangd::cancelableTask(), clang::clangd::fromJSON(), getExisting(), clang::clangd::lspEncoding(), and clang::clangd::TEST().

◆ getExisting()

template<class Type >
const Type & clang::clangd::Context::getExisting ( const Key< Type > &  Key) const
inline

A helper to get a reference to a Key that must exist in the map.

Must not be called for keys that are not in the map.

Definition at line 109 of file Context.h.

References get().

◆ operator=() [1/2]

Context & clang::clangd::Context::operator= ( const Context )
delete

◆ operator=() [2/2]

Context & clang::clangd::Context::operator= ( Context &&  )
default

◆ swapCurrent()

Context clang::clangd::Context::swapCurrent ( Context  Replacement)
static

Definition at line 29 of file Context.cpp.

References clang::clangd::currentContext().


The documentation for this class was generated from the following files: