PySimulation.h 1.61 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#ifndef PYSIMULATION_H
#define PYSIMULATION_H

#include <Python.h>

#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>
#include <vector>

#include "Components/BaseComponent.h"
#include "Simulation.h"

namespace DPsim {
	enum SimState {
		StateStopped = 0,
		StateRunning,
		StatePaused,
		StateDone
	};

	struct PySimulation {
		PyObject_HEAD

		Simulation *sim;
		Logger *log; // TODO other loggers

		std::condition_variable *cond;
		std::mutex *mut;
		std::atomic_bool running;
		std::atomic_int sigPause, numStep;
		std::thread *simThread;
		SimState state;
35
36
37

		PyObject* pyComps; // Components as a (Python) list of PyComponents
		std::vector<BaseComponent*> comps;
38
39
40
41
42
43
44
45
46
47
48
49
50
		
		// Function executed by the simulation thread
		static void simThreadFunction(PySimulation* pySim);

		// The Python API has no notion of C++ classes and methods, so the methods
		// that can be called from Python are static.
		//
		// Helper methods for memory management / initialization etc.
		static PyObject* newfunc(PyTypeObject* type, PyObject *args, PyObject *kwds);
		static int init(PySimulation* self, PyObject *args, PyObject *kwds);
		static void dealloc(PySimulation*);

		// Methods that are actually available from Python
51
		static PyObject* lvector(PyObject *self, PyObject *args);
52
53
54
55
56
57
58
		static PyObject* start(PyObject *self, PyObject *args);
		static PyObject* step(PyObject *self, PyObject *args);
		static PyObject* stop(PyObject *self, PyObject *args);
		static PyObject* pause(PyObject *self, PyObject *args);
		static PyObject* wait(PyObject *self, PyObject *args);
	};

59
	extern PyTypeObject PySimulationType;
60
61
};
#endif