Go to the documentation of this file.
20 #include <type_traits>
23 namespace Azure {
namespace Core {
namespace Tracing {
27 namespace Azure {
namespace Core {
51 Key const* m_uniqueAddress;
58 Key() : m_uniqueAddress(this) {}
67 return this->m_uniqueAddress == other.m_uniqueAddress;
79 struct ContextSharedState final
81 std::shared_ptr<ContextSharedState> Parent;
82 std::atomic<DateTime::rep> Deadline;
83 std::shared_ptr<Azure::Core::Tracing::TracerProvider> TraceProvider;
85 std::shared_ptr<void> Value;
86 #if defined(AZ_CORE_RTTI)
87 const std::type_info& ValueType;
89 static constexpr DateTime::rep ToDateTimeRepresentation(
DateTime const& dateTime)
91 return dateTime.time_since_epoch().count();
94 static constexpr
DateTime FromDateTimeRepresentation(DateTime::rep dtRepresentation)
96 return DateTime(DateTime::time_point(DateTime::duration(dtRepresentation)));
99 explicit ContextSharedState()
100 : Deadline(ToDateTimeRepresentation((DateTime::max)())), Value(nullptr)
101 #if defined(AZ_CORE_RTTI)
103 ValueType(typeid(std::nullptr_t))
108 explicit ContextSharedState(
109 const std::shared_ptr<ContextSharedState>& parent,
110 DateTime
const& deadline)
111 : Parent(parent), Deadline(ToDateTimeRepresentation(deadline)), Value(nullptr)
112 #if defined(AZ_CORE_RTTI)
114 ValueType(typeid(std::nullptr_t))
120 explicit ContextSharedState(
121 const std::shared_ptr<ContextSharedState>& parent,
122 DateTime
const& deadline,
123 Context::Key
const& key,
125 : Parent(parent), Deadline(ToDateTimeRepresentation(deadline)), Key(key),
126 Value(std::make_shared<T>(std::move(value)))
127 #if defined(AZ_CORE_RTTI)
135 std::shared_ptr<ContextSharedState> m_contextSharedState;
137 explicit Context(std::shared_ptr<ContextSharedState> impl)
138 : m_contextSharedState(std::move(impl))
147 Context() : m_contextSharedState(std::make_shared<ContextSharedState>()) {}
167 return Context{std::make_shared<ContextSharedState>(m_contextSharedState, deadline)};
181 return Context{std::make_shared<ContextSharedState>(
182 m_contextSharedState, (DateTime::max)(), key, std::forward<T>(value))};
211 for (
auto ptr = m_contextSharedState; ptr; ptr = ptr->Parent)
215 #if defined(AZ_CORE_RTTI)
217 typeid(T) == ptr->ValueType,
"Type mismatch for Context::TryGetValue().");
220 outputValue = *
reinterpret_cast<const T*
>(ptr->Value.get());
233 m_contextSharedState->Deadline
234 = ContextSharedState::ToDateTimeRepresentation((DateTime::min)());
261 return m_contextSharedState->TraceProvider;
269 m_contextSharedState->TraceProvider = tracerProvider;
std::shared_ptr< Tracing::TracerProvider > GetTracerProvider()
Returns the tracer provider for the current context.
Definition: context.hpp:259
void ThrowIfCancelled() const
Checks if the context is cancelled.
Definition: context.hpp:248
#define AZ_CORE_DLLEXPORT
Applies DLL export attribute, when applicable.
Definition: dll_import_export.hpp:93
OperationCancelledException(std::string const &what)
Constructs an OperationCancelledException with message string as the description.
Definition: context.hpp:39
Support for date and time standardized string formats.
bool IsCancelled() const
Checks if the context is cancelled.
Definition: context.hpp:241
DateTime GetDeadline() const
Gets the deadline for this context or the branch of contexts this context belongs to.
Definition: context.cpp:10
An exception thrown when an operation is cancelled.
Definition: context.hpp:32
Run-time type info enable or disable.
Context WithValue(Key const &key, T &&value) const
Creates a context without a deadline, but with key and value associated with it.
Definition: context.hpp:179
void SetTracerProvider(std::shared_ptr< Tracing::TracerProvider > tracerProvider)
Sets the tracer provider for the current context.
Definition: context.hpp:267
Context WithDeadline(DateTime const &deadline) const
Creates a context with a deadline.
Definition: context.hpp:165
Azure SDK abstractions.
Definition: azure_assert.hpp:55
bool TryGetValue(Key const &key, T &outputValue) const
Gets the value associated with a key parameter within this context or the branch of contexts this con...
Definition: context.hpp:209
void Cancel()
Cancels the context.
Definition: context.hpp:231
Provide assert macros to use with pre-conditions.
Manages date and time in standardized string formats.
Definition: datetime.hpp:54
bool operator!=(Key const &other) const
Compares with other Key for equality.
Definition: context.hpp:75
Context & operator=(const Context &other)=default
Assigns Context to another Context instance.
bool operator==(Key const &other) const
Compares with other Key for equality.
Definition: context.hpp:65
Context()
Constructs a new context with no deadline, and no value associated.
Definition: context.hpp:147
A context is a node within a tree that represents deadlines and key/value pairs.
Definition: context.hpp:45
A key used to store and retrieve data in an Azure::Core::Context object.
Definition: context.hpp:50
static AZ_CORE_DLLEXPORT Context ApplicationContext
The application context (root).
Definition: context.hpp:276
Key()
Constructs a default instance of Key.
Definition: context.hpp:58