Tissue Forge C++ 0.2.1
Interactive, particle-based physics, chemistry and biology modeling and simulation environment
Loading...
Searching...
No Matches
tfEvent.h
Go to the documentation of this file.
1/*******************************************************************************
2 * This file is part of Tissue Forge.
3 * Copyright (c) 2022-2024 T.J. Sego
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 ******************************************************************************/
19
25#ifndef _SOURCE_EVENT_TFEVENT_H_
26#define _SOURCE_EVENT_TFEVENT_H_
27
28#include <TissueForge_private.h>
29
30#include <forward_list>
31#include <iostream>
32
33
35
36
37 template<typename event_t> using EventMethodT = HRESULT (*)(const event_t&);
38
39 // Flags for providing feedback during predicate and invoke evaluations
40 enum class EventFlag : unsigned int {
41 REMOVE
42 };
43
44 // Base class of all events
45 struct CAPI_EXPORT EventBase {
46
47 // Flags set by invoke and predicate to provide feedback
48 std::forward_list<EventFlag> flags;
49
53 FloatP_t last_fired;
54
59
66 virtual HRESULT predicate() = 0;
67
73 virtual HRESULT invoke() = 0;
74
75 EventBase() :
76 last_fired(0.0),
77 times_fired(0)
78 {}
79 virtual ~EventBase() {}
80
81 // Tests the predicate and evaluates invoke accordingly
82 // Returns 1 if the event was invoked, 0 if not, and a negative value on error
83 virtual HRESULT eval(const FloatP_t &time) {
84 // check predicate
85 if(!predicate()) return 0;
86
87 // invoke
88 if(invoke() == 1) return -1;
89
90 // Update internal data
91 times_fired += 1;
92 last_fired = time;
93
94 return 1;
95 }
96
100 void remove() { flags.push_front(EventFlag::REMOVE); }
101
102 };
103
104 struct Event;
105
106 using EventMethod = EventMethodT<Event>;
107
108 // Simple event
109 struct CAPI_EXPORT Event : EventBase {
110
111 Event();
112
119 Event(EventMethod *invokeMethod, EventMethod *predicateMethod);
120 virtual ~Event();
121
124 HRESULT eval(const FloatP_t &time);
125
126 private:
127
128 EventMethod *invokeMethod;
129 EventMethod *predicateMethod;
130
131 };
132
140 CPPAPI_FUNC(Event*) onEvent(EventMethod *invokeMethod, EventMethod *predicateMethod);
141
142};
143
144#endif // _SOURCE_EVENT_TFEVENT_H_
Tissue Forge event system.
Definition tfEvent.h:34
Event * onEvent(EventMethod *invokeMethod, EventMethod *predicateMethod)
Creates an event using prescribed invoke and predicate functions.
Definition tfEvent.h:45
virtual HRESULT predicate()=0
FloatP_t last_fired
Record of last time fired.
Definition tfEvent.h:53
int times_fired
Record of how many times fired.
Definition tfEvent.h:58
void remove()
Designates event for removal.
Definition tfEvent.h:100
virtual HRESULT invoke()=0
Definition tfEvent.h:109
Event(EventMethod *invokeMethod, EventMethod *predicateMethod)
Construct an instance using functions.
int32_t HRESULT
Definition tf_port.h:255