azure-core
Loading...
Searching...
No Matches
context.hpp
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
9#pragma once
10
13#include "azure/core/rtti.hpp"
14
15#include <atomic>
16#include <chrono>
17#include <memory>
18#include <stdexcept>
19#include <string>
20#include <type_traits>
21
22// Forward declare TracerProvider to resolve an include file dependency ordering problem.
23namespace Azure { namespace Core { namespace Tracing {
24 class TracerProvider;
25}}} // namespace Azure::Core::Tracing
26
27namespace Azure { namespace Core {
28
32 class OperationCancelledException final : public std::runtime_error {
33 public:
39 explicit OperationCancelledException(std::string const& what) : std::runtime_error(what) {}
40 };
41
72 class Context final {
73 public:
77 class Key final {
78 Key const* m_uniqueAddress;
79
80 public:
85 Key() : m_uniqueAddress(this) {}
86
92 bool operator==(Key const& other) const
93 {
94 return this->m_uniqueAddress == other.m_uniqueAddress;
95 }
96
102 bool operator!=(Key const& other) const { return !(*this == other); }
103 };
104
105 private:
106 struct ContextSharedState final
107 {
108 std::shared_ptr<ContextSharedState> Parent;
109 std::atomic<DateTime::rep> Deadline;
110 std::shared_ptr<Azure::Core::Tracing::TracerProvider> TraceProvider;
111 Context::Key Key;
112 std::shared_ptr<void> Value;
113#if defined(AZ_CORE_RTTI)
114 const std::type_info& ValueType;
115#endif
116 static constexpr DateTime::rep ToDateTimeRepresentation(DateTime const& dateTime)
117 {
118 return dateTime.time_since_epoch().count();
119 }
120
121 static constexpr DateTime FromDateTimeRepresentation(DateTime::rep dtRepresentation)
122 {
123 return DateTime(DateTime::time_point(DateTime::duration(dtRepresentation)));
124 }
125
126 ContextSharedState(ContextSharedState const&) = delete;
127 ContextSharedState(ContextSharedState&&) = delete;
128 ContextSharedState& operator=(ContextSharedState const&) = delete;
129 ContextSharedState&& operator=(ContextSharedState&&) = delete;
130
134 explicit ContextSharedState()
135 : Deadline(ToDateTimeRepresentation((DateTime::max)())), Value(nullptr)
136#if defined(AZ_CORE_RTTI)
137 ,
138 ValueType(typeid(std::nullptr_t))
139#endif
140 {
141 }
142
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)
155 ,
156 ValueType(typeid(std::nullptr_t))
157#endif
158 {
159 }
160
172 template <class T>
173 explicit ContextSharedState(
174 const std::shared_ptr<ContextSharedState>& parent,
175 DateTime const& deadline,
176 Context::Key const& key,
177 T value) // NOTE, should this be T&&
178 : Parent(parent), Deadline(ToDateTimeRepresentation(deadline)), Key(key),
179 Value(std::make_shared<T>(std::move(value)))
180#if defined(AZ_CORE_RTTI)
181 ,
182 ValueType(typeid(T))
183#endif
184 {
185 }
186 };
187
188 std::shared_ptr<ContextSharedState> m_contextSharedState;
189
190 explicit Context(std::shared_ptr<ContextSharedState> impl)
191 : m_contextSharedState(std::move(impl))
192 {
193 }
194
195 public:
200 Context() : m_contextSharedState(std::make_shared<ContextSharedState>()) {}
201
209 explicit Context(DateTime const& deadline)
210 : m_contextSharedState(std::make_shared<ContextSharedState>(nullptr, deadline))
211 {
212 }
213
225 Context(Context const&) = default;
226
238 Context& operator=(Context const& other) = default;
239
248 Context(Context&& other) = default;
249
259 Context& operator=(Context&& other) = default;
260
265 ~Context() = default;
266
274 Context WithDeadline(DateTime const& deadline) const
275 {
276 return Context{std::make_shared<ContextSharedState>(m_contextSharedState, deadline)};
277 }
278
289 template <class T> Context WithValue(Key const& key, T&& value) const
290 {
291 return Context{std::make_shared<ContextSharedState>(
292 m_contextSharedState, (DateTime::max)(), key, std::forward<T>(value))};
293 }
294
303 DateTime GetDeadline() const;
304
319 template <class T> bool TryGetValue(Key const& key, T& outputValue) const
320 {
321 for (std::shared_ptr<ContextSharedState> ptr = m_contextSharedState; ptr; ptr = ptr->Parent)
322 {
323 if (ptr->Key == key)
324 {
325#if defined(AZ_CORE_RTTI)
327 typeid(T) == ptr->ValueType, "Type mismatch for Context::TryGetValue().");
328#endif
329
330 outputValue = *reinterpret_cast<const T*>(ptr->Value.get());
331 return true;
332 }
333 }
334 return false;
335 }
336
348 void Cancel()
349 {
350 m_contextSharedState->Deadline
351 = ContextSharedState::ToDateTimeRepresentation((DateTime::min)());
352 }
353
358 bool IsCancelled() const { return GetDeadline() < std::chrono::system_clock::now(); }
359
364 void ThrowIfCancelled() const
365 {
366 if (IsCancelled())
367 {
368 throw OperationCancelledException("Request was cancelled by context.");
369 }
370 }
371
379 [[deprecated(
380 "ApplicationContext is no longer supported. Instead customers should create their "
381 "own root context objects.")]] static const AZ_CORE_DLLEXPORT Context ApplicationContext;
382 };
383}} // namespace Azure::Core
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