Jamba C++ API 7.5.0
Loading...
Searching...
No Matches
SelfContainedViewListener.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2019 pongasoft
3 *
4 * Licensed under the Apache License, Version 2.0 or the MIT license,
5 * at your option. You may not use this file except in compliance with
6 * one of these licenses. You may obtain copies of the licenses at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 * https://opensource.org/licenses/MIT
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations under
15 * the License.
16 *
17 * @author Yan Pujante
18 */
19#pragma once
20
21#include <vstgui4/vstgui/lib/iviewlistener.h>
22#include <vstgui4/vstgui/lib/cview.h>
23#include <pongasoft/logging/logging.h>
24
26
27using namespace VSTGUI;
28
86class SelfContainedViewListener : protected ViewListenerAdapter, public std::enable_shared_from_this<SelfContainedViewListener>
87{
88public:
99 template<typename T, typename ...Args>
100 static std::shared_ptr<T> create(CView *iView, Args&& ...iArgs)
101 {
102 // ensures that TView is a subclass of SelfContainedViewListener
103 static_assert(std::is_convertible<T *, SelfContainedViewListener*>::value, "T must be a subclass of SelfContainedViewListener");
104
105 auto res = std::make_shared<T>(std::forward<Args>(iArgs)...);
106 res->registerView(iView);
107 return res;
108 }
109
115 virtual void unregister()
116 {
117 if(fView)
118 {
119 fView->unregisterViewListener(this);
120 fView = nullptr;
121 fThis = nullptr;
122 }
123 }
124
130
135 virtual std::shared_ptr<SelfContainedViewListener> registerView(CView *iView)
136 {
137 DCHECK_F(iView != nullptr);
138
139 // first we close any prior connection if there was one
140 unregister();
141
142 fView = iView;
143 fView->registerViewListener(this);
144 fThis = shared_from_this();
145
146 return fThis;
147 }
148
149protected:
150
155 void viewWillDelete(CView *iView) override
156 {
157 DCHECK_F(iView == fView);
158
159 unregister();
160 }
161
162protected:
163 std::shared_ptr<SelfContainedViewListener> fThis{};
164 CView *fView{};
165};
166
167}
static std::shared_ptr< T > create(CView *iView, Args &&...iArgs)
This is the main method that should be used to create an instance of this class.
Definition SelfContainedViewListener.h:100
SelfContainedViewListener()=default
The constructor which unfortunately has to be declared public for the purpose of a creating a shared ...
CView * fView
Definition SelfContainedViewListener.h:164
virtual std::shared_ptr< SelfContainedViewListener > registerView(CView *iView)
Registers this class as a view listener.
Definition SelfContainedViewListener.h:135
void viewWillDelete(CView *iView) override
Called by the view itself when it is going to be deleted.
Definition SelfContainedViewListener.h:155
virtual void unregister()
You can call this method at anytime to unregister this class as the listener to the view that was pre...
Definition SelfContainedViewListener.h:115
std::shared_ptr< SelfContainedViewListener > fThis
Definition SelfContainedViewListener.h:163
Definition CustomController.h:25