CFx SDK Documentation  2022 SP0
tracked_connect.h
Go to the documentation of this file.
1 //
2 // tracked_connect.h
3 // slimsig
4 //
5 // Created by Christopher Tarquini on 4/22/14.
6 //
7 //
8 
9 #ifndef slimsig_tracked_connect_h
10 #define slimsig_tracked_connect_h
11 #include <memory>
12 #include <vector>
13 #include <initializer_list>
14 #include <algorithm>
15 #include <type_traits>
16 #include <functional>
17 namespace slimsig {
18  template <class T, class Observer, class BaseAllocator = std::allocator<T>>
19  class trackable_allocator : private BaseAllocator {
20  using allocator_traits = std::allocator_traits<BaseAllocator>;
21  using base = BaseAllocator;
22  template<class U>
23  using parent_rebind_traits_t = typename allocator_traits::template rebind_traits<U>;
24  template <class U>
25  using parent_rebind_t = typename parent_rebind_traits_t<U>::allocator_type;
26  public:
27  using typename allocator_traits::value_type;
28  using typename allocator_traits::pointer;
29  using typename allocator_traits::const_pointer;
30  using typename allocator_traits::void_pointer;
31  using typename allocator_traits::const_void_pointer;
32  using typename allocator_traits::difference_type;
33  using typename allocator_traits::size_type;
34  using typename allocator_traits::propagate_on_container_copy_assignment;
35  using typename allocator_traits::propagate_on_container_move_assignment;
36  using typename allocator_traits::propagate_on_container_swap;
37  trackable_allocator() : base() {};
38  template <class U, class O = Observer>
39  trackable_allocator(const trackable_allocator<U, Observer>& other) : base(other), m_observer(other.m_observer) {};
40  using base::allocate;
41  using base::deallocate;
42  using base::construct;
43 
44  template <class U>
45  void destroy(U* p) {
46  m_observer(p);
47  allocator_traits::destroy(*this, p);
48  }
49  private:
50  Observer m_observer;
51  };
52  template <class T, class Observer, class Deleter = std::default_delete<T>>
54  constexpr trackable_delete() noexcept = default;
55  template <class U, class OtherObserver = Observer, class OtherDeleter = Deleter, std::enable_if<std::is_convertible<U*, T*>::value, bool> = true>
56  trackable_delete(const trackable_delete<U, OtherObserver, OtherDeleter>& other) : m_observer(other.m_observer), m_deleter(m_deleter) {};
57  trackable_delete(Observer observer = Observer{}, Deleter deleter = Deleter{}) : m_observer(std::move(observer)), m_deleter(std::move(deleter)){};
62  void operator()(T* ptr) const {
63  m_observer(ptr);
64  m_deleter(ptr);
65  }
66  const Deleter& get_deleter() const {
67  return m_deleter;
68  }
69  Deleter& get_deleter() {
70  return m_deleter;
71  }
72  const Observer& get_observer() const {
73  return m_observer;
74  }
75  Observer& get_observer() {
76  return m_observer;
77  }
78  private:
79  Observer m_observer;
80  Deleter m_deleter;
81  };
82 
83  template <class T, class Observer, class Deleter = std::default_delete<T>>
84  using trackable_ptr = std::unique_ptr<T, trackable_delete<T, Observer, Deleter>>;
85 
86  template <class T, class Observer, class Deleter = std::default_delete<T>, class TrackableDeleter = trackable_delete<T, Observer, Deleter>, class... Args>
87  std::shared_ptr<T> make_trackable(TrackableDeleter deleter, Args&&... args) {
88  return std::shared_ptr<T> ( deleter, T{std::forward<Args>(args)...} );
89  }
90  template <class T, class Observer, class Deleter = std::default_delete<T>, class TrackableDeleter = trackable_delete<T, Observer, Deleter>, class... Args>
91  std::shared_ptr<T> make_trackable(Observer observer, Args&&... args) {
92  return std::shared_ptr<T> ( TrackableDeleter { observer }, T{std::forward<Args>(args)...} );
93  }
94  template <class T, class Observer, class Deleter = std::default_delete<T>, class TrackableDeleter = trackable_delete<T, Observer, Deleter>, class... Args>
95  std::shared_ptr<T> make_trackable(Observer observer, Deleter deleter, Args&&... args) {
96  return std::shared_ptr<T> ( TrackableDeleter { observer, deleter }, T{std::forward<Args>(args)...} );
97  }
98  template <class T, class Observer, class Allocator = std::allocator<T>, class TrackableAllocator = trackable_allocator<T, Observer, Allocator>, class... Args>
99  std::shared_ptr<T> allocate_trackable(TrackableAllocator allocator, Args&&... args) {
100  return std::allocate_shared<T>(allocator, std::forward<Args>(args)...);
101  }
102  template <class T, class Observer, class Allocator = std::allocator<T>, class TrackableAllocator = trackable_allocator<T, Observer, Allocator>, class... Args>
103  std::shared_ptr<T> allocate_trackable(Observer observer, Args&&... args) {
104  return std::allocate_shared<T>(TrackableAllocator {observer}, std::forward<Args>(args)...);
105  }
106  template <class T, class Observer, class Allocator = std::allocator<T>, class TrackableAllocator = trackable_allocator<T, Observer, Allocator>, class... Args>
107  std::shared_ptr<T> allocate_trackable(Observer observer, Allocator allocator, Args&&... args) {
108  return std::allocate_shared<T>(TrackableAllocator {observer, allocator}, std::forward<Args>(args)...);
109  }
110 
111 
112  template <class T>
114  public:
115  using weak_ptr_type = std::weak_ptr<T>;
116  trackable_lock(std::vector<std::weak_ptr<T>> trackable_objects) : m_tracking(std::move(trackable_objects)) {};
117  trackable_lock(std::initializer_list<std::weak_ptr<T>> trackable_list) : m_tracking{std::move(trackable_list)} {}
118  /*template <class Iterator>
119  weak_ptr(Iterator begin, Iterator end) : m_tracking(begin, end) {}*/
120 
121  bool try_lock() {
122  m_locked.reserve(m_tracking.size());
123 
124  }
125  private:
126  std::vector<std::weak_ptr<T>> m_tracking;
127  std::vector<std::shared_ptr<T>> m_locked;
128  };
129 }
130 
131 #endif
true
Definition: DimVarDefs.h:2046
trackable_allocator(const trackable_allocator< U, Observer > &other)
std::weak_ptr< T > weak_ptr_type
trackable_lock(std::initializer_list< std::weak_ptr< T >> trackable_list)
trackable_lock(std::vector< std::weak_ptr< T >> trackable_objects)
GLenum void ** pointer
Definition: gles2_ext.h:288
GLsizei const GLfloat * value
Definition: gles2_ext.h:302
std::unique_ptr< T, trackable_delete< T, Observer, Deleter > > trackable_ptr
std::shared_ptr< T > make_trackable(TrackableDeleter deleter, Args &&... args)
std::shared_ptr< T > allocate_trackable(TrackableAllocator allocator, Args &&... args)
void operator()(T *ptr) const
trackable_delete & operator=(const trackable_delete &)=default
trackable_delete(const trackable_delete &)=default
trackable_delete & operator=(trackable_delete &&)=default
trackable_delete(Observer observer=Observer{}, Deleter deleter=Deleter{})
trackable_delete(trackable_delete &&)=default
constexpr trackable_delete() noexcept=default
const Deleter & get_deleter() const
const Observer & get_observer() const