Estella 0.113.0
A fast 2D and 3D game engine powered by WebAssembly and ECS
Loading...
Searching...
No Matches
Connection.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
16
17#pragma once
18
19// =============================================================================
20// Includes
21// =============================================================================
22
23#include "../core/Types.hpp"
24
25#include <functional>
26
27namespace esengine {
28
29// =============================================================================
30// Type Definitions
31// =============================================================================
32
35
36// =============================================================================
37// Connection Class
38// =============================================================================
39
58public:
59 using DisconnectFunc = std::function<void(CallbackId)>;
60
64 Connection() = default;
65
72 : id_(id), disconnectFunc_(std::move(disconnectFunc)) {}
73
78 disconnect();
79 }
80
85 Connection(Connection&& other) noexcept
86 : id_(other.id_), disconnectFunc_(std::move(other.disconnectFunc_)) {
87 other.id_ = INVALID_CALLBACK_ID;
88 other.disconnectFunc_ = nullptr;
89 }
90
96 Connection& operator=(Connection&& other) noexcept {
97 if (this != &other) {
98 disconnect();
99 id_ = other.id_;
100 disconnectFunc_ = std::move(other.disconnectFunc_);
101 other.id_ = INVALID_CALLBACK_ID;
102 other.disconnectFunc_ = nullptr;
103 }
104 return *this;
105 }
106
107 Connection(const Connection&) = delete;
108 Connection& operator=(const Connection&) = delete;
109
113 void disconnect() {
114 if (isConnected() && disconnectFunc_) {
115 disconnectFunc_(id_);
117 disconnectFunc_ = nullptr;
118 }
119 }
120
125 [[nodiscard]] bool isConnected() const {
126 return id_ != INVALID_CALLBACK_ID;
127 }
128
133 [[nodiscard]] CallbackId getId() const {
134 return id_;
135 }
136
142 CallbackId id = id_;
144 disconnectFunc_ = nullptr;
145 return id;
146 }
147
148private:
150 DisconnectFunc disconnectFunc_ = nullptr;
151};
152
160public:
161 ScopedConnection() = default;
162
164 : connection_(std::move(conn)) {}
165
166 ScopedConnection(ScopedConnection&&) noexcept = default;
167 ScopedConnection& operator=(ScopedConnection&&) noexcept = default;
168
170 ScopedConnection& operator=(const ScopedConnection&) = delete;
171
172 [[nodiscard]] bool isConnected() const {
173 return connection_.isConnected();
174 }
175
176 void disconnect() {
177 connection_.disconnect();
178 }
179
181 return connection_;
182 }
183
184private:
185 Connection connection_;
186};
187
195public:
196 ConnectionHolder() = default;
197 ~ConnectionHolder() = default;
198
199 ConnectionHolder(ConnectionHolder&&) noexcept = default;
200 ConnectionHolder& operator=(ConnectionHolder&&) noexcept = default;
201
203 ConnectionHolder& operator=(const ConnectionHolder&) = delete;
204
209 void add(Connection&& conn) {
210 connections_.push_back(std::move(conn));
211 }
212
217 connections_.clear();
218 }
219
224 [[nodiscard]] usize size() const {
225 return connections_.size();
226 }
227
232 [[nodiscard]] bool empty() const {
233 return connections_.empty();
234 }
235
236private:
237 std::vector<Connection> connections_;
238};
239
240} // namespace esengine
Core type definitions and utilities for ESEngine.
bool empty() const
Check if there are any connections.
void disconnectAll()
Disconnect all managed connections.
ConnectionHolder(ConnectionHolder &&) noexcept=default
usize size() const
Get the number of active connections.
void add(Connection &&conn)
Add a connection to manage.
RAII handle for signal connections.
std::function< void(CallbackId)> DisconnectFunc
Connection(Connection &&other) noexcept
Move constructor transfers ownership.
Connection(const Connection &)=delete
Connection(CallbackId id, DisconnectFunc disconnectFunc)
Constructs a connection with disconnect callback.
CallbackId release()
Release ownership without disconnecting.
bool isConnected() const
Check if still connected.
~Connection()
Destructor automatically disconnects.
Connection()=default
Default constructor creates an empty connection.
Connection & operator=(const Connection &)=delete
Connection & operator=(Connection &&other) noexcept
Move assignment transfers ownership.
void disconnect()
Manually disconnect from the signal.
CallbackId getId() const
Get the callback ID.
ScopedConnection(ScopedConnection &&) noexcept=default
ScopedConnection(Connection &&conn)
constexpr CallbackId INVALID_CALLBACK_ID
std::uint64_t u64
Unsigned 64-bit integer.
Definition Types.hpp:47
std::size_t usize
Size type for indexing and sizes.
Definition Types.hpp:64
u64 CallbackId