Aurore
A Switch-like interface for your app, on every screen
Loading...
Searching...
No Matches
bind.hpp
Go to the documentation of this file.
1/*
2 Copyright 2021 natinusala
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of 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,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#pragma once
18
20#include <aurore/core/view.hpp>
21
22namespace aurore
23{
24
25// Exception thrown when using a BoundView with an unknown ID for
26// the given owner.
27class ViewNotFoundException : public std::exception
28{
29 public:
30 ViewNotFoundException(View* owner, std::string searchedId);
31 ViewNotFoundException(Activity* owner, std::string searchedId);
32
33 const char* what() const noexcept;
34
35 private:
36 std::string errorMessage;
37};
38
39#define AURORE_BIND(type, name, id) aurore::BoundView<type> name = aurore::BoundView<type>(id, this)
40
41// A BoundView represents a "child" view, bound to its "parent" view class.
42// Dereferencing operator has been overridden to directly resolve the view by ID and forward the call to the view pointer for you.
43// Allows easy access of child views inside the parent class when used with the AURORE_BIND macro.
44// Does NOT do any type check - it assumes the type you give it is the same as the view you are trying to get.
45template <typename T>
47{
48 public:
49 BoundView(std::string id, View* owner)
50 : id(id)
51 , ownerView(owner)
52 {
53 }
54
55 BoundView(std::string id, Activity* owner)
56 : id(id)
57 , ownerActivity(owner)
58 {
59 }
60
62 {
63 this->resolve();
64 return this->view;
65 }
66
67 operator T*()
68 {
69 return this->getView();
70 }
71
73 {
74 return this->getView();
75 }
76
77 bool operator==(const T* value)
78 {
79 return this->getView() == value;
80 }
81
82 private:
83 std::string id = "";
84
85 T* view = nullptr;
86
87 View* ownerView = nullptr;
88 Activity* ownerActivity = nullptr;
89 unsigned generation = 0;
90
99 void resolve()
100 {
101 // An activity's content made again (hot reload) has views of its own
102 if (this->view && this->ownerActivity && this->generation != this->ownerActivity->getContentGeneration())
103 this->view = nullptr;
104
105 if (this->view)
106 return;
107
108 // Resolve by owner activity first
109 if (this->ownerView)
110 {
111 this->view = dynamic_cast<T*>(this->ownerView->getView(this->id));
112
113 if (!this->view)
114 throw ViewNotFoundException(this->ownerView, this->id);
115 }
116 // Then resolve by owner view
117 else if (this->ownerActivity)
118 {
119 this->view = (T*)this->ownerActivity->getView(this->id);
120 this->generation = this->ownerActivity->getContentGeneration();
121
122 if (!this->view)
123 throw ViewNotFoundException(this->ownerActivity, this->id);
124 }
125 else
126 {
127 throw std::logic_error("No owner view or activity given to BoundView");
128 }
129 }
130};
131
132} // namespace aurore
Definition activity.hpp:45
unsigned getContentGeneration() const
Definition activity.hpp:100
View * getView(std::string id)
Definition bind.hpp:47
BoundView(std::string id, Activity *owner)
Definition bind.hpp:55
bool operator==(const T *value)
Definition bind.hpp:77
T * getView()
Definition bind.hpp:61
BoundView(std::string id, View *owner)
Definition bind.hpp:49
T * operator->()
Definition bind.hpp:72
Definition bind.hpp:28
ViewNotFoundException(View *owner, std::string searchedId)
ViewNotFoundException(Activity *owner, std::string searchedId)
const char * what() const noexcept
Definition view.hpp:232
virtual View * getView(std::string id)
Definition actions.hpp:25