userver: components::ComponentContext Class Reference
Loading...
Searching...
No Matches
components::ComponentContext Class Referencefinal

#include <userver/components/component_context.hpp>

Detailed Description

Class to retrieve other components.

Only the const member functions of this class are meant for usage in component constructor (because of that this class is always passed as a const reference to the constructors).

Warning
Don't store references to ComponentContext in your component! Lifetime of the passed ComponentContext ends as soon as the constructor ends.

For usage outside the component constructor see components::State.

See also
Components
Examples
core/src/components/component_sample_test.cpp, core/src/components/component_sample_test.hpp, postgresql/functional_tests/basic_chaos/postgres_service.cpp, samples/config_service/main.cpp, samples/digest_auth_service/auth_digest.cpp, samples/digest_auth_service/auth_digest.hpp, samples/easy/4_custom_dependency/main.cpp, samples/easy/5_pg_service_template/src/main.cpp, samples/flatbuf_service/main.cpp, samples/grpc-generic-proxy/src/proxy_service.cpp, samples/grpc-generic-proxy/src/proxy_service.hpp, samples/grpc_middleware_service/src/middlewares/server/meta_filter.cpp, samples/grpc_middleware_service/src/middlewares/server/meta_filter.hpp, samples/grpc_middleware_service/src/service/view.cpp, samples/grpc_middleware_service/src/service/view.hpp, samples/grpc_service/src/greeter_service.cpp, samples/grpc_service/src/greeter_service.hpp, samples/kafka_service/src/consumer_handler.cpp, samples/kafka_service/src/consumer_handler.hpp, samples/kafka_service/src/producer_handler.cpp, samples/kafka_service/src/producer_handler.hpp, samples/mongo_service/main.cpp, samples/postgres_auth/auth_bearer.cpp, samples/postgres_auth/auth_bearer.hpp, samples/postgres_service/main.cpp, samples/redis_service/main.cpp, samples/s3api/src/s3api_client.cpp, samples/s3api/src/s3api_client.hpp, samples/scylla_service/src/main.cpp, samples/tcp_full_duplex_service/main.cpp, samples/tcp_service/main.cpp, samples/testsuite-support/src/metrics.cpp, samples/testsuite-support/src/metrics.hpp, samples/testsuite-support/src/tasks.cpp, samples/websocket_client/main.cpp, samples/ydb_service/components/federated_topic_reader.cpp, samples/ydb_service/components/federated_topic_reader.hpp, samples/ydb_service/components/topic_reader.cpp, samples/ydb_service/components/topic_reader.hpp, samples/ydb_service/views/base_handler.hpp, samples/ydb_topic_writer_service/src/write_handler.cpp, and samples/ydb_topic_writer_service/src/write_handler.hpp.

Definition at line 70 of file component_context.hpp.

Public Member Functions

 ComponentContext (ComponentContext &&)=delete
ComponentContext & operator= (ComponentContext &&)=delete
template<typename T>
T & FindComponent () const
 Finds a component of type T with specified name (if any) and returns the component after it was initialized.
template<typename T>
T & FindComponent (std::string_view name) const
template<typename T>
T & FindComponent (std::string_view={})
template<typename T>
T * FindComponentOptional () const
 If there's no component with specified type and name return nullptr; otherwise behaves as FindComponent().
template<typename T>
T * FindComponentOptional (std::string_view name) const
template<typename T>
T & FindComponentOptional (std::string_view={})
engine::TaskProcessor & GetTaskProcessor (std::string_view name) const
 Returns an engine::TaskProcessor with the specified name.
template<typename T>
engine::TaskProcessor & GetTaskProcessor (const T &)
std::string_view GetComponentName () const
 Returns the current component name. This is helpful in cases where multiple instances of the component class may be created using component_list.Append<T>("custom-name") syntax.
utils::ResourceScopeStorage & Scopes () const
 Returns the component's utils::ResourceScopeStorage.

Member Function Documentation

◆ FindComponent() [1/3]

template<typename T>
T & components::ComponentContext::FindComponent ( ) const
inline

Finds a component of type T with specified name (if any) and returns the component after it was initialized.

Can only be called from other component's constructor in a task where that constructor was called. May block and asynchronously wait for the creation of the requested component.

Exceptions
ComponentsLoadCancelledExceptionif components loading was cancelled due to errors in the creation of other component.
std::runtime_errorif component missing in component_list was requested.
Examples
core/src/components/component_sample_test.cpp, postgresql/functional_tests/basic_chaos/postgres_service.cpp, samples/digest_auth_service/auth_digest.cpp, samples/easy/4_custom_dependency/main.cpp, samples/easy/5_pg_service_template/src/main.cpp, samples/kafka_service/src/consumer_handler.cpp, samples/kafka_service/src/producer_handler.cpp, samples/mongo_service/main.cpp, samples/postgres_auth/auth_bearer.cpp, samples/s3api/src/s3api_client.cpp, samples/scylla_service/src/main.cpp, samples/testsuite-support/src/metrics.cpp, samples/websocket_client/main.cpp, samples/ydb_service/components/federated_topic_reader.cpp, samples/ydb_service/components/topic_reader.cpp, samples/ydb_service/views/base_handler.hpp, and samples/ydb_topic_writer_service/src/write_handler.cpp.

Definition at line 87 of file component_context.hpp.

◆ FindComponent() [2/3]

template<typename T>
T & components::ComponentContext::FindComponent ( std::string_view name) const
inline

Definition at line 93 of file component_context.hpp.

◆ FindComponent() [3/3]

template<typename T>
T & components::ComponentContext::FindComponent ( std::string_view = {})
inline

Definition at line 108 of file component_context.hpp.

◆ FindComponentOptional() [1/3]

template<typename T>
T * components::ComponentContext::FindComponentOptional ( ) const
inline

If there's no component with specified type and name return nullptr; otherwise behaves as FindComponent().

Definition at line 115 of file component_context.hpp.

◆ FindComponentOptional() [2/3]

template<typename T>
T * components::ComponentContext::FindComponentOptional ( std::string_view name) const
inline

Definition at line 121 of file component_context.hpp.

◆ FindComponentOptional() [3/3]

template<typename T>
T & components::ComponentContext::FindComponentOptional ( std::string_view = {})
inline

Definition at line 129 of file component_context.hpp.

◆ GetComponentName()

std::string_view components::ComponentContext::GetComponentName ( ) const

Returns the current component name. This is helpful in cases where multiple instances of the component class may be created using component_list.Append<T>("custom-name") syntax.

Warning
The lifetime of the returned string ends as soon as the current component's constructor completes. Store it as an std::string if needed.

◆ GetTaskProcessor()

template<typename T>
engine::TaskProcessor & components::ComponentContext::GetTaskProcessor ( const T & )
inline

Definition at line 137 of file component_context.hpp.

◆ Scopes()

utils::ResourceScopeStorage & components::ComponentContext::Scopes ( ) const

Returns the component's utils::ResourceScopeStorage.

See choosing scopes before passing these scopes to another object.


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