helics 3.7.0
Loading...
Searching...
No Matches
core-data.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2017-2026,
3Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Energy
4Innovation LLC. See the top-level NOTICE for additional details. All rights reserved.
5SPDX-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
26namespace helics {
27
29class 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
117 public:
120 virtual std::unique_ptr<Message> process(std::unique_ptr<Message> message) override
121 {
122 return message;
123 }
124};
125
132 public:
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
153 public:
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
166 public:
167 FederateOperator() = default;
169 virtual ~FederateOperator() = default;
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
193 public:
194 NullFederateOperator() = default;
196 {
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
212template<class sizeType, class SizedDataType>
213inline bool isValidIndex(sizeType testSize, const SizedDataType& vec)
214{
215 return ((testSize >= sizeType(0)) && (testSize < static_cast<sizeType>(vec.size())));
216}
217
218} // namespace helics
Definition core-data.hpp:165
virtual std::pair< Time, IterationRequest > operate(iteration_time newTime)=0
virtual void finalize()
Definition core-data.hpp:183
virtual IterationRequest initializeOperations()=0
virtual ~FederateOperator()=default
virtual void error_handler(int error_code, std::string_view errorString)
Definition core-data.hpp:185
Definition core-data.hpp:85
virtual std::unique_ptr< Message > process(std::unique_ptr< Message > message)=0
virtual std::vector< std::unique_ptr< Message > > processVector(std::unique_ptr< Message > message)
Definition core-data.hpp:94
virtual bool isMessageGenerating() const
Definition core-data.hpp:111
std::unique_ptr< Message > operator()(std::unique_ptr< Message > message)
Definition core-data.hpp:105
virtual ~FilterOperator()=default
Definition core-data.hpp:29
Time time
the event time the message is sent
Definition core-data.hpp:31
std::int32_t counter
indexing counter not used directly by helics
Definition core-data.hpp:40
void swap(Message &m2) noexcept
Definition core-data.hpp:46
std::string_view to_string() const
Definition core-data.hpp:64
void * backReference
back referencing pointer not used by helics
Definition core-data.hpp:41
std::string original_source
the original source of the message
Definition core-data.hpp:38
std::uint16_t messageValidation
extra field for user object usage, not used by HELICS
Definition core-data.hpp:33
std::string dest
the destination of the message
Definition core-data.hpp:36
SmallBuffer data
the data packet for the message
Definition core-data.hpp:35
std::uint16_t flags
message flags
Definition core-data.hpp:32
void clear()
Definition core-data.hpp:66
bool isValid() const noexcept
Definition core-data.hpp:59
std::int32_t messageID
the messageID for a message
Definition core-data.hpp:34
std::string original_dest
the original destination of a message
Definition core-data.hpp:39
std::string source
the most recent source of the message
Definition core-data.hpp:37
Message()=default
Definition core-data.hpp:192
virtual IterationRequest initializeOperations() override
Definition core-data.hpp:195
virtual std::pair< Time, IterationRequest > operate(iteration_time newTime) override
Definition core-data.hpp:201
Definition core-data.hpp:116
virtual std::unique_ptr< Message > process(std::unique_ptr< Message > message) override
Definition core-data.hpp:120
Definition core-data.hpp:152
virtual SmallBuffer convertToValue(std::unique_ptr< Message >) override
Definition core-data.hpp:156
virtual std::unique_ptr< Message > convertToMessage(const SmallBuffer &) override
Definition core-data.hpp:159
Definition SmallBuffer.hpp:25
void swap(SmallBuffer &sb2) noexcept
Definition SmallBuffer.hpp:338
std::string_view to_string() const
Definition SmallBuffer.hpp:238
bool empty() const
Definition SmallBuffer.hpp:329
Definition core-data.hpp:131
virtual Time computeNewMessageTime(Time valueTime)
Definition core-data.hpp:144
virtual SmallBuffer convertToValue(std::unique_ptr< Message > message)=0
virtual Time computeNewValueTime(Time messageTime)
Definition core-data.hpp:146
virtual ~TranslatorOperator()=default
virtual std::unique_ptr< Message > convertToMessage(const SmallBuffer &value)=0
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition AsyncFedCallInfo.hpp:14
bool isValidIndex(sizeType testSize, const SizedDataType &vec)
Definition core-data.hpp:213
constexpr Time timeZero
Definition helicsTime.hpp:31
IterationRequest
Definition CoreTypes.hpp:103
@ NO_ITERATIONS
indicator that the iterations have completed
TimeRepresentation< count_time< 9 > > Time
Definition helicsTime.hpp:27
Definition helicsTime.hpp:46