| helics 3.6.1

Publications.hpp
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 "../core/core-exceptions.hpp"
10 #include "Federate.hpp"
11 #include "HelicsPrimaryTypes.hpp"
12
13 #include <memory>
14 #include <string>
15 #include <string_view>
16 #include <vector>
17
18 namespace units {
19 class precise_unit;
20 } // namespace units
21
22 namespace helics {
23 class ValueFederate;
25 class HELICS_CXX_EXPORT Publication: public Interface {
26 protected:
27 ValueFederate* fed{nullptr};
28 private:
29 int referenceIndex{-1};
30 void* dataReference{nullptr};
31 double delta{-1.0};
32 protected:
33 DataType pubType{DataType::HELICS_ANY};
34 bool changeDetectionEnabled{false};
35 bool disableAssign{false};
36 private:
37 size_t customTypeHash{
38 0};
39 mutable defV prevValue;
40 std::string pubUnits;
41 std::shared_ptr<units::precise_unit>
42 pubUnitType;
43 public:
44 Publication() = default;
51 Publication(ValueFederate* valueFed,
52 InterfaceHandle id,
53 std::string_view key,
54 std::string_view type,
55 std::string_view units);
56
62 Publication(ValueFederate* valueFed,
63 std::string_view key,
64 std::string_view type,
65 std::string_view units = std::string_view{});
66
73 template<class FedPtr>
74 Publication(FedPtr valueFed,
75 std::string_view key,
76 std::string_view type = std::string_view(),
77 std::string_view units = std::string_view()):
78 Publication(std::addressof(*valueFed), key, type, units)
79 {
80 static_assert(
81 std::is_base_of<ValueFederate, std::remove_reference_t<decltype(*valueFed)>>::value,
82 "first argument must be a pointer to a ValueFederate");
83 }
91 Publication(InterfaceVisibility locality,
92 ValueFederate* valueFed,
93 std::string_view key,
94 std::string_view type,
95 std::string_view units = std::string_view());
104 template<class FedPtr>
105 Publication(InterfaceVisibility locality,
106 FedPtr& valueFed,
107 std::string_view key,
108 std::string_view type,
109 std::string_view units = std::string_view()):
110 Publication(locality, std::addressof(*valueFed), key, type, units)
111 {
112 static_assert(
113 std::is_base_of<ValueFederate, std::remove_reference_t<decltype(*valueFed)>>::value,
114 "first argument must be a pointer to a ValueFederate");
115 }
116
123 Publication(ValueFederate* valueFed,
124 std::string_view key,
125 DataType type,
126 std::string_view units = std::string_view()):
127 Publication(valueFed, key, typeNameStringRef(type), units)
128 {
129 }
136 template<class FedPtr>
137 Publication(FedPtr& valueFed,
138 std::string_view key,
139 DataType type,
140 std::string_view units = std::string_view()):
141 Publication(valueFed, key, typeNameStringRef(type), units)
142 {
143 }
151 Publication(InterfaceVisibility locality,
152 ValueFederate* valueFed,
153 std::string_view key,
154 DataType type,
155 std::string_view units = std::string_view()):
156 Publication(locality, valueFed, key, typeNameStringRef(type), units)
157 {
158 }
159
167 template<class FedPtr>
168 Publication(InterfaceVisibility locality,
169 FedPtr& valueFed,
170 std::string_view key,
171 DataType type,
172 std::string_view units = std::string_view()):
173 Publication(locality, valueFed, key, typeNameStringRef(type), units)
174 {
175 }
176
178 const std::string& getType() const { return getExtractionType(); }
180 const std::string& getUnits() const { return pubUnits; }
182 void addTarget(std::string_view target) { addDestinationTarget(target); }
184 void addInputTarget(std::string_view target) { addDestinationTarget(target); }
191 void publish(double val);
192 void publish(const std::vector<std::string>& val);
193 void publish(const std::vector<double>& val);
194 void publish(const double* vals, int size);
195 void publish(const std::vector<std::complex<double>>& val);
197 void publishComplex(const double* vals, int size);
198 void publish(std::complex<double> val);
199
200 void publish(bool val);
201 void publish(Time val);
202 void publish(char val);
203 void publish(const NamedPoint& np);
204 void publish(std::string_view field, double val);
210 void publish(double val, const std::string& units);
211 void publish(double val, const units::precise_unit& units);
212
214 template<class X>
215 std::enable_if_t<(std::is_constructible_v<std::string_view, X>), void> publish(const X& val)
216 {
217 publishString(val);
218 }
219
221 template<class X>
222 std::enable_if_t<(std::is_same_v<defV, remove_cv_ref<X>>), void> publish(const X& val)
223 {
224 publishDefV(val);
225 }
226
228 template<class X>
229 std::enable_if_t<(std::is_integral_v<X> && !std::is_same_v<remove_cv_ref<X>, char>), void>
230 publish(X val)
231 {
232 publishInt(static_cast<int64_t>(val));
233 }
234
237 void setMinimumChange(double deltaV) noexcept
238 {
239 if (delta < 0.0) {
240 changeDetectionEnabled = true;
241 }
242 delta = deltaV;
243 if (delta < 0.0) {
244 changeDetectionEnabled = false;
245 }
246 }
251 void enableChangeDetection(bool enabled = true) noexcept { changeDetectionEnabled = enabled; }
252
253 virtual const std::string& getDisplayName() const override { return getName(); }
254
255 private:
261 void publishInt(int64_t val);
262 void publishString(std::string_view val);
263 void publishDefV(const defV& val);
264 friend class ValueFederateManager;
265 };
266
267 } // namespace helics
HelicsPrimaryTypes.hpp
naming a set of types that are interchangeable and recognizable inside the HELICS application API and...
helics::Interface
Definition: application_api/Federate.hpp:880
helics::NamedPoint
Definition: helicsTypes.hpp:110
helics::Publication
Definition: Publications.hpp:25
helics::Publication::getDisplayName
virtual const std::string & getDisplayName() const override
Definition: Publications.hpp:253
helics::Publication::getType
const std::string & getType() const
Definition: Publications.hpp:178
helics::Publication::publish
std::enable_if_t<(std::is_integral_v< X > &&!std::is_same_v< remove_cv_ref< X >, char >), void > publish(X val)
Definition: Publications.hpp:230
helics::Publication::Publication
Publication(FedPtr &valueFed, std::string_view key, DataType type, std::string_view units=std::string_view())
Definition: Publications.hpp:137
helics::Publication::Publication
Publication(ValueFederate *valueFed, std::string_view key, DataType type, std::string_view units=std::string_view())
Definition: Publications.hpp:123
helics::Publication::addInputTarget
void addInputTarget(std::string_view target)
Definition: Publications.hpp:184
helics::Publication::getUnits
const std::string & getUnits() const
Definition: Publications.hpp:180
helics::Publication::Publication
Publication(InterfaceVisibility locality, ValueFederate *valueFed, std::string_view key, DataType type, std::string_view units=std::string_view())
Definition: Publications.hpp:151
helics::Publication::setMinimumChange
void setMinimumChange(double deltaV) noexcept
Definition: Publications.hpp:237
helics::Publication::publish
std::enable_if_t<(std::is_constructible_v< std::string_view, X >), void > publish(const X &val)
Definition: Publications.hpp:215
helics::Publication::publish
std::enable_if_t<(std::is_same_v< defV, remove_cv_ref< X >>), void > publish(const X &val)
Definition: Publications.hpp:222
helics::Publication::addTarget
void addTarget(std::string_view target)
Definition: Publications.hpp:182
helics::Publication::Publication
Publication(FedPtr valueFed, std::string_view key, std::string_view type=std::string_view(), std::string_view units=std::string_view())
Definition: Publications.hpp:74
helics::Publication::Publication
Publication(InterfaceVisibility locality, FedPtr &valueFed, std::string_view key, std::string_view type, std::string_view units=std::string_view())
Definition: Publications.hpp:105
helics::Publication::Publication
Publication(InterfaceVisibility locality, FedPtr &valueFed, std::string_view key, DataType type, std::string_view units=std::string_view())
Definition: Publications.hpp:168
helics::Publication::enableChangeDetection
void enableChangeDetection(bool enabled=true) noexcept
Definition: Publications.hpp:251
helics::ValueFederateManager
Definition: ValueFederateManager.hpp:64
helics::ValueFederate
Definition: application_api/ValueFederate.hpp:28
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::DataType
DataType
Definition: helicsTypes.hpp:273
helics::typeNameStringRef
const std::string & typeNameStringRef(DataType type)
Definition: helicsTypes.cpp:64
helics::InterfaceVisibility
InterfaceVisibility
Definition: helicsTypes.hpp:40
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
helics::defV
std::variant< double, int64_t, std::string, std::complex< double >, std::vector< double >, std::vector< std::complex< double > >, NamedPoint > defV
Definition: HelicsPrimaryTypes.hpp:37
helics::publish
void publish(ValueFederate &fed, std::string_view pubName, Us... pargs)
Definition: application_api/ValueFederate.hpp:571