| helics 3.6.1

core-data.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2025,
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:
31 Time time = timeZero;
32 std::uint16_t flags{0};
33 std::uint16_t messageValidation{0U};
34 std::int32_t messageID{0};
35 SmallBuffer data;
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
85 class FilterOperator {
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 class TranslatorOperator {
132 public:
134 TranslatorOperator() = default;
136 virtual ~TranslatorOperator() = default;
138 virtual SmallBuffer convertToValue(std::unique_ptr<Message> message) = 0;
139
141 virtual std::unique_ptr<Message> convertToMessage(const SmallBuffer& value) = 0;
142
144 virtual Time computeNewMessageTime(Time valueTime) { return valueTime + minDelay; }
146 virtual Time computeNewValueTime(Time messageTime) { return messageTime + minDelay; }
147 Time minDelay{Time::epsilon()};
148 };
149
152 class NullTranslatorOperator final: public TranslatorOperator {
153 public:
155 NullTranslatorOperator() = default;
156 virtual SmallBuffer convertToValue(std::unique_ptr<Message> /*message*/) override { return {}; }
157
159 virtual std::unique_ptr<Message> convertToMessage(const SmallBuffer& /*value*/) override
160 {
161 return nullptr;
162 }
163 };
164
165 class FederateOperator {
166 public:
167 FederateOperator() = default;
169 virtual ~FederateOperator() = default;
173 virtual IterationRequest initializeOperations() = 0;
174
179 virtual std::pair<Time, IterationRequest> operate(iteration_time newTime) = 0;
180 /*NOTE: the following two functions will be called only once and they will be the last call in
181 * the federate lifecycle*/
183 virtual void finalize() {}
185 virtual void error_handler([[maybe_unused]] int error_code,
186 [[maybe_unused]] std::string_view errorString)
187 {
188 }
189 };
190
192 class NullFederateOperator final: public FederateOperator {
193 public:
194 NullFederateOperator() = default;
195 virtual IterationRequest initializeOperations() override
196 {
197 return IterationRequest::NO_ITERATIONS;
198 };
199
200 virtual std::pair<Time, IterationRequest>
201 operate([[maybe_unused]] iteration_time newTime) override
202 {
203 return {Time::maxVal(), IterationRequest::NO_ITERATIONS};
204 }
205 };
206
212 template<class sizeType, class SizedDataType>
213 inline bool isValidIndex(sizeType testSize, const SizedDataType& vec)
214 {
215 return ((testSize >= sizeType(0)) && (testSize < static_cast<sizeType>(vec.size())));
216 }
217
218 } // namespace helics
helics::FederateOperator
Definition: core-data.hpp:165
helics::FederateOperator::operate
virtual std::pair< Time, IterationRequest > operate(iteration_time newTime)=0
helics::FederateOperator::finalize
virtual void finalize()
Definition: core-data.hpp:183
helics::FederateOperator::initializeOperations
virtual IterationRequest initializeOperations()=0
helics::FederateOperator::~FederateOperator
virtual ~FederateOperator()=default
helics::FederateOperator::error_handler
virtual void error_handler([[maybe_unused]] int error_code, [[maybe_unused]] std::string_view errorString)
Definition: core-data.hpp:185
helics::FilterOperator
Definition: core-data.hpp:85
helics::FilterOperator::operator()
std::unique_ptr< Message > operator()(std::unique_ptr< Message > message)
Definition: core-data.hpp:105
helics::FilterOperator::processVector
virtual std::vector< std::unique_ptr< Message > > processVector(std::unique_ptr< Message > message)
Definition: core-data.hpp:94
helics::FilterOperator::isMessageGenerating
virtual bool isMessageGenerating() const
Definition: core-data.hpp:111
helics::FilterOperator::FilterOperator
FilterOperator()=default
helics::FilterOperator::process
virtual std::unique_ptr< Message > process(std::unique_ptr< Message > message)=0
helics::FilterOperator::~FilterOperator
virtual ~FilterOperator()=default
helics::Message
Definition: core-data.hpp:29
helics::Message::time
Time time
the event time the message is sent
Definition: core-data.hpp:31
helics::Message::counter
std::int32_t counter
indexing counter not used directly by helics
Definition: core-data.hpp:40
helics::Message::swap
void swap(Message &m2) noexcept
Definition: core-data.hpp:46
helics::Message::to_string
std::string_view to_string() const
Definition: core-data.hpp:64
helics::Message::backReference
void * backReference
back referencing pointer not used by helics
Definition: core-data.hpp:41
helics::Message::original_source
std::string original_source
the original source of the message
Definition: core-data.hpp:38
helics::Message::messageValidation
std::uint16_t messageValidation
extra field for user object usage, not used by HELICS
Definition: core-data.hpp:33
helics::Message::dest
std::string dest
the destination of the message
Definition: core-data.hpp:36
helics::Message::data
SmallBuffer data
the data packet for the message
Definition: core-data.hpp:35
helics::Message::flags
std::uint16_t flags
message flags
Definition: core-data.hpp:32
helics::Message::clear
void clear()
Definition: core-data.hpp:66
helics::Message::isValid
bool isValid() const noexcept
Definition: core-data.hpp:59
helics::Message::messageID
std::int32_t messageID
the messageID for a message
Definition: core-data.hpp:34
helics::Message::original_dest
std::string original_dest
the original destination of a message
Definition: core-data.hpp:39
helics::Message::source
std::string source
the most recent source of the message
Definition: core-data.hpp:37
helics::Message::Message
Message()=default
helics::NullFederateOperator
Definition: core-data.hpp:192
helics::NullFederateOperator::initializeOperations
virtual IterationRequest initializeOperations() override
Definition: core-data.hpp:195
helics::NullFilterOperator
Definition: core-data.hpp:116
helics::NullFilterOperator::NullFilterOperator
NullFilterOperator()=default
helics::NullFilterOperator::process
virtual std::unique_ptr< Message > process(std::unique_ptr< Message > message) override
Definition: core-data.hpp:120
helics::NullTranslatorOperator
Definition: core-data.hpp:152
helics::NullTranslatorOperator::convertToMessage
virtual std::unique_ptr< Message > convertToMessage(const SmallBuffer &) override
Definition: core-data.hpp:159
helics::NullTranslatorOperator::convertToValue
virtual SmallBuffer convertToValue(std::unique_ptr< Message >) override
Definition: core-data.hpp:156
helics::NullTranslatorOperator::NullTranslatorOperator
NullTranslatorOperator()=default
helics::SmallBuffer
Definition: SmallBuffer.hpp:25
helics::SmallBuffer::swap
void swap(SmallBuffer &sb2) noexcept
Definition: SmallBuffer.hpp:338
helics::SmallBuffer::to_string
std::string_view to_string() const
Definition: SmallBuffer.hpp:238
helics::SmallBuffer::empty
bool empty() const
Definition: SmallBuffer.hpp:329
helics::TranslatorOperator
Definition: core-data.hpp:131
helics::TranslatorOperator::convertToMessage
virtual std::unique_ptr< Message > convertToMessage(const SmallBuffer &value)=0
helics::TranslatorOperator::computeNewMessageTime
virtual Time computeNewMessageTime(Time valueTime)
Definition: core-data.hpp:144
helics::TranslatorOperator::convertToValue
virtual SmallBuffer convertToValue(std::unique_ptr< Message > message)=0
helics::TranslatorOperator::computeNewValueTime
virtual Time computeNewValueTime(Time messageTime)
Definition: core-data.hpp:146
helics::TranslatorOperator::~TranslatorOperator
virtual ~TranslatorOperator()=default
helics::TranslatorOperator::TranslatorOperator
TranslatorOperator()=default
helicsTime.hpp
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::isValidIndex
bool isValidIndex(sizeType testSize, const SizedDataType &vec)
Definition: core-data.hpp:213
helics::timeZero
constexpr Time timeZero
Definition: helicsTime.hpp:31
helics::IterationRequest
IterationRequest
Definition: CoreTypes.hpp:103
helics::IterationRequest::NO_ITERATIONS
@ NO_ITERATIONS
indicator that the iterations have completed
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::iteration_time
Definition: helicsTime.hpp:43