7#ifndef HELICS_CPP98_FEDERATE_HPP_
8#define HELICS_CPP98_FEDERATE_HPP_
12#include "Translator.hpp"
14#include "helics/helics.h"
15#include "helicsExceptions.hpp"
21#if defined(HELICS_HAS_FUNCTIONAL) && HELICS_HAS_FUNCTIONAL != 0
36 fedInfo = helicsCreateFederateInfo();
37 helicsFederateInfoSetCoreTypeFromString(fedInfo, coretype.c_str(),
hThrowOnError());
43 fedInfo = helicsCreateFederateInfo();
44 helicsFederateInfoSetCoreType(fedInfo, coretype,
hThrowOnError());
49 fedInfo = helicsFederateClone(otherFedInfo.fedInfo,
hThrowOnError());
55 helicsFederateInfoFree(fedInfo);
59#ifdef HELICS_HAS_RVALUE_REFS
63 fedInfo = otherFedInfo.fedInfo;
64 otherFedInfo.fedInfo = HELICS_NULL_POINTER;
69 helicsFederateInfoFree(fedInfo);
70 fedInfo = otherFedInfo.fedInfo;
71 otherFedInfo.fedInfo = HELICS_NULL_POINTER;
77 void loadFromArgs(
const std::string& argString)
79 helicsFederateInfoLoadFromString(fedInfo, argString.c_str(), HELICS_NULL_POINTER);
85 helicsFederateInfoSetCoreName(fedInfo, coreName.c_str(), HELICS_NULL_POINTER);
90 helicsFederateInfoSetSeparator(fedInfo, sep, HELICS_NULL_POINTER);
96 helicsFederateInfoSetCoreInitString(fedInfo, coreInit.c_str(), HELICS_NULL_POINTER);
101 helicsFederateInfoSetBrokerInitString(fedInfo, brokerInit.c_str(), HELICS_IGNORE_ERROR);
108 helicsFederateInfoSetCoreTypeFromString(fedInfo, coretype.c_str(),
hThrowOnError());
115 helicsFederateInfoSetCoreType(fedInfo, coretype, HELICS_NULL_POINTER);
122 helicsFederateInfoSetBroker(fedInfo, broker.c_str(), HELICS_NULL_POINTER);
129 helicsFederateInfoSetBrokerKey(fedInfo, brokerkey.c_str(), HELICS_NULL_POINTER);
137 helicsFederateInfoSetFlagOption(fedInfo,
140 HELICS_NULL_POINTER);
148 helicsFederateInfoSetTimeProperty(fedInfo, timeProperty, timeValue, HELICS_NULL_POINTER);
156 helicsFederateInfoSetIntegerProperty(fedInfo,
159 HELICS_NULL_POINTER);
169#if defined(HELICS_HAS_FUNCTIONAL) && HELICS_HAS_FUNCTIONAL != 0
172 inline void helicCppQueryCallbackExecutor(
const char* query,
177 auto cback =
reinterpret_cast<std::function<std::string(
const std::string&)
>*>(userData);
178 std::string val(query, stringSize);
179 std::string result = (*cback)(val);
180 helicsQueryBufferFill(buffer, result.c_str(),
static_cast<int>(result.size()),
nullptr);
187 auto cback =
reinterpret_cast<std::function<
void(
HelicsTime,
bool)
>*>(userData);
199 (*cback)(newState, oldState);
203 inline void helicCppTimeRequestEntryCallbackExecutor(
HelicsTime currentTime,
209 (*cback)(currentTime, requestTime, iterating ==
HELICS_TRUE);
212 inline void helicCppTimeRequestReturnCallbackExecutor(
HelicsTime newTime,
216 auto cback =
reinterpret_cast<std::function<
void(
HelicsTime,
bool)
>*>(userData);
226} HelicsIterationTime;
245#ifdef HELICS_HAS_RVALUE_REFS
251 fedObj.fed = HELICS_NULL_POINTER;
258 fedObj.fed = HELICS_NULL_POINTER;
265 if (
fed != HELICS_NULL_POINTER) {
268#if defined(HELICS_HAS_FUNCTIONAL) && HELICS_HAS_FUNCTIONAL != 0
282 helicsFederateSetFlagOption(
fed,
332 void setSeparator(
char sep) { helicsFederateSetSeparator(
fed, sep, HELICS_NULL_POINTER); }
340 helicsFederateRegisterInterfaces(
fed, configString.c_str(),
hThrowOnError());
345 return helicsFederateGetState(
fed, HELICS_NULL_POINTER);
354 return helicsFederateIsAsyncOperationCompleted(
fed, HELICS_NULL_POINTER) !=
HELICS_FALSE;
414 out_iterate = helicsFederateEnterExecutingModeIterative(
fed, iterate,
hThrowOnError());
430 helicsFederateEnterExecutingModeIterativeAsync(
fed, iterate,
hThrowOnError());
443 out_iterate = helicsFederateEnterExecutingModeIterativeComplete(
fed,
hThrowOnError());
488 itTime.
grantedTime = helicsFederateRequestTimeIterative(
509 helicsFederateRequestTimeIterativeAsync(
fed, time, iterate,
hThrowOnError());
532 helicsFederateProcessCommunications(
fed, period, HELICS_IGNORE_ERROR);
535 HELICS_NODISCARD
const char*
getName()
const {
return helicsFederateGetName(
fed); }
538 void protect() { helicsFederateProtect(helicsFederateGetName(
fed), HELICS_IGNORE_ERROR); }
541 void unProtect() { helicsFederateUnProtect(helicsFederateGetName(
fed), HELICS_IGNORE_ERROR); }
556 std::string
query(
const std::string& target,
557 const std::string& queryStr,
583 std::string
query(
const std::string& queryStr,
603 void sendCommand(
const std::string& target,
const std::string& cmd)
605 helicsFederateSendCommand(
fed, target.c_str(), cmd.c_str(), HELICS_IGNORE_ERROR);
616 return helicsFederateGetCommand(
fed, HELICS_IGNORE_ERROR);
627 return helicsFederateWaitCommand(
fed, HELICS_IGNORE_ERROR);
638 return helicsFederateGetCommandSource(
fed, HELICS_IGNORE_ERROR);
641 void setQueryCallback(
646 helicsFederateSetQueryCallback(
fed, queryAnswer, userdata,
hThrowOnError());
649 void setTimeRequestEntryCallback(
void (*timeRequestEntry)(
HelicsTime currentTime,
656 helicsFederateSetTimeRequestEntryCallback(
fed, timeRequestEntry, userdata, hThrowOnError());
659 void setTimeUpdateCallback(
void (*timeUpdate)(
HelicsTime time,
665 helicsFederateSetTimeUpdateCallback(
fed, timeUpdate, userdata, hThrowOnError());
674 helicsFederateSetStateChangeCallback(
fed, stateChange, userdata, hThrowOnError());
677 void setTimeRequestReturnCallback(
void (*timeRequestReturn)(
HelicsTime newTime,
683 helicsFederateSetTimeRequestReturnCallback(
fed,
688#if defined(HELICS_HAS_FUNCTIONAL) && HELICS_HAS_FUNCTIONAL != 0
689 void setQueryCallback(std::function<std::string(
const std::string&)> callback)
692 checkCallbackAllocation();
693 callbackBuffers[queryCallbackLocation] =
694 new std::function<std::string(const std::string&)>(std::move(callback));
695 helicsFederateSetQueryCallback(
fed,
696 details::helicCppQueryCallbackExecutor,
697 callbackBuffers[queryCallbackLocation],
701 void setTimeRequestEntryCallback(
706 checkCallbackAllocation();
707 callbackBuffers[timeRequestEntryCallbackLocation] =
708 new std::function<void(HelicsTime, HelicsTime, bool)>(std::move(callback));
709 helicsFederateSetTimeRequestEntryCallback(
fed,
710 details::helicCppTimeRequestEntryCallbackExecutor,
711 callbackBuffers[timeRequestEntryCallbackLocation],
715 void setTimeUpdateCallback(std::function<
void(
HelicsTime time,
bool iterating)> callback)
718 checkCallbackAllocation();
719 callbackBuffers[timeUpdateCallbackLocation] =
720 new std::function<void(HelicsTime time, bool iterating)>(std::move(callback));
721 helicsFederateSetTimeUpdateCallback(
fed,
722 details::helicCppTimeUpdateCallbackExecutor,
723 callbackBuffers[timeUpdateCallbackLocation],
727 void setStateChangeCallback(
731 checkCallbackAllocation();
732 callbackBuffers[stateChangeCallbackLocation] =
733 new std::function<void(HelicsFederateState, HelicsFederateState)>(std::move(callback));
734 helicsFederateSetStateChangeCallback(
fed,
735 details::helicCppStateChangeCallbackExecutor,
736 callbackBuffers[stateChangeCallbackLocation],
740 void setTimeRequestReturnCallback(
741 std::function<
void(
HelicsTime newTime,
bool iterating)> callback)
744 checkCallbackAllocation();
745 callbackBuffers[timeRequestReturnCallbackLocation] =
746 new std::function<void(HelicsTime, bool)>(std::move(callback));
747 helicsFederateSetTimeRequestReturnCallback(
749 details::helicCppTimeRequestReturnCallbackExecutor,
750 callbackBuffers[timeRequestReturnCallbackLocation],
781 const std::string& filterName = std::string())
796 deliveryEndpoint.c_str(),
821 void setGlobal(
const std::string& valueName,
const std::string& value)
823 helicsFederateSetGlobal(
fed, valueName.c_str(), value.c_str(),
hThrowOnError());
830 void addAlias(
const std::string& interfaceName,
const std::string& alias)
832 helicsFederateAddAlias(
fed, interfaceName.c_str(), alias.c_str(),
hThrowOnError());
840 void setTag(
const std::string& tag,
const std::string& value)
850 HELICS_NODISCARD
const char*
getTag(
const std::string& tag)
const
868 void localError(
int errorCode,
const std::string& errorString)
870 helicsFederateLocalError(
fed, errorCode, errorString.c_str(),
hThrowOnError());
879 helicsFederateGlobalError(
fed, errorCode, errorString.c_str(),
hThrowOnError());
905 helicsFederateLogLevelMessage(
fed, level, message.c_str(),
hThrowOnError());
915#if defined(HELICS_HAS_FUNCTIONAL) && HELICS_HAS_FUNCTIONAL != 0
917 static constexpr int numberOfCallbacks = 5;
918 static constexpr int timeRequestEntryCallbackLocation = 0;
919 static constexpr int timeUpdateCallbackLocation = 1;
920 static constexpr int stateChangeCallbackLocation = 2;
921 static constexpr int queryCallbackLocation = 3;
922 static constexpr int timeRequestReturnCallbackLocation = 4;
923 void** callbackBuffers{
nullptr};
925 void checkCallbackAllocation()
927 if (callbackBuffers ==
nullptr) {
928 callbackBuffers =
new void*[numberOfCallbacks];
929 for (
int ii = 0; ii < numberOfCallbacks; ++ii) {
930 callbackBuffers[ii] =
nullptr;
936 if (callbackBuffers ==
nullptr) {
939 if (callbackBuffers[queryCallbackLocation] !=
nullptr) {
940 auto cback =
reinterpret_cast<std::function<std::string(
const std::string&)
>*>(
941 callbackBuffers[queryCallbackLocation]);
944 if (callbackBuffers[timeRequestEntryCallbackLocation] !=
nullptr) {
946 callbackBuffers[timeRequestEntryCallbackLocation]);
949 if (callbackBuffers[timeUpdateCallbackLocation] !=
nullptr) {
950 auto cback =
reinterpret_cast<std::function<
void(
HelicsTime,
bool)
>*>(
951 callbackBuffers[timeUpdateCallbackLocation]);
954 if (callbackBuffers[stateChangeCallbackLocation] !=
nullptr) {
957 callbackBuffers[stateChangeCallbackLocation]);
960 if (callbackBuffers[timeRequestReturnCallbackLocation] !=
nullptr) {
961 auto cback =
reinterpret_cast<std::function<
void(
HelicsTime,
bool)
>*>(
962 callbackBuffers[timeRequestReturnCallbackLocation]);
965 delete[] callbackBuffers;
970inline void protect(
const std::string& name)
972 helicsFederateProtect(name.c_str(), hThrowOnError());
974inline void unProtect(
const std::string& name)
976 helicsFederateUnProtect(name.c_str(), hThrowOnError());
979inline bool isProtected(
const std::string& name)
981 return (helicsFederateIsProtected(name.c_str(), hThrowOnError()) ==
HELICS_TRUE) ? true :
false;
HelicsFilter helicsFederateRegisterCloningFilter(HelicsFederate fed, const char *name, HelicsError *err)
Definition MessageFiltersExport.cpp:167
int helicsFederateGetFilterCount(HelicsFederate fed)
Definition MessageFiltersExport.cpp:255
HelicsFilter helicsFederateRegisterGlobalCloningFilter(HelicsFederate fed, const char *name, HelicsError *err)
Definition MessageFiltersExport.cpp:187
HelicsFilter helicsFederateGetFilterByIndex(HelicsFederate fed, int index, HelicsError *err)
Definition MessageFiltersExport.cpp:264
HelicsFilter helicsFederateGetFilter(HelicsFederate fed, const char *name, HelicsError *err)
Definition MessageFiltersExport.cpp:231
HelicsFilter helicsFederateRegisterGlobalFilter(HelicsFederate fed, HelicsFilterTypes type, const char *name, HelicsError *err)
Definition MessageFiltersExport.cpp:124
HelicsFilter helicsFederateRegisterFilter(HelicsFederate fed, HelicsFilterTypes type, const char *name, HelicsError *err)
Definition MessageFiltersExport.cpp:103
void * HelicsFederateInfo
Definition api-data.h:77
void * HelicsCore
Definition api-data.h:54
double HelicsTime
Definition api-data.h:106
HelicsIterationResult
Definition api-data.h:139
@ HELICS_ITERATION_RESULT_NEXT_STEP
Definition api-data.h:140
int HelicsBool
Definition api-data.h:120
void * HelicsQuery
Definition api-data.h:83
HelicsFederateState
Definition api-data.h:149
HelicsIterationRequest
Definition api-data.h:128
@ HELICS_ITERATION_REQUEST_NO_ITERATION
Definition api-data.h:129
void * HelicsFederate
Definition api-data.h:65
const HelicsBool HELICS_FALSE
Definition api-data.h:123
void * HelicsQueryBuffer
Definition api-data.h:94
const HelicsBool HELICS_TRUE
Definition api-data.h:122
Definition Filter.hpp:129
Definition cpp98/Federate.hpp:28
FederateInfo(int coretype)
Definition cpp98/Federate.hpp:41
FederateInfo(const FederateInfo &otherFedInfo)
Definition cpp98/Federate.hpp:47
void setCoreType(int coretype)
Definition cpp98/Federate.hpp:113
void setProperty(int integerProperty, int propertyValue)
Definition cpp98/Federate.hpp:154
void setProperty(int timeProperty, HelicsTime timeValue)
Definition cpp98/Federate.hpp:146
~FederateInfo()
Definition cpp98/Federate.hpp:76
void setFlagOption(int flag, bool value=true)
Definition cpp98/Federate.hpp:135
void setBrokerInit(const std::string &brokerInit)
Set a string for the broker initialization in command line argument format.
Definition cpp98/Federate.hpp:99
void setCoreName(const std::string &coreName)
Definition cpp98/Federate.hpp:83
void setBroker(const std::string &broker)
Definition cpp98/Federate.hpp:120
FederateInfo & operator=(const FederateInfo &otherFedInfo)
Definition cpp98/Federate.hpp:52
HELICS_NODISCARD HelicsFederateInfo getInfo()
Definition cpp98/Federate.hpp:163
FederateInfo(const std::string &coretype)
Definition cpp98/Federate.hpp:34
void setBrokerKey(const std::string &brokerkey)
Definition cpp98/Federate.hpp:127
void setCoreType(const std::string &coretype)
Definition cpp98/Federate.hpp:106
FederateInfo()
Definition cpp98/Federate.hpp:31
void setSeparator(char sep)
Set the separator character.
Definition cpp98/Federate.hpp:88
void setCoreInit(const std::string &coreInit)
Definition cpp98/Federate.hpp:94
Definition cpp98/Federate.hpp:229
HELICS_NODISCARD HelicsCore getCore()
Definition cpp98/Federate.hpp:908
void enterInitializingMode()
Definition cpp98/Federate.hpp:359
Filter registerGlobalFilter(HelicsFilterTypes type, const std::string &filterName=std::string())
Definition cpp98/Federate.hpp:780
void unProtect()
Definition cpp98/Federate.hpp:541
std::string query(const std::string &queryStr, HelicsSequencingModes mode=HELICS_SEQUENCING_MODE_FAST) const
Definition cpp98/Federate.hpp:583
HelicsFederate fed
underlying HelicsFederate object
Definition cpp98/Federate.hpp:913
HelicsIterationResult enterExecutingModeComplete()
Definition cpp98/Federate.hpp:439
HELICS_NODISCARD const char * getName() const
Definition cpp98/Federate.hpp:535
void setProperty(int intProperty, int value)
Definition cpp98/Federate.hpp:299
void protect()
Definition cpp98/Federate.hpp:538
Federate & operator=(const Federate &fedObj)
Copy assignment operator.
Definition cpp98/Federate.hpp:239
void enterInitializingModeComplete()
Definition cpp98/Federate.hpp:372
void enterExecutingModeAsync(HelicsIterationRequest iterate=HELICS_ITERATION_REQUEST_NO_ITERATION)
Definition cpp98/Federate.hpp:423
HelicsTime requestTimeComplete()
Definition cpp98/Federate.hpp:514
HELICS_NODISCARD Filter getFilter(const std::string &filterName)
Definition cpp98/Federate.hpp:804
void logWarningMessage(const std::string &message)
Definition cpp98/Federate.hpp:888
void registerInterfaces(const std::string &configString)
Definition cpp98/Federate.hpp:338
void localError(int errorCode, const std::string &errorString)
Definition cpp98/Federate.hpp:868
void addAlias(const std::string &interfaceName, const std::string &alias)
Definition cpp98/Federate.hpp:830
void enterInitializingModeIterativeAsync()
Definition cpp98/Federate.hpp:390
HelicsIterationTime requestTimeIterativeComplete()
Definition cpp98/Federate.hpp:522
HelicsTime requestTimeAdvance(HelicsTime timeDelta)
Definition cpp98/Federate.hpp:477
HelicsTime requestNextStep()
Definition cpp98/Federate.hpp:472
CloningFilter registerCloningFilter(const std::string &deliveryEndpoint)
Definition cpp98/Federate.hpp:770
HELICS_NODISCARD HelicsFederate baseObject() const
Definition cpp98/Federate.hpp:275
void requestTimeAsync(HelicsTime time)
Definition cpp98/Federate.hpp:496
void enterInitializingModeIterativeComplete()
Definition cpp98/Federate.hpp:398
Federate(const Federate &fedObj)
Copy constructor.
Definition cpp98/Federate.hpp:234
HELICS_NODISCARD int getFilterCount() const
Definition cpp98/Federate.hpp:800
HELICS_NODISCARD int getIntegerProperty(int intProperty) const
Definition cpp98/Federate.hpp:321
Filter registerFilter(HelicsFilterTypes type, const std::string &filterName=std::string())
Definition cpp98/Federate.hpp:759
void setProperty(int tProperty, HelicsTime timeValue)
Definition cpp98/Federate.hpp:291
void finalize()
Definition cpp98/Federate.hpp:452
HELICS_NODISCARD const char * getCommand()
Definition cpp98/Federate.hpp:614
void setGlobal(const std::string &valueName, const std::string &value)
Definition cpp98/Federate.hpp:821
void setFlagOption(int flag, bool flagValue=true)
Definition cpp98/Federate.hpp:280
void setTag(const std::string &tag, const std::string &value)
Definition cpp98/Federate.hpp:840
void logErrorMessage(const std::string &message)
Definition cpp98/Federate.hpp:883
void logInfoMessage(const std::string &message)
Definition cpp98/Federate.hpp:893
CloningFilter registerGlobalCloningFilter(const std::string &deliveryEndpoint)
Definition cpp98/Federate.hpp:793
void requestTimeIterativeAsync(HelicsTime time, HelicsIterationRequest iterate)
Definition cpp98/Federate.hpp:507
void logDebugMessage(const std::string &message)
Definition cpp98/Federate.hpp:898
HELICS_NODISCARD bool getFlagOption(int flag) const
Definition cpp98/Federate.hpp:307
void logMessage(int level, const std::string &message)
Definition cpp98/Federate.hpp:903
HELICS_NODISCARD Filter getFilter(int index)
Definition cpp98/Federate.hpp:811
void addDependency(const std::string &fedName)
Definition cpp98/Federate.hpp:859
bool exec_async_iterate
indicator that the federate is in an async operation
Definition cpp98/Federate.hpp:914
void sendCommand(const std::string &target, const std::string &cmd)
Definition cpp98/Federate.hpp:603
HELICS_NODISCARD HelicsFederateState getCurrentMode() const
Definition cpp98/Federate.hpp:343
void enterInitializingModeAsync()
Definition cpp98/Federate.hpp:364
void enterInitializingModeIterative()
Definition cpp98/Federate.hpp:382
HELICS_NODISCARD const char * getCommandSource()
Definition cpp98/Federate.hpp:636
HELICS_NODISCARD HelicsTime getTimeProperty(int tProperty) const
Definition cpp98/Federate.hpp:314
HELICS_NODISCARD const char * waitCommand()
Definition cpp98/Federate.hpp:625
HELICS_NODISCARD const char * getTag(const std::string &tag) const
Definition cpp98/Federate.hpp:850
virtual ~Federate()
Definition cpp98/Federate.hpp:263
std::string query(const std::string &target, const std::string &queryStr, HelicsSequencingModes mode=HELICS_SEQUENCING_MODE_FAST) const
Definition cpp98/Federate.hpp:556
void globalError(int errorCode, const std::string &errorString)
Definition cpp98/Federate.hpp:877
void setSeparator(char sep)
Definition cpp98/Federate.hpp:332
HelicsIterationTime requestTimeIterative(HelicsTime time, HelicsIterationRequest iterate)
Definition cpp98/Federate.hpp:485
HelicsTime requestTime(HelicsTime time)
Definition cpp98/Federate.hpp:466
HELICS_NODISCARD HelicsTime getCurrentTime()
Definition cpp98/Federate.hpp:459
void finalizeAsync()
Definition cpp98/Federate.hpp:455
HELICS_NODISCARD HelicsFederate getObject() const
Definition cpp98/Federate.hpp:910
void finalizeComplete()
Definition cpp98/Federate.hpp:457
HELICS_NODISCARD bool isAsyncOperationCompleted() const
Definition cpp98/Federate.hpp:351
HelicsIterationResult enterExecutingMode(HelicsIterationRequest iterate=HELICS_ITERATION_REQUEST_NO_ITERATION)
Definition cpp98/Federate.hpp:408
Federate() HELICS_NOTHROW
Default constructor.
Definition cpp98/Federate.hpp:232
Definition helicsExceptions.hpp:38
const char * helicsQueryExecute(HelicsQuery query, HelicsFederate fed, HelicsError *err)
Definition helicsExport.cpp:1158
void helicsQuerySetOrdering(HelicsQuery query, int32_t mode, HelicsError *err)
Definition helicsExport.cpp:1322
void helicsQueryFree(HelicsQuery query)
Definition helicsExport.cpp:1331
void helicsFederateFree(HelicsFederate fed)
Definition helicsExport.cpp:1076
HelicsQuery helicsCreateQuery(const char *target, const char *query)
Definition helicsExport.cpp:1145
HelicsFilterTypes
Definition helics_enums.h:393
HelicsSequencingModes
Definition helics_enums.h:429
@ HELICS_SEQUENCING_MODE_FAST
Definition helics_enums.h:431
Definition cpp98/Broker.hpp:18
Definition cpp98/Federate.hpp:223
HelicsIterationResult status
the convergence state
Definition cpp98/Federate.hpp:225
HelicsTime grantedTime
the time of the granted step
Definition cpp98/Federate.hpp:224