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/dll_import_export.hpp"
14#include "azure/core/rtti.hpp"
15
16#include <atomic>
17#include <chrono>
18#include <memory>
19#include <stdexcept>
20#include <string>
21#include <type_traits>
22
23// Forward declare TracerProvider to resolve an include file dependency ordering problem.
24namespace Azure { namespace Core { namespace Tracing {
25 class TracerProvider;
26}}} // namespace Azure::Core::Tracing
27
28namespace Azure { namespace Core {
29
33 class OperationCancelledException final : public std::runtime_error {
34 public:
40 explicit OperationCancelledException(std::string const& what) : std::runtime_error(what) {}
41 };
42
46 class Context final {
47 public:
51 class Key final {
52 Key const* m_uniqueAddress;
53
54 public:
59 Key() : m_uniqueAddress(this) {}
60
66 bool operator==(Key const& other) const
67 {
68 return this->m_uniqueAddress == other.m_uniqueAddress;
69 }
70
76 bool operator!=(Key const& other) const { return !(*this == other); }
77 };
78
79 private:
80 struct ContextSharedState final
81 {
82 std::shared_ptr<ContextSharedState> Parent;
83 std::atomic<DateTime::rep> Deadline;
84 std::shared_ptr<Azure::Core::Tracing::TracerProvider> TraceProvider;
85 Context::Key Key;
86 std::shared_ptr<void> Value;
87#if defined(AZ_CORE_RTTI)
88 const std::type_info& ValueType;
89#endif
90 static constexpr DateTime::rep ToDateTimeRepresentation(DateTime const& dateTime)
91 {
92 return dateTime.time_since_epoch().count();
93 }
94
95 static constexpr DateTime FromDateTimeRepresentation(DateTime::rep dtRepresentation)
96 {
97 return DateTime(DateTime::time_point(DateTime::duration(dtRepresentation)));
98 }
99
100 explicit ContextSharedState()
101 : Deadline(ToDateTimeRepresentation((DateTime::max)())), Value(nullptr)
102#if defined(AZ_CORE_RTTI)
103 ,
104 ValueType(typeid(std::nullptr_t))
105#endif
106 {
107 }
108
109 explicit ContextSharedState(
110 const std::shared_ptr<ContextSharedState>& parent,
111 DateTime const& deadline)
112 : Parent(parent), Deadline(ToDateTimeRepresentation(deadline)), Value(nullptr)
113#if defined(AZ_CORE_RTTI)
114 ,
115 ValueType(typeid(std::nullptr_t))
116#endif
117 {
118 }
119
120 template <class T>
121 explicit ContextSharedState(
122 const std::shared_ptr<ContextSharedState>& parent,
123 DateTime const& deadline,
124 Context::Key const& key,
125 T value) // NOTE, should this be T&&
126 : Parent(parent), Deadline(ToDateTimeRepresentation(deadline)), Key(key),
127 Value(std::make_shared<T>(std::move(value)))
128#if defined(AZ_CORE_RTTI)
129 ,
130 ValueType(typeid(T))
131#endif
132 {
133 }
134 };
135
136 std::shared_ptr<ContextSharedState> m_contextSharedState;
137
138 explicit Context(std::shared_ptr<ContextSharedState> impl)
139 : m_contextSharedState(std::move(impl))
140 {
141 }
142
143 public:
148 Context() : m_contextSharedState(std::make_shared<ContextSharedState>()) {}
149
157 Context WithDeadline(DateTime const& deadline) const
158 {
159 return Context{std::make_shared<ContextSharedState>(m_contextSharedState, deadline)};
160 }
161
171 template <class T> Context WithValue(Key const& key, T&& value) const
172 {
173 return Context{std::make_shared<ContextSharedState>(
174 m_contextSharedState, (DateTime::max)(), key, std::forward<T>(value))};
175 }
176
185 DateTime GetDeadline() const;
186
201 template <class T> bool TryGetValue(Key const& key, T& outputValue) const
202 {
203 for (auto ptr = m_contextSharedState; ptr; ptr = ptr->Parent)
204 {
205 if (ptr->Key == key)
206 {
207#if defined(AZ_CORE_RTTI)
209 typeid(T) == ptr->ValueType, "Type mismatch for Context::TryGetValue().");
210#endif
211
212 outputValue = *reinterpret_cast<const T*>(ptr->Value.get());
213 return true;
214 }
215 }
216 return false;
217 }
218
223 void Cancel()
224 {
225 m_contextSharedState->Deadline
226 = ContextSharedState::ToDateTimeRepresentation((DateTime::min)());
227 }
228
233 bool IsCancelled() const { return GetDeadline() < std::chrono::system_clock::now(); }
234
240 void ThrowIfCancelled() const
241 {
242 if (IsCancelled())
243 {
244 throw OperationCancelledException("Request was cancelled by context.");
245 }
246 }
247
252 static const AZ_CORE_DLLEXPORT Context ApplicationContext;
253 };
254}} // 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:51
bool operator!=(Key const &other) const
Compares with other Key for equality.
Definition context.hpp:76
bool operator==(Key const &other) const
Compares with other Key for equality.
Definition context.hpp:66
Key()
Constructs a default instance of Key.
Definition context.hpp:59
A context is a node within a tree that represents deadlines and key/value pairs.
Definition context.hpp:46
void ThrowIfCancelled() const
Checks if the context is cancelled.
Definition context.hpp:240
void Cancel()
Cancels the context.
Definition context.hpp:223
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:201
Context WithDeadline(DateTime const &deadline) const
Creates a context with a deadline.
Definition context.hpp:157
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:171
static const AZ_CORE_DLLEXPORT Context ApplicationContext
The application context (root).
Definition context.hpp:252
Context()
Constructs a new context with no deadline, and no value associated.
Definition context.hpp:148
bool IsCancelled() const
Checks if the context is cancelled.
Definition context.hpp:233
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:33
OperationCancelledException(std::string const &what)
Constructs an OperationCancelledException with message string as the description.
Definition context.hpp:40
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:423
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