YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
MonitorPython.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026-2026 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#ifndef MONITORPYTHON_H
7#define MONITORPYTHON_H
8
9#include <string>
10#include "MonitorBinding.h"
11
12// Avoid linking against python3XX_d.lib in Debug builds on MSVC.
13// Otherwise you need to python debug libs installed, which is not the case for most users.
14#if defined(_MSC_VER) && defined(_DEBUG)
15 # include <cstddef>
16 # include <cstdio>
17 # include <cstdlib>
18 # undef _DEBUG
19 # include "Python.h"
20 # define _DEBUG
21#else
22 # include "Python.h"
23#endif
24
25
26#include <mutex>
27
29{
30
31public:
33 ~MonitorPython() override;
34
35 bool load(const yarp::os::Property& options) override;
36 bool setParams(const yarp::os::Property& params) override;
37 bool getParams(yarp::os::Property& params) override;
38
39 bool acceptData(yarp::os::Things& thing) override;
42
43 bool peerTrigged() override;
44 bool canAccept() override;
45
46 bool setAcceptConstraint(const char* constraint) override
47 {
48 if(!constraint) {
49 return false;
50 }
51 MonitorPython::constraint = constraint;
52 trimString(MonitorPython::constraint);
53 return true;
54 }
55
56 const char* getAcceptConstraint() override {
57 return constraint.c_str();
58 }
59
60 bool hasAccept() override {
61 return bHasAcceptCallback;
62 }
63
64 bool hasUpdate() override {
65 return bHasUpdateCallback;
66 }
67
68 bool hasUpdateReply() override {
69 return bHasUpdateReplyCallback;
70 }
71
72private:
73 std::string constraint;
74 bool bHasAcceptCallback;
75 bool bHasUpdateCallback;
76 bool bHasUpdateReplyCallback;
77
78 std::string m_path;
79 std::string m_pythonScriptName = "monitor"; // Python module containing the functions/classes
80 std::recursive_mutex m_monitor_mutex;
81
82 bool classWrapper(PyObject* &pClassInstance, std::string methodName, PyObject* &pClassMethodArgs, PyObject* &pValue);
83 bool functionWrapper(std::string functionName, PyObject* &pArgs, PyObject* &pValue);
84 bool hasPythonFunction(const std::string& functionName);
85 bool ensureYarpModuleLoaded();
86
87 PyObject* m_classInstance{nullptr}; // Python object of the created class
88 PyObject* m_lastUpdateResult{nullptr};
89 PyObject* m_pModule{nullptr};
90
91public:
93
94private:
95 void trimString(std::string& str);
96 void searchReplace(std::string& str,
97 const std::string& oldStr, const std::string& newStr);
98 bool isKeyword(const char* str);
99 bool loadPythonModule(const std::string& moduleName, PyObject* &pModule);
100};
101
102
103#endif //MONITORPYTHON_H
bool peerTrigged() override
bool hasAccept() override
bool setParams(const yarp::os::Property &params) override
bool load(const yarp::os::Property &options) override
yarp::os::Things & updateData(yarp::os::Things &thing) override
bool acceptData(yarp::os::Things &thing) override
MonitorTrigger< MonitorPython > * trigger
const char * getAcceptConstraint() override
bool hasUpdate() override
bool canAccept() override
bool hasUpdateReply() override
bool getParams(yarp::os::Property &params) override
MonitorPython()
Class MonitorPython.
yarp::os::Things & updateReply(yarp::os::Things &thing) override
~MonitorPython() override
bool setAcceptConstraint(const char *constraint) override
A class for storing options and configuration information.
Definition Property.h:32
Base class for generic things.
Definition Things.h:18