NRP Core  1.4.1
data_conversion.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 DATA_CONVERSION_H
23 #define DATA_CONVERSION_H
24 
25 #include <boost/python.hpp>
26 
28 
29 
35 template <class T_IN, class T_OUT>
36 struct dataConverter {
37  static void convert(const T_IN */*d1*/, T_OUT &/*d2*/) {
38  std::stringstream s;
39  s << "data conversion from " << typeid(T_IN).name() << " to type " << typeid(T_OUT).name()
40  << " is not implemented.";
41  throw NRPException::logCreate(s.str());
42  }
43 };
44 
45 // dataConverter specializations
46 
47 namespace bpy = boost::python;
48 
49 template <class T_IN>
50 struct dataConverter<T_IN, bpy::object> {
51  static void convert(const T_IN *d1, bpy::object &d2)
52  {
53  try {
54  if constexpr((std::is_class_v<T_IN> || std::is_union_v<T_IN>) && !std::is_same_v<std::string, T_IN> )
55  d2 = bpy::object(bpy::ptr(d1));
56  else
57  d2 = bpy::object(*d1);
58  }
59  catch (const boost::python::error_already_set&) {
60  std::string error_msg = "An error occurred while converting C++ data to Python. Please be sure you imported the Python module/s supporting the conversion of this data type";
61  PyErr_Print();
62  throw NRPException::logCreate(error_msg);
63  }
64  }
65 };
66 
67 template <class T_OUT>
68 struct dataConverter<bpy::object, T_OUT> {
69  static void convert(const bpy::object *d1, T_OUT &d2)
70  {
71  try {
72  d2 = boost::python::extract<T_OUT>(*d1);
73  }
74  catch (const boost::python::error_already_set&) {
75  std::string error_msg = "An error occurred while converting Python to C++ data";
76  PyErr_Print();
77  throw NRPException::logCreate(error_msg);
78  }
79  }
80 };
81 
82 #endif //DATA_CONVERSION_H
dataConverter< bpy::object, T_OUT >::convert
static void convert(const bpy::object *d1, T_OUT &d2)
Definition: data_conversion.h:69
dataConverter
Template for data conversion functions used in InputPorts upon new message arrival.
Definition: data_conversion.h:36
nrp_exceptions.h
dataConverter::convert
static void convert(const T_IN *, T_OUT &)
Definition: data_conversion.h:37
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
dataConverter< T_IN, bpy::object >::convert
static void convert(const T_IN *d1, bpy::object &d2)
Definition: data_conversion.h:51