NRP Core  1.4.1
proto_field_ops.h
Go to the documentation of this file.
1 /* * NRP Core - Backend infrastructure to synchronize simulations
2  *
3  * Copyright 2020-2023 NRP Team
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * This project has received funding from the European Union’s Horizon 2020
18  * Framework Programme for Research and Innovation under the Specific Grant
19  * Agreement No. 945539 (Human Brain Project SGA3).
20  */
21 
22 #ifndef PROTO_FIELD_OPS_H
23 #define PROTO_FIELD_OPS_H
24 
25 #include "google/protobuf/message.h"
26 #include <boost/python.hpp>
28 
29 namespace bpy = boost::python;
30 namespace gpb = google::protobuf;
31 
32 
36 namespace proto_field_ops {
37 
41  bpy::object GetScalarField(gpb::Message &m, const gpb::FieldDescriptor *field);
42 
46  std::string GetScalarFieldAsString(const gpb::Message &m, const gpb::FieldDescriptor *field);
47 
51  bpy::object GetRepeatedScalarField(gpb::Message &m, const gpb::FieldDescriptor *field, int index);
52 
56  template<class MSG, class ...REMAINING_MSGS>
57  bpy::object GetMessageField(gpb::Message &m, const gpb::FieldDescriptor *field) {
58  static_assert(std::is_base_of_v<google::protobuf::Message, MSG>,"Parameter MSG must derive from protobuf::Message");
59  static_assert((std::is_base_of_v<google::protobuf::Message, REMAINING_MSGS> && ...), "Parameter REMAINING_MSGS must derive from protobuf::Message");
60 
61  MSG *msg_field = dynamic_cast<MSG *>(m.GetReflection()->MutableMessage(&m, field));
62  if(msg_field != nullptr) {
64  return bpy::object(bpy::handle<>(convert(msg_field)));
65  }
66 
67  if constexpr (sizeof...(REMAINING_MSGS) > 0)
68  return GetMessageField<REMAINING_MSGS...>(m, field);
69  else
70  throw NRPException::logCreate("Unable to get composite field with name " + field->name());
71  }
72 
76  void SetScalarField(gpb::Message &m, const gpb::FieldDescriptor *field, const bpy::object &value);
77 
81  void SetRepeatedScalarField(gpb::Message &m, const gpb::FieldDescriptor *field, const bpy::object &value, int index);
82 
86  void AddRepeatedScalarField(gpb::Message &m, const gpb::FieldDescriptor *field, const bpy::object &value);
87 
88 }
89 
90 #endif // PROTO_FIELD_OPS_H
proto_field_ops::GetScalarFieldAsString
std::string GetScalarFieldAsString(const gpb::Message &m, const gpb::FieldDescriptor *field)
Get scalar field value as a string.
Definition: proto_field_ops.cpp:68
nrp_exceptions.h
proto_field_ops::SetScalarField
void SetScalarField(gpb::Message &m, const gpb::FieldDescriptor *field, const bpy::object &value)
Set scalar field.
Definition: proto_field_ops.cpp:142
python_grpc_engine.type
type
Definition: python_grpc_engine.py:63
proto_field_ops
Implement single field Get/Set operations using field descriptor and reflection interface.
Definition: proto_field_ops.cpp:25
proto_field_ops::GetScalarField
bpy::object GetScalarField(gpb::Message &m, const gpb::FieldDescriptor *field)
Get scalar field. Returns a copy of the field value.
Definition: proto_field_ops.cpp:27
proto_field_ops::SetRepeatedScalarField
void SetRepeatedScalarField(gpb::Message &m, const gpb::FieldDescriptor *field, const bpy::object &value, int index)
Set repeated scalar field.
Definition: proto_field_ops.cpp:177
NRPException::logCreate
static EXCEPTION logCreate(LOG_EXCEPTION_T &exception, const std::string &msg, NRPLogger::spdlog_out_fcn_t spdlogCall=NRPLogger::critical)
Definition: nrp_exceptions.h:73
proto_field_ops::GetRepeatedScalarField
bpy::object GetRepeatedScalarField(gpb::Message &m, const gpb::FieldDescriptor *field, int index)
Get repeated scalar field. Returns a copy of the field value.
Definition: proto_field_ops.cpp:104
proto_field_ops::AddRepeatedScalarField
void AddRepeatedScalarField(gpb::Message &m, const gpb::FieldDescriptor *field, const bpy::object &value)
Append repeated scalar field.
Definition: proto_field_ops.cpp:211
proto_field_ops::GetMessageField
bpy::object GetMessageField(gpb::Message &m, const gpb::FieldDescriptor *field)
Get message field. Returns a reference of the field value.
Definition: proto_field_ops.h:57