simple thread
context.hpp
1 //SPDX-License-Identifier: LicenseRef-Apache-License-2.0
2 //Author: Blayne Dennis
3 
4 #ifndef __SIMPLE_THREADING_CONTEXT__
5 #define __SIMPLE_THREADING_CONTEXT__
6 
7 #include <memory>
8 #include <typeinfo>
9 
10 #include "utility.hpp"
11 
12 namespace st { // simple thread
13 
42 template <typename CRTP, typename CRTPCTX>
44 protected:
50  inline std::shared_ptr<CRTPCTX>& ctx() const {
51  return this->m_context;
52  }
53 
57  inline void ctx(std::shared_ptr<CRTPCTX> new_ctx) {
58  this->m_context = new_ctx;
59  }
60 
61 public:
62  virtual ~shared_context() { }
63 
64  inline void* get() {
65  return this->ctx().get();
66  }
67 
68  inline void reset() {
69  this->ctx().reset();
70  }
71 
75  inline operator bool() const {
76  return (bool)(this->ctx());
77  }
78 
82  inline bool operator==(const CRTP& rhs) const noexcept {
83  return this->ctx() == rhs.ctx();
84  }
85 
89  inline bool operator!=(const CRTP& rhs) const noexcept {
90  return this->ctx() != rhs.ctx();
91  }
92 
96  inline bool operator<(const CRTP& rhs) const noexcept {
97  return this->ctx() < rhs.ctx();
98  }
99 
104  inline CRTP& operator=(const CRTP& rhs) {
105  this->ctx() = rhs.ctx();
106  return *this;
107  }
108 
109 private:
110  mutable std::shared_ptr<CRTPCTX> m_context;
111 };
112 
113 }
114 
115 #endif
CRTP-templated interface to provide shared context api.
Definition: context.hpp:43
std::shared_ptr< CRTPCTX > & ctx() const
Definition: context.hpp:50
void ctx(std::shared_ptr< CRTPCTX > new_ctx)
Definition: context.hpp:57
bool operator==(const CRTP &rhs) const noexcept
Definition: context.hpp:82
bool operator!=(const CRTP &rhs) const noexcept
Definition: context.hpp:89
CRTP & operator=(const CRTP &rhs)
assign the context
Definition: context.hpp:104
bool operator<(const CRTP &rhs) const noexcept
Definition: context.hpp:96