clang-tools 10.0.0

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. More...
| 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. More...
Context & | operator= (const Context &)=delete
| Context (Context &&)=default
Context & | operator= (Context &&)=default
template<class Type >
const Type * | get (const Key< Type > &Key) const
| Get data stored for a typed `Key`. More...
template<class Type >
const Type & | getExisting (const Key< Type > &Key) const
| A helper to get a reference to a `Key` that must exist in the map. More...
template<class Type >
Context | derive (const Key< Type > &Key, typename std::decay< Type >::type Value) const &
| Derives a child context It is safe to move or destroy a parent context after calling derive(). More...
template<class Type >
Context | derive (const Key< Type > &Key, typename std::decay< Type >::type Value) &&
template<class Type >
Context | derive (Type &&Value) const &
| Derives a child context, using an anonymous key. More...
template<class Type >
Context | derive (Type &&Value) &&
Context | clone () const
| Clone this context object. More...

## Static Public Member Functions
static Context | empty ()
| Returns an empty root context that contains no data. More...
static const Context & | current ()
| Returns the context for the current thread, creating it if needed. More...
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.

Constructor is defined to workaround a bug in MSVC's version of STL. (arguments of std::future<> must be default-constructible in MSVC).

Referenced by clone(), 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

Clone this context object.

Definition at line 20 of file Context.cpp.

References Context().

Referenced by clang::clangd::runAsync().

## ◆ current()

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

Returns the context for the current thread, creating it if needed.

Definition at line 27 of file Context.cpp.

References clang::clangd::currentContext().

Referenced by clang::clangd::cancelableTask(), clang::clangd::TUScheduler::getFileBeingProcessedInContext(), clang::clangd::lspEncoding(), clang::clangd::TUScheduler::run(), and clang::clangd::runAsync().

## ◆ derive() [1/4]

template<class Type >

Context clang::clangd::Context::derive | ( | const Key< Type > & | Key,
|  | typename std::decay< Type >::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 121 of file Context.h.

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

## ◆ derive() [2/4]

template<class Type >

Context clang::clangd::Context::derive | ( | const Key< Type > & | Key,
|  | typename std::decay< Type >::type | Value
| ) |  | &&
| inline

Definition at line 131 of file Context.h.

## ◆ derive() [3/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 141 of file Context.h.

References clang::clangd::Value.

## ◆ derive() [4/4]

template<class Type >

Context clang::clangd::Context::derive | ( | Type && | Value | ) | &&
| inline

Definition at line 146 of file Context.h.

References Parent, 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 100 of file Context.h.

References Type.

Referenced by clang::clangd::isCancelled(), 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 111 of file Context.h.

References clang::clangd::Key< Type >::Key().

## ◆ 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().

Referenced by clang::clangd::WithContext::~WithContext().

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

- Context.h
- Context.cpp