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 {
35private:
36 Leaf() = default;
37 friend Class;
38
39protected:
40 std::weak_ptr<System> m_system;
41
42 auto &get_system() {
43 auto const ptr = m_system.lock();
44 assert(ptr);
45 return *ptr;
46 }
47
48 auto &get_system() const {
49 auto const ptr = m_system.lock();
50 assert(ptr);
51 return *ptr;
52 }
53
54public:
55 void bind_system(std::shared_ptr<System> const &system) {
56 assert(system);
57 assert(m_system.expired() or m_system.lock() == system);
58 m_system = system;
59 }
60
61 void detach_system([[maybe_unused]] std::shared_ptr<System> const &system) {
62 assert(system);
63 assert(not m_system.expired());
64 assert(system == m_system.lock());
65 m_system.reset();
66 }
67};
68
69} // 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