Jamba C++ API  4.3.0
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 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  *
16  * @author Yan Pujante
17  */
18 #pragma once
19 
20 #include <vstgui4/vstgui/lib/iviewlistener.h>
21 #include <vstgui4/vstgui/lib/cview.h>
22 #include <pongasoft/logging/logging.h>
23 
25 
26 using namespace VSTGUI;
27 
85 class SelfContainedViewListener : protected IViewListenerAdapter, public std::enable_shared_from_this<SelfContainedViewListener>
86 {
87 public:
98  template<typename T, typename ...Args>
99  static std::shared_ptr<T> create(CView *iView, Args&& ...iArgs)
100  {
101  // ensures that TView is a subclass of CView
102  static_assert(std::is_convertible<T *, SelfContainedViewListener*>::value, "T must be a subclass of SelfContainedViewListener");
103 
104  auto res = std::make_shared<T>(std::forward<Args>(iArgs)...);
105  res->registerView(iView);
106  return res;
107  }
108 
114  virtual void unregister()
115  {
116  if(fView)
117  {
118  fView->unregisterViewListener(this);
119  fView = nullptr;
120  fThis = nullptr;
121  }
122  }
123 
128  SelfContainedViewListener() = default;
129 
134  virtual std::shared_ptr<SelfContainedViewListener> registerView(CView *iView)
135  {
136  DCHECK_F(iView != nullptr);
137 
138  // first we close any prior connection if there was one
139  unregister();
140 
141  fView = iView;
142  fView->registerViewListener(this);
143  fThis = shared_from_this();
144 
145  return fThis;
146  }
147 
148 protected:
149 
154  void viewWillDelete(CView *iView) override
155  {
156  DCHECK_F(iView == fView);
157 
158  unregister();
159  }
160 
161 protected:
162  std::shared_ptr<SelfContainedViewListener> fThis{};
163  CView *fView{};
164 };
165 
166 }
void viewWillDelete(CView *iView) override
Called by the view itself when it is going to be deleted.
Definition: SelfContainedViewListener.h:154
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:114
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:99
Definition: Types.h:29
Definition: CustomController.h:24
virtual std::shared_ptr< SelfContainedViewListener > registerView(CView *iView)
Registers this class as a view listener.
Definition: SelfContainedViewListener.h:134
The purpose of this class is to implement a view listener that is "self-contained":
Definition: SelfContainedViewListener.h:85