13#include "azure/core/rtti.hpp"
23namespace Azure {
namespace Core {
namespace Tracing {
27namespace Azure {
namespace Core {
78 Key const* m_uniqueAddress;
85 Key() : m_uniqueAddress(this) {}
94 return this->m_uniqueAddress == other.m_uniqueAddress;
106 struct ContextSharedState final
108 std::shared_ptr<ContextSharedState> Parent;
109 std::atomic<DateTime::rep> Deadline;
110 std::shared_ptr<Azure::Core::Tracing::TracerProvider> TraceProvider;
112 std::shared_ptr<void> Value;
113#if defined(AZ_CORE_RTTI)
114 const std::type_info& ValueType;
116 static constexpr DateTime::rep ToDateTimeRepresentation(
DateTime const& dateTime)
118 return dateTime.time_since_epoch().count();
121 static constexpr DateTime FromDateTimeRepresentation(DateTime::rep dtRepresentation)
126 ContextSharedState(ContextSharedState
const&) =
delete;
127 ContextSharedState(ContextSharedState&&) =
delete;
128 ContextSharedState& operator=(ContextSharedState
const&) =
delete;
129 ContextSharedState&& operator=(ContextSharedState&&) =
delete;
134 explicit ContextSharedState()
135 : Deadline(ToDateTimeRepresentation((DateTime::max)())), Value(nullptr)
136#if defined(AZ_CORE_RTTI)
138 ValueType(typeid(std::nullptr_t))
150 explicit ContextSharedState(
151 const std::shared_ptr<ContextSharedState>& parent,
152 DateTime
const& deadline = (DateTime::max)())
153 : Parent(parent), Deadline(ToDateTimeRepresentation(deadline)), Value(nullptr)
154#if defined(AZ_CORE_RTTI)
156 ValueType(typeid(std::nullptr_t))
173 explicit ContextSharedState(
174 const std::shared_ptr<ContextSharedState>& parent,
175 DateTime
const& deadline,
176 Context::Key
const& key,
178 : Parent(parent), Deadline(ToDateTimeRepresentation(deadline)), Key(key),
179 Value(std::make_shared<T>(std::move(value)))
180#if defined(AZ_CORE_RTTI)
188 std::shared_ptr<ContextSharedState> m_contextSharedState;
190 explicit Context(std::shared_ptr<ContextSharedState> impl)
191 : m_contextSharedState(std::move(impl))
200 Context() : m_contextSharedState(std::make_shared<ContextSharedState>()) {}
210 : m_contextSharedState(std::make_shared<ContextSharedState>(nullptr, deadline))
276 return Context{std::make_shared<ContextSharedState>(m_contextSharedState, deadline)};
291 return Context{std::make_shared<ContextSharedState>(
292 m_contextSharedState, (DateTime::max)(), key, std::forward<T>(value))};
321 for (std::shared_ptr<ContextSharedState> ptr = m_contextSharedState; ptr; ptr = ptr->Parent)
325#if defined(AZ_CORE_RTTI)
327 typeid(T) == ptr->ValueType,
"Type mismatch for Context::TryGetValue().");
330 outputValue = *
reinterpret_cast<const T*
>(ptr->Value.get());
350 m_contextSharedState->Deadline
351 = ContextSharedState::ToDateTimeRepresentation((DateTime::min)());
380 "ApplicationContext is no longer supported. Instead customers should create their "
Provide assert macros to use with pre-conditions.
#define AZURE_ASSERT_MSG(exp, msg)
Azure specific assert macro with message.
Definition azure_assert.hpp:53
A key used to store and retrieve data in an Azure::Core::Context object.
Definition context.hpp:77
bool operator!=(Key const &other) const
Compares with other Key for equality.
Definition context.hpp:102
bool operator==(Key const &other) const
Compares with other Key for equality.
Definition context.hpp:92
Key()
Constructs a default instance of Key.
Definition context.hpp:85
A context is a node within a unidirectional tree that represents deadlines and key/value pairs.
Definition context.hpp:72
void ThrowIfCancelled() const
Throws if the context is cancelled.
Definition context.hpp:364
void Cancel()
Cancels the context. All operations which share this Context will be cancelled.
Definition context.hpp:348
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:319
Context WithDeadline(DateTime const &deadline) const
Creates a context with a deadline from an existing Context object.
Definition context.hpp:274
Context & operator=(Context &&other)=default
Moves a context.
Context(Context &&other)=default
Moves a context.
~Context()=default
Destroys a context.
Context WithValue(Key const &key, T &&value) const
Creates a new child context with key and value associated with it.
Definition context.hpp:289
Context & operator=(Context const &other)=default
Assigns a context.
static const AZ_CORE_DLLEXPORT Context ApplicationContext
The ApplicationContext is a deprecated singleton Context object.
Definition context.hpp:381
Context()
Constructs a context with no deadline, and no value associated.
Definition context.hpp:200
bool IsCancelled() const
Checks if the context is cancelled.
Definition context.hpp:358
Context(Context const &)=default
Copies a context.
DateTime GetDeadline() const
Gets the deadline for this context or the branch of contexts this context belongs to.
Definition context.cpp:30
Context(DateTime const &deadline)
Constructs a context with a deadline object.
Definition context.hpp:209
An exception thrown when an operation is cancelled.
Definition context.hpp:32
OperationCancelledException(std::string const &what)
Constructs an OperationCancelledException with message string as the description.
Definition context.hpp:39
Manages date and time in standardized string formats.
Definition datetime.hpp:54
operator std::chrono::system_clock::time_point() const
Convert an instance of Azure::DateTime to std::chrono::system_clock::time_point.
Definition datetime.cpp:424
Support for date and time standardized string formats.
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57