-
Notifications
You must be signed in to change notification settings - Fork 1
/
PythonMessage.h
106 lines (92 loc) · 3.17 KB
/
PythonMessage.h
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (c) 2022 Ultimaker B.V.
// pyArcus is released under the terms of the LGPLv3 or higher.
#ifndef ARCUS_PYTHON_MESSAGE_H
#define ARCUS_PYTHON_MESSAGE_H
#include "Arcus/Types.h"
#include <Python.h>
namespace google
{
namespace protobuf
{
class Descriptor;
class Reflection;
} // namespace protobuf
} // namespace google
namespace Arcus
{
/**
* A simple wrapper around a Protobuf message so it can be used from Python.
*
* This class wraps a Protobuf message and makes it possible to get and set
* values from the message. Message properties are exposed as Python properties
* so can be set using things like `message.data = b"something"` from Python.
*
* Repeated messages are supported, using addRepeatedMessage, repeatedMessageCount
* and getRepeatedMessage. A repeated message is returned as a PythonMessage object
* so exposes the same API as the top level message.
*/
class PythonMessage
{
public:
PythonMessage(google::protobuf::Message* message);
PythonMessage(const MessagePtr& message);
virtual ~PythonMessage();
/**
* Get the message type name of this message.
*/
std::string getTypeName() const;
/**
* Python property interface.
*/
bool __hasattr__(const std::string& field_name) const;
PyObject* __getattr__(const std::string& field_name) const;
void __setattr__(const std::string& name, PyObject* value);
/**
* Add an instance of a Repeated Message to a specific field.
*
* \param field_name The name of the field to add a message to.
*
* \return A pointer to an instance of PythonMessage wrapping the new Message in the field.
*/
PythonMessage* addRepeatedMessage(const std::string& field_name);
/**
* Get the number of messages in a repeated message field.
*/
int repeatedMessageCount(const std::string& field_name) const;
/**
* Get a specific instance of a message in a repeated message field.
*
* \param field_name The name of a repeated message field to get an instance from.
* \param index The index of the item to get in the repeated field.
*
* \return A pointer to an instance of PythonMessage wrapping the specified repeated message.
*/
PythonMessage* getRepeatedMessage(const std::string& field_name, int index);
/**
* Get a specific instance of a message in a message field.
*
* \param field_name The name of a repeated message field to get an instance from.
*
* \return A pointer to an instance of PythonMessage wrapping the specified repeated message.
*/
PythonMessage* getMessage(const std::string& field_name);
/**
* Get the value of a certain enumeration.
*
* \param enum_value The fully-qualified name of an Enum value.
*
* \return The integer value of the specified enum.
*/
int getEnumValue(const std::string& enum_value) const;
/**
* Internal.
*/
MessagePtr getSharedMessage() const;
private:
MessagePtr _shared_message;
google::protobuf::Message* _message;
const google::protobuf::Reflection* _reflection;
const google::protobuf::Descriptor* _descriptor;
};
} // namespace Arcus
#endif // ARCUS_PYTHON_MESSAGE_H