BitShares-Core  6.1.0
BitShares blockchain implementation and command-line interface software
thread_specific.hpp
Go to the documentation of this file.
1 #pragma once
2 #include "thread.hpp"
3 
4 namespace fc
5 {
6  namespace detail
7  {
10  }
11 
12  template <typename T>
14  {
15  private:
16  unsigned slot;
17  public:
20  {}
21 
22  T* get() const
23  {
24  return static_cast<T*>(detail::get_thread_specific_data(slot));
25  }
26  T* operator->() const
27  {
28  return get();
29  }
30  T& operator*() const
31  {
32  return *get();
33  }
34  operator bool() const
35  {
36  return get() != static_cast<T*>(0);
37  }
38  static void cleanup_function(void* obj)
39  {
40  delete static_cast<T*>(obj);
41  }
42  void reset(T* new_value = 0)
43  {
44  detail::set_thread_specific_data(slot, new_value, cleanup_function);
45  }
46  };
47 
48  template <typename T>
50  {
51  private:
52  unsigned slot;
53  public:
55  slot(detail::get_next_unused_task_storage_slot())
56  {}
57 
58  T* get() const
59  {
60  return static_cast<T*>(detail::get_task_specific_data(slot));
61  }
62  T* operator->() const
63  {
64  return get();
65  }
66  T& operator*() const
67  {
68  return *get();
69  }
70  operator bool() const
71  {
72  return get() != static_cast<T*>(0);
73  }
74  static void cleanup_function(void* obj)
75  {
76  delete static_cast<T*>(obj);
77  }
78  void reset(T* new_value = 0)
79  {
80  detail::set_task_specific_data(slot, new_value, cleanup_function);
81  }
82  };
83 
84 }
void set_task_specific_data(unsigned slot, void *new_value, void(*cleanup)(void *))
unsigned get_next_unused_task_storage_slot()
void * get_task_specific_data(unsigned slot)
void reset(T *new_value=0)
void * get_thread_specific_data(unsigned slot)
void reset(T *new_value=0)
unsigned get_next_unused_thread_storage_slot()
Definition: api.hpp:15
static void cleanup_function(void *obj)
void set_thread_specific_data(unsigned slot, void *new_value, void(*cleanup)(void *))
static void cleanup_function(void *obj)