ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
script_interface/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
23
25
26#include <cassert>
27#include <memory>
28
29namespace ScriptInterface {
30namespace System {
31
32/**
33 * @brief Script interface wrapper for a component of the system class.
34 *
35 * This class manages a core leaf object. A leaf can exist without a system,
36 * in which case the managed object cannot be fully initialized
37 * and has nothing to act upon.
38 * Binding a leaf to a system triggers an initialization of the managed object.
39 * Detaching a leaf may trigger a memory deallocation of the managed object
40 * resources, without deallocating the managed object itself.
41 * This behavior can be leveraged to implement move semantics.
42 * See @ref SystemClassDesign for more details.
43 */
44class Leaf : public ObjectHandle {
45 /** @brief Callback triggered upon binding a leaf to a system. */
47 /** @brief Callback triggered upon detaching a leaf from a system. */
49
50protected:
51 std::weak_ptr<::System::System> m_system;
52
53 auto const &get_system() const {
54 auto const ptr = m_system.lock();
55 assert(ptr);
56 return *ptr;
57 }
58
59 auto &get_system() {
60 auto const ptr = m_system.lock();
61 assert(ptr);
62 return *ptr;
63 }
64
65public:
66 void bind_system(std::shared_ptr<::System::System> const &system) {
67 assert(m_system.expired() or m_system.lock() == system);
70 }
71
73 auto const ptr = m_system.lock();
74 assert(ptr != nullptr);
75 on_detach_system(*ptr);
76 m_system.reset();
77 }
78};
79
80} // namespace System
81} // namespace ScriptInterface
Base class for interface handles.
Script interface wrapper for a component of the system class.
virtual void on_detach_system(::System::System &)
Callback triggered upon detaching a leaf from a system.
std::weak_ptr<::System::System > m_system
void bind_system(std::shared_ptr<::System::System > const &system)
virtual void on_bind_system(::System::System &)
Callback triggered upon binding a leaf to a system.
Main system class.
T get_value(Variant const &v)
Extract value of specific type T from a Variant.