xtd 0.2.0
thread_local_object.h
Go to the documentation of this file.
1 #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 
14 namespace xtd {
16  namespace threading {
37  template<typename value_t>
38  class thread_local_object : public object {
39  struct data : public object {
41  bool track_all_values = false;
42  func<value_t> value_factory;
43  std::map<intptr, value_t> values;
44  };
45 
46  struct lock_guard_mutex {
47  lock_guard_mutex(mutex& mutex) : mutex_(mutex) {mutex_.lock();}
48  ~lock_guard_mutex() {mutex_.unlock();}
49  private:
50  mutex& mutex_;
51  };
52 
53  public:
55 
60  thread_local_object() = default;
64  explicit thread_local_object(bool track_all_values) {data_->track_all_values = track_all_values;}
67  explicit thread_local_object(const func<value_t>& value_factory) {data_->value_factory = value_factory;}
72  thread_local_object(const func<value_t>& value_factory, bool track_all_values) {
73  data_->value_factory = value_factory;
74  data_->track_all_values = track_all_values;
75  }
77 
79  template<typename func_t>
80  explicit thread_local_object(func_t value_factory) : thread_local_object(func<value_t> {value_factory}) {}
81  template<typename func_t>
82  explicit thread_local_object(func_t value_factory, bool track_all_values) : thread_local_object(func<value_t> {value_factory}, track_all_values) {}
84  thread_local_object(const thread_local_object&) = default;
85  thread_local_object& operator =(const thread_local_object& other) = default;
87 
89 
93  bool is_value_created() const noexcept {
94  lock_guard_mutex lock {data_->mutex};
95  return data_->values.find(thread::current_thread().thread_id()) != data_->values.end();
96  }
97 
101  value_t value() const noexcept {
102  lock_guard_mutex lock {data_->mutex};
103  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();
104  return data_->values.find(thread::current_thread().thread_id())->second;
105  }
106 
110  void value(value_t value) noexcept {
111  lock_guard_mutex lock {data_->mutex};
112  data_->values[thread::current_thread().thread_id()] = value;
113  }
114 
119  std::vector<value_t> values() const {
120  if (!data_->track_all_values) throw invalid_operation_exception(csf_);
121  lock_guard_mutex lock {data_->mutex};
122  auto values = std::vector<value_t> {};
123  for (const auto& entry : data_->values)
124  values.push_back(entry.second);
125  return values;
126  }
128 
130 
132  ustring to_string() const noexcept override {
133  return ustring::format("{}", value());
134  }
136 
137  private:
138  mutable std::shared_ptr<data> data_ = std::make_shared<data>();
139  };
140  }
141 }
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:119
static thread & current_thread()
Gets the currently running thread.
Provides a mechanism that synchronizes access to objects with xtd::threading::monitor.
Definition: lock_guard.h:30
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:67
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
A synchronization primitive that can also be used for interprocess synchronization.
Definition: mutex.h:48
void value(value_t value) noexcept
Sets the value of this instance for the current thread.
Definition: thread_local_object.h:110
#define csf_
Provides information about the current stack frame.
Definition: current_stack_frame.h:30
ustring to_string() const noexcept override
Returns a sxd::ustring that represents the current object.
Definition: thread_local_object.h:132
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
Contains xtd::threading::thread class.
intptr thread_id() const noexcept
Gets the native operating system thread id.
Provides thread-local storage of data.
Definition: thread_local_object.h:38
value_t value() const noexcept
Gets the value of this instance for the current thread.
Definition: thread_local_object.h:101
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:93
Contains xtd::threading::mutex exception.
The operating system is other.
The exception that is thrown when a method call is invalid for the object&#39;s current state...
Definition: invalid_operation_exception.h:18
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes...
Definition: object.h:32
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:64
thread_local_object()=default
Initializes the xtd::threading::thread_local_object instance.
Represents a delegate, which is a data structure that refers to a static method or to a class instanc...
Definition: delegate.h:362
static ustring format(const ustring &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition: ustring.h:744
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:72