NRP Core  1.4.1
datapack.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_DATAPACK_H
23 #define DATA_DATAPACK_H
24 
27 #include <boost/python.hpp>
28 
35 template< class DATA_TYPE>
37 {
38 public:
39 
40  DataPack(const std::string &name, const std::string &engineName, DATA_TYPE* data_)
41  : DataPackInterface(createID(name, engineName)), data(data_)
42  { this->setIsEmpty(false); }
43 
44  DataPack(const std::string &name, const std::string &engineName)
45  : DataPackInterface(createID(name, engineName)), data(new DATA_TYPE())
46  { this->setIsEmpty(false); }
47 
48  DataPack (const DataPack&) = delete;
49  DataPack& operator= (const DataPack&) = delete;
50 
51  DataPack(DataPack&& obj) = default;
52  DataPack& operator = (DataPack&&) = default;
53 
61  static std::string getType()
62  {
63  return typeid(DATA_TYPE).name();
64  }
65 
76  static DataPackIdentifier createID(const std::string &name, const std::string &engineName)
77  {
79  }
80 
89  const DATA_TYPE& getData() const
90  {
91  if(this->isEmpty())
92  throw NRPException::logCreate("Attempt to get data from empty DataPack " + this->name());
93  else
94  return *data.get();
95  }
96 
102  PyObject * toPythonString()
103  {
104  std::string dataStr = boost::python::extract<std::string>(boost::python::str(this->data.get()));
105  std::string dataPackStr = "- name: '" + this->name() + "'\n- engine: '" + this->engineName() + "'\n- data:\n" + dataStr;
106  return PyUnicode_FromString(dataPackStr.c_str());
107  }
108 
109  static void create_python(const std::string &name)
110  {
111  using namespace boost::python;
112  class_< DataPack<DATA_TYPE>, DataPack<DATA_TYPE> *, bases<DataPackInterface>, boost::noncopyable >
113  binder(name.data(), init<const std::string&, const std::string& >((boost::python::arg("name"), boost::python::arg("engine_name"))));
114  binder.add_property("data", make_function(&DataPack<DATA_TYPE>::getData, return_internal_reference<>()));
115  binder.def("__str__", &DataPack<DATA_TYPE>::toPythonString);
116  binder.def("getType", &DataPack<DATA_TYPE>::getType).staticmethod("getType");
117  }
118 
119  DataPackInterface* clone() const override
120  { return new DataPack<DATA_TYPE>(this->name(), this->engineName(), new DATA_TYPE(*data), isUpdated()); }
121 
122 private:
123 
124  DataPack(const std::string &name, const std::string &engineName, DATA_TYPE* data_, bool isUpdated) :
126  data(data_)
127  {
128  this->setIsEmpty(false);
129  }
130 
131  std::unique_ptr<DATA_TYPE> data;
132 };
133 
134 
142 template< class DATA_TYPE>
143 class RawData : public DataPack<DATA_TYPE>
144 {
145 public:
146 
148  : DataPack<DATA_TYPE>("", "")
149  {}
150 
151  static void create_python(const std::string &name)
152  {
153  using namespace boost::python;
154  class_< RawData<DATA_TYPE>, RawData<DATA_TYPE> *, bases<DataPack<DATA_TYPE>>, boost::noncopyable>
155  binder(name.data(), init<>());
156  binder.add_property("data", make_function(&RawData<DATA_TYPE>::getData, return_internal_reference<>()));
157  binder.def("__str__", &RawData<DATA_TYPE>::toPythonString);
158  binder.def("getType", &RawData<DATA_TYPE>::getType).staticmethod("getType");
159  }
160 };
161 
162 #endif // DATA_DATAPACK_H
RawData
Wrapper class for DataPacks with no name and no engine association.
Definition: datapack.h:143
DataPack::operator=
DataPack & operator=(const DataPack &)=delete
DataPack::clone
DataPackInterface * clone() const override
Virtual clone method to support polymorphic copy.
Definition: datapack.h:119
DataPack::toPythonString
PyObject * toPythonString()
Returns a python string representation of this object content.
Definition: datapack.h:102
datapack_interface.h
nrp_exceptions.h
DataPack::getType
static std::string getType()
Returns type of the datapack class.
Definition: datapack.h:61
DataPack::getData
const DATA_TYPE & getData() const
Returns reference to data stored in the object.
Definition: datapack.h:89
DataPack::DataPack
DataPack(const std::string &name, const std::string &engineName)
Definition: datapack.h:44
python_grpc_engine.str
str
Definition: python_grpc_engine.py:63
DataPackInterface::isEmpty
bool isEmpty() const
Indicates if the datapack contains any data aside from datapack ID.
Definition: datapack_interface.cpp:74
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
RawData::create_python
static void create_python(const std::string &name)
Definition: datapack.h:151
DataPackIdentifier
Identifies a single datapack.
Definition: datapack_interface.h:38
DataPack
Base datapack class.
Definition: datapack.h:36
DataPack::DataPack
DataPack(const std::string &name, const std::string &engineName, DATA_TYPE *data_)
Definition: datapack.h:40
DataPackInterface::isUpdated
bool isUpdated() const
Indicates if the DataPack was created or received on the current simulation iteration.
Definition: datapack_interface.cpp:84
DataPackInterface
Interface to datapacks.
Definition: datapack_interface.h:89
DataPackInterface::name
const std::string & name() const
Definition: datapack_interface.cpp:34
DataPack::createID
static DataPackIdentifier createID(const std::string &name, const std::string &engineName)
Creates a DataPackIdentifier object with type matching the DATA_TYPE used by the DataPack class.
Definition: datapack.h:76
DataPack::create_python
static void create_python(const std::string &name)
Definition: datapack.h:109
DataPackInterface::setIsEmpty
void setIsEmpty(bool value)
Definition: datapack_interface.cpp:79
DataPackInterface::engineName
const std::string & engineName() const
Definition: datapack_interface.cpp:54
RawData::RawData
RawData()
Definition: datapack.h:147