helics  2.8.1
core-data.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2021,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7 #pragma once
8 
9 #include "helics-time.hpp"
10 #include "helics/helics-config.h"
11 
12 #include <memory>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
25 namespace helics {
31 class data_block {
32  private:
33  std::string m_data;
34  friend class data_view;
35  friend class ActionMessage;
36  public:
38  data_block() = default;
40  ~data_block() = default;
42  explicit data_block(size_t blockSize) { m_data.resize(blockSize); }
44  data_block(size_t blockSize, char init): m_data(blockSize, init) {}
46  data_block(const data_block& db) = default;
48  data_block(data_block&& db) = default;
50  // NOLINTNEXTLINE
51  /* implicit */ data_block(const char* s): m_data(s) {}
53  // NOLINTNEXTLINE
54  /* implicit */ data_block(const std::string& str): m_data(str) {}
56  // NOLINTNEXTLINE
57  /* implicit */ data_block(std::string&& str) noexcept: m_data(std::move(str)) {}
59  data_block(const char* s, size_t len): m_data(s, len) {}
61  // NOLINTNEXTLINE
62  /* implicit */ data_block(const std::vector<char>& vdata): m_data(vdata.data(), vdata.size()) {}
64  template<class X>
65  // NOLINTNEXTLINE
66  /* implicit */ data_block(const std::vector<X>& vdata):
67  m_data(reinterpret_cast<const char*>(vdata.data()), vdata.size() * sizeof(X))
68  {
69  }
71  data_block& operator=(const data_block& db) = default;
73  data_block& operator=(data_block&& db) = default;
75  data_block& operator=(std::string str)
76  {
77  m_data = std::move(str);
78  return *this;
79  }
81  data_block& operator=(const char* s)
82  {
83  m_data.assign(s);
84  return *this;
85  }
87  data_block& assign(const char* s, size_t len)
88  {
89  m_data.assign(s, len);
90  return *this;
91  }
93  void swap(data_block& db2) noexcept { m_data.swap(db2.m_data); }
95  void append(const char* s, size_t len) { m_data.append(s, len); }
97  void append(const std::string& str) { m_data.append(str); }
99  bool operator==(const data_block& db) const { return m_data == db.m_data; }
101  bool operator==(const std::string& str) const { return m_data == str; }
103  bool operator<(const data_block& db) const { return (m_data < db.m_data); }
105  bool operator>(const data_block& db) const { return (m_data > db.m_data); }
107  char* data() { return &(m_data.front()); }
109  const char* data() const { return &(m_data.front()); }
110 
112  bool empty() const noexcept { return m_data.empty(); }
114  size_t size() const { return m_data.length(); }
116  void resize(size_t newSize) { m_data.resize(newSize); }
118  void resize(size_t newSize, char T) { m_data.resize(newSize, T); }
120  void reserve(size_t space) { m_data.reserve(space); }
122  const std::string& to_string() const { return m_data; }
124  char& operator[](int index) { return m_data[index]; }
126  char operator[](int index) const { return m_data[index]; }
128  auto begin() { return m_data.begin(); }
130  auto end() { return m_data.end(); }
132  auto cbegin() const { return m_data.cbegin(); }
134  auto cend() const { return m_data.cend(); }
136  void push_back(char newchar) { m_data.push_back(newchar); }
137 };
138 
140 inline bool operator!=(const data_block& db1, const data_block& db2)
141 {
142  return !(db1 == db2);
143 }
144 
146 class Message {
147  public:
149  std::uint16_t flags{0};
150  std::uint16_t messageValidation{0U};
151  std::int32_t messageID{0};
153  std::string dest;
154  std::string source;
155  std::string original_source;
156  std::string original_dest;
157  std::int32_t counter{0};
158  void* backReference{nullptr};
159 
161  Message() = default;
163  void swap(Message& m2) noexcept
164  {
165  std::swap(time, m2.time);
166  std::swap(flags, m2.flags);
167  std::swap(messageID, m2.messageID);
168  original_source.swap(m2.original_source);
169  source.swap(m2.source);
170  dest.swap(m2.dest);
171  data.swap(m2.data);
172  original_dest.swap(m2.original_dest);
173  }
176  bool isValid() const noexcept
177  {
178  return (!data.empty()) ? true : ((!source.empty()) ? true : (!dest.empty()));
179  }
181  const std::string& to_string() const { return data.to_string(); }
183  void clear()
184  {
185  time = timeZero;
186  flags = 0;
187  messageID = 0;
188  data.resize(0);
189  dest.clear();
190  source.clear();
191  original_source.clear();
192  original_dest.clear();
193  counter = 0;
194  }
195 };
196 
203  public:
205  FilterOperator() = default;
207  virtual ~FilterOperator() = default;
209  virtual std::unique_ptr<Message> process(std::unique_ptr<Message> message) = 0;
211  virtual std::vector<std::unique_ptr<Message>> processVector(std::unique_ptr<Message> message)
212  {
213  std::vector<std::unique_ptr<Message>> ret;
214  auto res = process(std::move(message));
215  if (res) {
216  ret.push_back(std::move(res));
217  }
218  return ret;
219  }
222  std::unique_ptr<Message> operator()(std::unique_ptr<Message> message)
223  {
224  return process(std::move(message));
225  }
228  virtual bool isMessageGenerating() const { return false; }
229 };
230 
233 class NullFilterOperator final: public FilterOperator {
234  public:
236  NullFilterOperator() = default;
237  virtual std::unique_ptr<Message> process(std::unique_ptr<Message> message) override
238  {
239  return message;
240  }
241 };
242 
248 template<class sizeType, class SizedDataType>
249 inline bool isValidIndex(sizeType testSize, const SizedDataType& vec)
250 {
251  return ((testSize >= sizeType(0)) && (testSize < static_cast<sizeType>(vec.size())));
252 }
253 
254 } // namespace helics
helics::operator!=
bool operator!=(const data_block &db1, const data_block &db2)
Definition: core-data.hpp:140
helics::timeZero
constexpr Time timeZero
Definition: helics-time.hpp:31
helics-time.hpp
helics::Message::isValid
bool isValid() const noexcept
Definition: core-data.hpp:176
helics::data_block::~data_block
~data_block()=default
helics::data_block::assign
data_block & assign(const char *s, size_t len)
Definition: core-data.hpp:87
helics::data_block::append
void append(const char *s, size_t len)
Definition: core-data.hpp:95
helics::Message::to_string
const std::string & to_string() const
Definition: core-data.hpp:181
helics::data_block::operator=
data_block & operator=(std::string str)
Definition: core-data.hpp:75
helics::data_block::operator==
bool operator==(const std::string &str) const
Definition: core-data.hpp:101
helics::data_block::operator==
bool operator==(const data_block &db) const
Definition: core-data.hpp:99
helics::data_block
Definition: core-data.hpp:31
helics::data_block::reserve
void reserve(size_t space)
Definition: core-data.hpp:120
helics::Message::messageID
std::int32_t messageID
the messageID for a message
Definition: core-data.hpp:151
helics::FilterOperator::FilterOperator
FilterOperator()=default
helics::data_block::resize
void resize(size_t newSize)
Definition: core-data.hpp:116
helics::data_block::resize
void resize(size_t newSize, char T)
Definition: core-data.hpp:118
helics::Message::backReference
void * backReference
back referencing pointer not used by helics
Definition: core-data.hpp:158
helics::data_block::data
char * data()
Definition: core-data.hpp:107
helics::FilterOperator::operator()
std::unique_ptr< Message > operator()(std::unique_ptr< Message > message)
Definition: core-data.hpp:222
helics::FilterOperator::~FilterOperator
virtual ~FilterOperator()=default
helics::data_block::operator=
data_block & operator=(const char *s)
Definition: core-data.hpp:81
helics::data_block::data_block
data_block(std::string &&str) noexcept
Definition: core-data.hpp:57
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helics-time.hpp:27
helics::data_block::operator=
data_block & operator=(const data_block &db)=default
helics::ActionMessage
Definition: ActionMessage.hpp:29
helics::Message::source
std::string source
the most recent source of the message
Definition: core-data.hpp:154
helics::FilterOperator::isMessageGenerating
virtual bool isMessageGenerating() const
Definition: core-data.hpp:228
helics::Message::data
data_block data
the data packet for the message
Definition: core-data.hpp:152
helics::data_view
Definition: data_view.hpp:22
helics::NullFilterOperator::process
virtual std::unique_ptr< Message > process(std::unique_ptr< Message > message) override
Definition: core-data.hpp:237
helics::data_block::data_block
data_block(const std::vector< char > &vdata)
Definition: core-data.hpp:62
helics::Message::counter
std::int32_t counter
indexing counter not used directly by helics
Definition: core-data.hpp:157
helics::data_block::empty
bool empty() const noexcept
Definition: core-data.hpp:112
helics::data_block::operator[]
char operator[](int index) const
Definition: core-data.hpp:126
helics::data_block::data_block
data_block(size_t blockSize)
Definition: core-data.hpp:42
helics::data_block::data_block
data_block(const char *s, size_t len)
Definition: core-data.hpp:59
helics::data_block::data_block
data_block(const char *s)
Definition: core-data.hpp:51
helics::Message::original_dest
std::string original_dest
the original destination of a message
Definition: core-data.hpp:156
helics::Message::messageValidation
std::uint16_t messageValidation
extra field for user object usage, not used by HELICS
Definition: core-data.hpp:150
helics::Message::time
Time time
the event time the message is sent
Definition: core-data.hpp:148
helics::Message::clear
void clear()
Definition: core-data.hpp:183
helics::data_block::data_block
data_block(const std::vector< X > &vdata)
Definition: core-data.hpp:66
helics::data_block::data_block
data_block()=default
helics::Message::original_source
std::string original_source
the original source of the message
Definition: core-data.hpp:155
helics::FilterOperator
Definition: core-data.hpp:202
helics::data_block::append
void append(const std::string &str)
Definition: core-data.hpp:97
helics::data_block::data_block
data_block(const std::string &str)
Definition: core-data.hpp:54
helics::data_block::push_back
void push_back(char newchar)
Definition: core-data.hpp:136
helics::data_block::size
size_t size() const
Definition: core-data.hpp:114
helics::data_block::operator<
bool operator<(const data_block &db) const
Definition: core-data.hpp:103
helics::Message::flags
std::uint16_t flags
message flags
Definition: core-data.hpp:149
helics::data_block::swap
void swap(data_block &db2) noexcept
Definition: core-data.hpp:93
helics::data_block::begin
auto begin()
Definition: core-data.hpp:128
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::data_block::to_string
const std::string & to_string() const
Definition: core-data.hpp:122
helics::data_block::operator>
bool operator>(const data_block &db) const
Definition: core-data.hpp:105
helics::Message::swap
void swap(Message &m2) noexcept
Definition: core-data.hpp:163
helics::FilterOperator::process
virtual std::unique_ptr< Message > process(std::unique_ptr< Message > message)=0
helics::FilterOperator::processVector
virtual std::vector< std::unique_ptr< Message > > processVector(std::unique_ptr< Message > message)
Definition: core-data.hpp:211
helics::Message::Message
Message()=default
helics::data_block::cend
auto cend() const
Definition: core-data.hpp:134
helics::data_block::cbegin
auto cbegin() const
Definition: core-data.hpp:132
helics::NullFilterOperator::NullFilterOperator
NullFilterOperator()=default
helics::NullFilterOperator
Definition: core-data.hpp:233
helics::data_block::data
const char * data() const
Definition: core-data.hpp:109
helics::Message::dest
std::string dest
the destination of the message
Definition: core-data.hpp:153
helics::data_block::data_block
data_block(size_t blockSize, char init)
Definition: core-data.hpp:44
helics::data_block::end
auto end()
Definition: core-data.hpp:130
helics::Message
Definition: core-data.hpp:146
helics::isValidIndex
bool isValidIndex(sizeType testSize, const SizedDataType &vec)
Definition: core-data.hpp:249
helics::data_block::operator[]
char & operator[](int index)
Definition: core-data.hpp:124