ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
core/system/Leaf.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2023 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include <cassert>
23#include <memory>
24
25namespace System {
26
27class System;
28
29/**
30 * @brief Abstract class that represents a component of the system.
31 *
32 * See @ref SystemClassDesign for more details.
33 */
34template <typename Class> class Leaf {
35protected:
36 std::weak_ptr<System> m_system;
37
38 auto &get_system() {
39 auto const ptr = m_system.lock();
40 assert(ptr);
41 return *ptr;
42 }
43
44 auto &get_system() const {
45 auto const ptr = m_system.lock();
46 assert(ptr);
47 return *ptr;
48 }
49
50public:
51 void bind_system(std::shared_ptr<System> const &system) {
52 assert(system);
53 assert(m_system.expired() or m_system.lock() == system);
54 m_system = system;
55 }
56
57 void detach_system([[maybe_unused]] std::shared_ptr<System> const &system) {
58 assert(system);
59 assert(not m_system.expired());
60 assert(system == m_system.lock());
61 m_system.reset();
62 }
63};
64
65} // namespace System
Abstract class that represents a component of the system.
void bind_system(std::shared_ptr< System > const &system)
auto & get_system() const
void detach_system(std::shared_ptr< System > const &system)
std::weak_ptr< System > m_system