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>()) {}
158 return Context{std::make_shared<ContextSharedState>(m_contextSharedState, deadline)};
172 return Context{std::make_shared<ContextSharedState>(
173 m_contextSharedState, (DateTime::max)(), key, std::forward<T>(value))};
202 for (
auto ptr = m_contextSharedState; ptr; ptr = ptr->Parent)
206 #if defined(AZ_CORE_RTTI)
208 typeid(T) == ptr->ValueType,
"Type mismatch for Context::TryGetValue().");
211 outputValue = *
reinterpret_cast<const T*
>(ptr->Value.get());
224 m_contextSharedState->Deadline
225 = ContextSharedState::ToDateTimeRepresentation((DateTime::min)());
Provide assert macros to use with pre-conditions.
A key used to store and retrieve data in an Azure::Core::Context object.
Definition: context.hpp:50
bool operator!=(Key const &other) const
Compares with other Key for equality.
Definition: context.hpp:75
bool operator==(Key const &other) const
Compares with other Key for equality.
Definition: context.hpp:65
Key()
Constructs a default instance of Key.
Definition: context.hpp:58
A context is a node within a tree that represents deadlines and key/value pairs.
Definition: context.hpp:45
void ThrowIfCancelled() const
Checks if the context is cancelled.
Definition: context.hpp:239
void Cancel()
Cancels the context.
Definition: context.hpp:222
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:200
Context WithDeadline(DateTime const &deadline) const
Creates a context with a deadline.
Definition: context.hpp:156
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:170
static const AZ_CORE_DLLEXPORT Context ApplicationContext
The application context (root).
Definition: context.hpp:251
Context()
Constructs a new context with no deadline, and no value associated.
Definition: context.hpp:147
bool IsCancelled() const
Checks if the context is cancelled.
Definition: context.hpp:232
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
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
Support for date and time standardized string formats.
#define AZ_CORE_DLLEXPORT
Applies DLL export attribute, when applicable.
Definition: dll_import_export.hpp:93
Azure SDK abstractions.
Definition: azure_assert.hpp:55
Run-time type info enable or disable.