wxWidgets Observable Library
observable_evt.h
1 /*************************************************************************
2  *
3  * Project: OpenCPN
4  * Purpose: wxCommandEvt subclass which can carry also a shared_ptr<void>
5  *
6  * Copyright (C) 2022 Alec Leamas
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the
20  * Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  **************************************************************************/
23 
24 #ifndef OBSERVABLE_EVT_H // Guard also used in ocpn_plugin.h
25 #define OBSERVABLE_EVT_H
26 
27 #include <memory>
28 
29 #include <wx/event.h>
30 
32 class ObservedEvt;
33 
34 wxDECLARE_EVENT(obsNOTIFY, ObservedEvt);
35 
37 class ObservedEvt : public wxCommandEvent {
38 public:
40  ObservedEvt(wxEventType commandType = obsNOTIFY, int id = 0)
41  : wxCommandEvent(commandType, id) {}
42 
44  ObservedEvt(const ObservedEvt& event) : wxCommandEvent(event) {
45  this->m_shared_ptr = event.m_shared_ptr;
46  }
47 
49  wxEvent* Clone() const { return new ObservedEvt(*this); }
50 
52  std::shared_ptr<const void> GetSharedPtr() const { return m_shared_ptr; }
53 
55  void SetSharedPtr(std::shared_ptr<const void> p) { m_shared_ptr = p; }
56 
57 private:
58  std::shared_ptr<const void> m_shared_ptr;
59 };
60 
61 #endif // OBSERVABLE_EVT_H
Adds a std::shared<void> element to wxCommandEvent.
ObservedEvt(const ObservedEvt &event)
Copy operation.
wxEvent * Clone() const
Return a shallow copy of this.
ObservedEvt(wxEventType commandType=obsNOTIFY, int id=0)
Construct an event using given commandType and id.
void SetSharedPtr(std::shared_ptr< const void > p)
Set a shared_ptr transferred to listeners.
std::shared_ptr< const void > GetSharedPtr() const
Retrieve pointer set by SetSharedPtr().