MarlinMT  0.1.0
EventStore.h
Go to the documentation of this file.
1 #pragma once
2 
3 // -- std headers
4 #include <string>
5 #include <memory>
6 #include <typeindex>
7 
8 // -- marlinmt headers
9 #include <marlinmt/Extensions.h>
10 
11 namespace marlinmt {
12 
17  class EventStore {
18  public:
19  EventStore() = default ;
20  ~EventStore() = default ;
21  EventStore(const EventStore &) = delete ;
22  EventStore &operator=(const EventStore &) = delete ;
23  EventStore(EventStore &&) = default ;
24  EventStore &operator=(EventStore &&) = default ;
25 
31  void setUID( std::size_t uid ) ;
32 
36  std::size_t uid() const ;
37 
41  template <typename T>
42  std::shared_ptr<T> event() const ;
43 
48  template <typename T>
49  std::shared_ptr<T> takeEvent() ;
50 
54  template <typename T>
55  bool isType() const ;
56 
60  const std::type_index &type() const ;
61 
67  template <typename T>
68  void setEvent( std::shared_ptr<T> evt ) ;
69 
77  template <typename T>
78  void setBareEvent( T *evt, bool owner ) ;
79 
85  template <typename T, typename ...Args>
86  std::shared_ptr<T> allocate(Args && ...args) ;
87 
91  void reset() ;
92 
97 
101  const Extensions &extensions() const ;
102 
103  private:
105  std::size_t _uid {0} ;
107  std::shared_ptr<void> _event {nullptr} ;
109  std::type_index _eventType {typeid(nullptr)} ;
112  };
113 
114  //--------------------------------------------------------------------------
115  //--------------------------------------------------------------------------
116 
117  inline void EventStore::setUID( std::size_t id ) {
118  _uid = id ;
119  }
120 
121  //--------------------------------------------------------------------------
122 
123  inline std::size_t EventStore::uid() const {
124  return _uid ;
125  }
126 
127  //--------------------------------------------------------------------------
128 
129  template <typename T>
130  inline std::shared_ptr<T> EventStore::event() const {
131  return std::static_pointer_cast<T>( _event ) ;
132  }
133 
134  //--------------------------------------------------------------------------
135 
136  template <typename T>
137  inline std::shared_ptr<T> EventStore::takeEvent() {
138  _eventType = std::type_index( typeid(nullptr) ) ;
139  return std::static_pointer_cast<T>( _event ) ;
140  }
141 
142  //--------------------------------------------------------------------------
143 
144  template <typename T>
145  inline bool EventStore::isType() const {
146  // FIXME: use std::decay_t
147  return (std::type_index(typeid(T)) == _eventType) ;
148  }
149 
150  //--------------------------------------------------------------------------
151 
152  inline const std::type_index &EventStore::type() const {
153  return _eventType ;
154  }
155 
156  //--------------------------------------------------------------------------
157 
158  template <typename T>
159  inline void EventStore::setEvent( std::shared_ptr<T> evt ) {
160  _eventType = std::type_index(typeid(T)) ;
161  _event = evt ;
162  }
163 
164  //--------------------------------------------------------------------------
165 
166  template <typename T>
167  inline void EventStore::setBareEvent( T *evt, bool owner ) {
168  if( owner ) {
169  _event = std::shared_ptr<T>( evt ) ;
170  }
171  else {
172  _event = std::shared_ptr<T>( evt, [](T *ptr){/* nop */} ) ;
173  }
174  _eventType = std::type_index(typeid(T)) ;
175  }
176 
177  //--------------------------------------------------------------------------
178 
179  template <typename T, typename ...Args>
180  inline std::shared_ptr<T> EventStore::allocate(Args && ...args) {
181  auto ptr = std::make_shared<T>( args... ) ;
182  setEvent( ptr ) ;
183  return ptr ;
184  }
185 
186  //--------------------------------------------------------------------------
187 
188  inline void EventStore::reset() {
189  _eventType = std::type_index(typeid(nullptr)) ;
190  _event.reset() ;
191  }
192 
193  //--------------------------------------------------------------------------
194 
196  return _extensions ;
197  }
198 
199  //--------------------------------------------------------------------------
200 
201  inline const Extensions &EventStore::extensions() const {
202  return _extensions ;
203  }
204 
205 }
Extensions _extensions
The event extensions.
Definition: EventStore.h:111
std::shared_ptr< T > event() const
Get the underlying event to a specific type.
Definition: EventStore.h:130
Extensions class.
Definition: Extensions.h:71
std::size_t uid() const
Get the event unique id.
Definition: EventStore.h:123
std::type_index _eventType
The event implementtion type.
Definition: EventStore.h:109
std::size_t _uid
Definition: EventStore.h:105
void setEvent(std::shared_ptr< T > evt)
Set the event pointer using a shared pointer.
Definition: EventStore.h:159
void reset()
Reset the internal pointer.
Definition: EventStore.h:188
bool isType() const
Check whether the stored event is of a given type.
Definition: EventStore.h:145
EventStore & operator=(const EventStore &)=delete
void setBareEvent(T *evt, bool owner)
Set the event pointer using a bare pointer and an ownership flag.
Definition: EventStore.h:167
Extensions & extensions()
Access the event extensions.
Definition: EventStore.h:195
std::shared_ptr< void > _event
The underlying event store implementation.
Definition: EventStore.h:107
const std::type_index & type() const
Get the event type.
Definition: EventStore.h:152
std::shared_ptr< T > takeEvent()
Get the underlying event to a specific type.
Definition: EventStore.h:137
void setUID(std::size_t uid)
Set the event unique id.
Definition: EventStore.h:117
std::shared_ptr< T > allocate(Args &&...args)
Allocate a new event in the event store and return it.
Definition: EventStore.h:180
EventStore class.
Definition: EventStore.h:17