helics  3.0.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 "SmallBuffer.hpp"
10 #include "helics/helics-config.h"
11 #include "helicsTime.hpp"
12 
13 #include <memory>
14 #include <string>
15 #include <utility>
16 #include <vector>
17 
26 namespace helics {
27 
29 class Message {
30  public:
32  std::uint16_t flags{0};
33  std::uint16_t messageValidation{0U};
34  std::int32_t messageID{0};
36  std::string dest;
37  std::string source;
38  std::string original_source;
39  std::string original_dest;
40  std::int32_t counter{0};
41  void* backReference{nullptr};
42 
44  Message() = default;
46  void swap(Message& m2) noexcept
47  {
48  std::swap(time, m2.time);
49  std::swap(flags, m2.flags);
50  std::swap(messageID, m2.messageID);
51  original_source.swap(m2.original_source);
52  source.swap(m2.source);
53  dest.swap(m2.dest);
54  data.swap(m2.data);
55  original_dest.swap(m2.original_dest);
56  }
59  bool isValid() const noexcept
60  {
61  return (!data.empty()) ? true : ((!source.empty()) ? true : (!dest.empty()));
62  }
64  std::string_view to_string() const { return data.to_string(); }
66  void clear()
67  {
68  time = timeZero;
69  flags = 0;
70  messageID = 0;
71  data.resize(0);
72  dest.clear();
73  source.clear();
74  original_source.clear();
75  original_dest.clear();
76  counter = 0;
77  }
78 };
79 
86  public:
88  FilterOperator() = default;
90  virtual ~FilterOperator() = default;
92  virtual std::unique_ptr<Message> process(std::unique_ptr<Message> message) = 0;
94  virtual std::vector<std::unique_ptr<Message>> processVector(std::unique_ptr<Message> message)
95  {
96  std::vector<std::unique_ptr<Message>> ret;
97  auto res = process(std::move(message));
98  if (res) {
99  ret.push_back(std::move(res));
100  }
101  return ret;
102  }
105  std::unique_ptr<Message> operator()(std::unique_ptr<Message> message)
106  {
107  return process(std::move(message));
108  }
111  virtual bool isMessageGenerating() const { return false; }
112 };
113 
116 class NullFilterOperator final: public FilterOperator {
117  public:
119  NullFilterOperator() = default;
120  virtual std::unique_ptr<Message> process(std::unique_ptr<Message> message) override
121  {
122  return message;
123  }
124 };
125 
131 template<class sizeType, class SizedDataType>
132 inline bool isValidIndex(sizeType testSize, const SizedDataType& vec)
133 {
134  return ((testSize >= sizeType(0)) && (testSize < static_cast<sizeType>(vec.size())));
135 }
136 
137 } // namespace helics
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
helics::Message::isValid
bool isValid() const noexcept
Definition: core-data.hpp:59
helics::SmallBuffer
Definition: SmallBuffer.hpp:24
helics::Message::messageID
std::int32_t messageID
the messageID for a message
Definition: core-data.hpp:34
helics::FilterOperator::FilterOperator
FilterOperator()=default
helics::Message::backReference
void * backReference
back referencing pointer not used by helics
Definition: core-data.hpp:41
helics::FilterOperator::operator()
std::unique_ptr< Message > operator()(std::unique_ptr< Message > message)
Definition: core-data.hpp:105
helics::FilterOperator::~FilterOperator
virtual ~FilterOperator()=default
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::Message::source
std::string source
the most recent source of the message
Definition: core-data.hpp:37
helics::FilterOperator::isMessageGenerating
virtual bool isMessageGenerating() const
Definition: core-data.hpp:111
helics::NullFilterOperator::process
virtual std::unique_ptr< Message > process(std::unique_ptr< Message > message) override
Definition: core-data.hpp:120
helics::SmallBuffer::to_string
std::string_view to_string() const
Definition: SmallBuffer.hpp:222
helics::Message::counter
std::int32_t counter
indexing counter not used directly by helics
Definition: core-data.hpp:40
helics::Message::original_dest
std::string original_dest
the original destination of a message
Definition: core-data.hpp:39
helics::Message::messageValidation
std::uint16_t messageValidation
extra field for user object usage, not used by HELICS
Definition: core-data.hpp:33
helics::Message::time
Time time
the event time the message is sent
Definition: core-data.hpp:31
helics::Message::clear
void clear()
Definition: core-data.hpp:66
helics::SmallBuffer::swap
void swap(SmallBuffer &sb2) noexcept
Definition: SmallBuffer.hpp:315
helics::Message::original_source
std::string original_source
the original source of the message
Definition: core-data.hpp:38
helics::SmallBuffer::empty
bool empty() const
Definition: SmallBuffer.hpp:306
helics::FilterOperator
Definition: core-data.hpp:85
helics::Message::flags
std::uint16_t flags
message flags
Definition: core-data.hpp:32
helics::Message::to_string
std::string_view to_string() const
Definition: core-data.hpp:64
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::Message::swap
void swap(Message &m2) noexcept
Definition: core-data.hpp:46
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:94
helics::Message::Message
Message()=default
helics::NullFilterOperator::NullFilterOperator
NullFilterOperator()=default
helics::NullFilterOperator
Definition: core-data.hpp:116
helicsTime.hpp
helics::Message::data
SmallBuffer data
the data packet for the message
Definition: core-data.hpp:35
helics::Message::dest
std::string dest
the destination of the message
Definition: core-data.hpp:36
helics::Message
Definition: core-data.hpp:29
helics::isValidIndex
bool isValidIndex(sizeType testSize, const SizedDataType &vec)
Definition: core-data.hpp:132