NRP Core  1.4.1
datapack_interface.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 DATAPACK_INTERFACE_H
23 #define DATAPACK_INTERFACE_H
24 
26 
27 #include <string>
28 #include <string_view>
29 #include <memory>
30 #include <type_traits>
31 #include <vector>
32 #include <set>
33 
34 
39 {
40  public:
44  std::string Name;
45 
49  std::string EngineName;
50 
54  std::string Type;
55 
56  DataPackIdentifier() = default;
57 
58  DataPackIdentifier (const DataPackIdentifier&) = default;
60 
61  DataPackIdentifier(DataPackIdentifier&& obj) = default;
63 
64  DataPackIdentifier(const std::string &_name, const std::string &_engineName, const std::string &_type);
65 
66  DataPackIdentifier(std::string &&_name, std::string &&_engineName, std::string &&_type)
67  : Name(std::forward<std::string>(_name)),
68  EngineName(std::forward<std::string>(_engineName)),
69  Type(std::forward<std::string>(_type))
70  {}
71 
72  bool operator == (const DataPackIdentifier & rhs) const {
73  return (Name == rhs.Name)
74  && (EngineName == rhs.EngineName)
75  && (Type == rhs.Type);
76  }
77 
78  bool operator < (const DataPackIdentifier & rhs) const{
79  return (Name < rhs.Name) || (Name == rhs.Name && EngineName < rhs.EngineName) || (Name == rhs.Name &&
80  EngineName == rhs.EngineName && Type < rhs.Type);
81  }
82 
83 };
84 
85 
90  : public PtrTemplates<DataPackInterface>
91 {
92  public:
93  DataPackInterface() = default;
94 
95  DataPackInterface (const DataPackInterface&) = default;
97 
98  DataPackInterface(DataPackInterface&& obj) = default;
100 
101  template<class DEV_ID_T>
102  DataPackInterface(DEV_ID_T &&id)
103  : _id(std::forward<DEV_ID_T>(id)){
104  static_assert(std::is_same_v<std::remove_reference_t<DEV_ID_T>, const DataPackIdentifier> || std::is_same_v< DataPackIdentifier, std::remove_reference_t<DEV_ID_T> >,"Parameter DEV_ID_T must be type DataPackIdentifier or DataPackIdentifier&");
105  static_assert(std::is_same_v<std::remove_reference_t<DEV_ID_T>, DataPackIdentifier> || std::is_same_v< const DataPackIdentifier, std::remove_reference_t<DEV_ID_T> >,"Parameter DEV_ID_T must be type DataPackIdentifier or DataPackIdentifier&");
106  }
107 
108  DataPackInterface(const std::string &name, const std::string &engineName, const std::string &type);
109  virtual ~DataPackInterface() = default;
110 
111  const std::string &name() const;
112  void setName(const std::string &name);
113 
114  const std::string &type() const;
115  void setType(const std::string &type);
116 
117  const std::string &engineName() const;
118  void setEngineName(const std::string &engineName);
119 
120  const DataPackIdentifier &id() const;
121  void setID(const DataPackIdentifier &id);
122 
126  virtual DataPackInterface* clone() const
127  { return new DataPackInterface(this->name(), this->engineName(), this->type(), this->isUpdated()); }
128 
136  bool isEmpty() const;
137 
144  bool isUpdated() const;
145 
150  void resetIsUpdated() const;
151 
152  protected:
153 
154  void setIsEmpty(bool value);
155  DataPackInterface(const std::string &name, const std::string &engineName, const std::string &type, bool isUpdated)
157  {
158  this->_isUpdated = isUpdated;
159  }
160 
161  private:
165  DataPackIdentifier _id;
166 
170  bool _isEmpty = true;
171 
176  mutable bool _isUpdated = true;
177 };
178 
181 
182 
192 {
193  // Let the container know that we may be comparing with other types than shared_ptr (e.g. DataPackIdentifier)
194  // Needed when operator() is overloaded
195  using is_transparent = std::true_type;
196 
197  bool operator() (const std::shared_ptr<const DataPackInterface> & lhs, const std::shared_ptr<const DataPackInterface> & rhs) const
198  {
199  return lhs->id().Name < rhs->id().Name ||
200  (lhs->id().Name == rhs->id().Name &&
201  lhs->id().EngineName < rhs->id().EngineName);
202  }
203 
204  bool operator() (const std::shared_ptr<const DataPackInterface> & lhs, const DataPackIdentifier & rhs) const
205  {
206  return lhs->id().Name < rhs.Name ||
207  (lhs->id().Name == rhs.Name &&
208  lhs->id().EngineName < rhs.EngineName);
209  }
210 
211  bool operator() (const DataPackIdentifier & lhs, const std::shared_ptr<const DataPackInterface> & rhs) const
212  {
213  return lhs.Name < rhs->id().Name ||
214  (lhs.Name == rhs->id().Name &&
215  lhs.EngineName < rhs->id().EngineName);
216  }
217 };
218 
219 using datapacks_set_t = std::set<std::shared_ptr<const DataPackInterface>, DataPackPointerComparator>;
220 using datapacks_vector_t = std::vector<std::shared_ptr<const DataPackInterface>>;
221 using datapack_identifiers_set_t = std::set<DataPackIdentifier>;
222 
223 
224 #endif // DATAPACK_INTERFACE_H
datapacks_vector_t
std::vector< std::shared_ptr< const DataPackInterface > > datapacks_vector_t
Definition: datapack_interface.h:220
DataPackInterface::setEngineName
void setEngineName(const std::string &engineName)
Definition: datapack_interface.cpp:59
DataPackIdentifier::DataPackIdentifier
DataPackIdentifier()=default
datapacks_set_t
std::set< std::shared_ptr< const DataPackInterface >, DataPackPointerComparator > datapacks_set_t
Definition: datapack_interface.h:219
DataPackPointerComparator::operator()
bool operator()(const std::shared_ptr< const DataPackInterface > &lhs, const std::shared_ptr< const DataPackInterface > &rhs) const
Definition: datapack_interface.h:197
DataPackIdentifier::Type
std::string Type
DataPack Type.
Definition: datapack_interface.h:54
DataPackInterfaceConstSharedPtr
DataPackInterface::const_shared_ptr DataPackInterfaceConstSharedPtr
Definition: datapack_interface.h:180
DataPackInterface::setName
void setName(const std::string &name)
Definition: datapack_interface.cpp:39
DataPackInterface::DataPackInterface
DataPackInterface()=default
DataPackInterface::id
const DataPackIdentifier & id() const
Definition: datapack_interface.cpp:64
PtrTemplates< DataPackInterface >::const_shared_ptr
std::shared_ptr< const DataPackInterface > const_shared_ptr
Definition: ptr_templates.h:32
DataPackPointerComparator::is_transparent
std::true_type is_transparent
Definition: datapack_interface.h:195
DataPackInterface::~DataPackInterface
virtual ~DataPackInterface()=default
DataPackInterface::setID
void setID(const DataPackIdentifier &id)
Definition: datapack_interface.cpp:69
DataPackInterface::DataPackInterface
DataPackInterface(DEV_ID_T &&id)
Definition: datapack_interface.h:102
DataPackIdentifier::DataPackIdentifier
DataPackIdentifier(std::string &&_name, std::string &&_engineName, std::string &&_type)
Definition: datapack_interface.h:66
DataPackIdentifier::Name
std::string Name
DataPack Name. Used by simulator to identify source/sink of datapack.
Definition: datapack_interface.h:44
DataPackInterfaceSharedPtr
DataPackInterface::shared_ptr DataPackInterfaceSharedPtr
Definition: datapack_interface.h:179
datapack_identifiers_set_t
std::set< DataPackIdentifier > datapack_identifiers_set_t
Definition: datapack_interface.h:221
DataPackInterface::operator=
DataPackInterface & operator=(const DataPackInterface &)=default
DataPackIdentifier::operator==
bool operator==(const DataPackIdentifier &rhs) const
Definition: datapack_interface.h:72
DataPackInterface::clone
virtual DataPackInterface * clone() const
Virtual clone method to support polymorphic copy.
Definition: datapack_interface.h:126
PtrTemplates
Definition: ptr_templates.h:28
DataPackInterface::isEmpty
bool isEmpty() const
Indicates if the datapack contains any data aside from datapack ID.
Definition: datapack_interface.cpp:74
DataPackPointerComparator
Custom comparator functor used by sets of DataPack shared pointers.
Definition: datapack_interface.h:191
ptr_templates.h
DataPackIdentifier::operator<
bool operator<(const DataPackIdentifier &rhs) const
Definition: datapack_interface.h:78
PtrTemplates< DataPackInterface >::shared_ptr
std::shared_ptr< DataPackInterface > shared_ptr
Definition: ptr_templates.h:31
DataPackIdentifier
Identifies a single datapack.
Definition: datapack_interface.h:38
DataPackInterface::isUpdated
bool isUpdated() const
Indicates if the DataPack was created or received on the current simulation iteration.
Definition: datapack_interface.cpp:84
DataPackInterface::DataPackInterface
DataPackInterface(const std::string &name, const std::string &engineName, const std::string &type, bool isUpdated)
Definition: datapack_interface.h:155
DataPackInterface
Interface to datapacks.
Definition: datapack_interface.h:89
DataPackInterface::name
const std::string & name() const
Definition: datapack_interface.cpp:34
DataPackInterface::resetIsUpdated
void resetIsUpdated() const
Sets the isUpdated flag to false TODO This method is obviously non-const, it will be changed in NRRPL...
Definition: datapack_interface.cpp:89
DataPackIdentifier::operator=
DataPackIdentifier & operator=(const DataPackIdentifier &)=default
DataPackInterface::setType
void setType(const std::string &type)
Definition: datapack_interface.cpp:49
DataPackIdentifier::EngineName
std::string EngineName
Corresponding engine.
Definition: datapack_interface.h:49
DataPackInterface::setIsEmpty
void setIsEmpty(bool value)
Definition: datapack_interface.cpp:79
DataPackInterface::type
const std::string & type() const
Definition: datapack_interface.cpp:44
DataPackInterface::engineName
const std::string & engineName() const
Definition: datapack_interface.cpp:54