xtd 0.2.0
Loading...
Searching...
No Matches
thread_local_object.h
Go to the documentation of this file.
1
4#pragma once
5#include "mutex.h"
6#include "thread.h"
7#include "../func.h"
8#include "../invalid_operation_exception.h"
9#include "../object.h"
10#include "../time_span.h"
11#include <map>
12
14namespace xtd {
16 namespace threading {
39 template<typename value_t>
40 class thread_local_object : public object {
41 struct data : public object {
43 bool track_all_values = false;
44 func<value_t> value_factory;
45 std::map<intptr, value_t> values;
46 };
47
48 struct lock_guard_mutex {
49 lock_guard_mutex(mutex& mutex) : mutex_(mutex) {mutex_.lock();}
50 ~lock_guard_mutex() {mutex_.unlock();}
51 private:
52 mutex& mutex_;
53 };
54
55 public:
57
66 explicit thread_local_object(bool track_all_values) {data_->track_all_values = track_all_values;}
69 explicit thread_local_object(const func<value_t>& value_factory) {data_->value_factory = value_factory;}
74 thread_local_object(const func<value_t>& value_factory, bool track_all_values) {
75 data_->value_factory = value_factory;
76 data_->track_all_values = track_all_values;
77 }
79
81 template<typename func_t>
82 explicit thread_local_object(func_t value_factory) : thread_local_object(func<value_t> {value_factory}) {}
83 template<typename func_t>
84 explicit thread_local_object(func_t value_factory, bool track_all_values) : thread_local_object(func<value_t> {value_factory}, track_all_values) {}
87 thread_local_object& operator =(const thread_local_object& other) = default;
89
91
95 bool is_value_created() const noexcept {
96 lock_guard_mutex lock {data_->mutex};
97 return data_->values.find(thread::current_thread().thread_id()) != data_->values.end();
98 }
99
103 value_t value() const noexcept {
104 lock_guard_mutex lock {data_->mutex};
105 if (!is_value_created()) data_->values[thread::current_thread().thread_id()] = data_->value_factory.is_empty() ? value_t {} : data_->values[thread::current_thread().thread_id()] = data_->value_factory();
106 return data_->values.find(thread::current_thread().thread_id())->second;
107 }
108
112 void value(value_t value) noexcept {
113 lock_guard_mutex lock {data_->mutex};
114 data_->values[thread::current_thread().thread_id()] = value;
115 }
116
121 std::vector<value_t> values() const {
122 if (!data_->track_all_values) throw invalid_operation_exception {};
123 lock_guard_mutex lock {data_->mutex};
124 auto values = std::vector<value_t> {};
125 for (const auto& entry : data_->values)
126 values.push_back(entry.second);
127 return values;
128 }
130
132
134 string to_string() const noexcept override {
135 return string::format("{}", value());
136 }
138
139 private:
140 mutable xtd::sptr<data> data_ = xtd::new_sptr<data>();
141 };
142 }
143}
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition delegate.h:370
The exception that is thrown when a method call is invalid for the object's current state.
Definition invalid_operation_exception.h:19
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition lock_guard.h:32
A synchronization primitive that can also be used for interprocess synchronization.
Definition mutex.h:50
Provides thread-local storage of data.
Definition thread_local_object.h:40
thread_local_object(bool track_all_values)
Initializes the xtd::threading::thread_local_object instance and specifies whether all values are acc...
Definition thread_local_object.h:66
thread_local_object(const func< value_t > &value_factory, bool track_all_values)
Initializes the xtd::threading::thread_local_object instance with the specified value_factory functio...
Definition thread_local_object.h:74
void value(value_t value) noexcept
Sets the value of this instance for the current thread.
Definition thread_local_object.h:112
thread_local_object()=default
Initializes the xtd::threading::thread_local_object instance.
string to_string() const noexcept override
Returns a xtd::string that represents the current object.
Definition thread_local_object.h:134
bool is_value_created() const noexcept
Gets whether xtd::threading::thread_local_object::value is initialized on the current thread.
Definition thread_local_object.h:95
value_t value() const noexcept
Gets the value of this instance for the current thread.
Definition thread_local_object.h:103
std::vector< value_t > values() const
Gets a list containing the values stored by all threads that have accessed this instance.
Definition thread_local_object.h:121
thread_local_object(const func< value_t > &value_factory)
Initializes the xtd::threading::thread_local_object instance with the specified value_factory functio...
Definition thread_local_object.h:69
intptr thread_id() const noexcept
Gets the native operating system thread id.
static thread & current_thread() noexcept
Gets the currently running thread.
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
std::shared_ptr< type_t > sptr
The xtd::sptr object is a shared pointer.
Definition sptr.h:25
delegate< result_t(arguments_t...)> func
Represents a delegate that has variables parameters and returns a value of the type specified by the ...
Definition func.h:16
@ other
The operating system is other.
Contains xtd::threading::mutex exception.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
Contains xtd::threading::thread class.