This sample demonstrates how to iterate over all fields of a FIX message.
#include "../../Common/Helpers.h"
#include "../../Common/Settings.h"
#include <sstream>
using namespace Settings;
using namespace OnixS::FIX::FIX44;
namespace {
Message createMessageWithNestedRepeatingGroups();
static const std::string MessageLevelIndent = " ";
class OutputFunctor
{
std::string messageLevel_;
public:
explicit OutputFunctor(const std::string & messageLevel): messageLevel_(messageLevel) {}
void operator()(const Field & field) {
std::clog << messageLevel_ << field.
tag <<
"=" << field.
value.
toString() << std::endl;
Group group;
messageLevel_.append(MessageLevelIndent);
std::clog << messageLevel_ << "Begin of group:" << std::endl;
OutputFunctor output(messageLevel_);
for(const auto & instance : group)
output(instance);
std::clog << messageLevel_ << "End of group" << std::endl;
messageLevel_.erase(messageLevel_.size() - MessageLevelIndent.size());
}
}
void operator()(const GroupInstance & instance) {
OutputFunctor output(messageLevel_);
for(const auto & field : instance)
output(field);
}
};
class SetGroupInstanceFieldsFunctor
{
size_t groupInstanceCounter_;
const Dictionary & MessageDictionary;
public:
SetGroupInstanceFieldsFunctor(Tag tagForSetting, const Dictionary & dictionary)
: groupInstanceCounter_(0),
TagForSetting(tagForSetting),
MessageDictionary(dictionary) {}
void operator()(GroupInstance & instance) {
std::stringstream ss;
ss << groupInstanceCounter_++;
instance.
set(TagForSetting, MessageDictionary.
fieldName(TagForSetting) +
"Value" + ss.str());
}
};
};
int main()
{
try {
EngineShutdownGuard engineShutdownGuard;
Message msg = createMessageWithNestedRepeatingGroups();
std::clog << "\n\nIterating...\n" << std::endl;
std::clog << "Begin of message:" << std::endl;
OutputFunctor output("");
for(const auto & field : msg)
output(field);
std::clog << "End of message" << std::endl;
}
catch(const std::exception & ex) {
processSampleException(ex.what());
return 1;
}
return 0;
}
namespace {
Message createMessageWithNestedRepeatingGroups()
{
order.set(Tags::ClOrdID, dictionary.
fieldName(Tags::ClOrdID) +
"Value");
constexpr size_t CustomTag = 10000;
order.set(CustomTag, "CustomTag");
Group partiesGroup = order.setGroup(Tags::NoPartyIDs, 3);
SetGroupInstanceFieldsFunctor setPartyId(Tags::PartyID, dictionary);
for(auto & instance : partiesGroup)
setPartyId(instance);
Group partiesSubGroup = partiesGroup[0].setGroup(Tags::NoPartySubIDs, 6);
SetGroupInstanceFieldsFunctor setPartySubId(Tags::PartySubID, dictionary);
for(auto & instance : partiesSubGroup)
setPartySubId(instance);
std::clog << "\n\nMessage with nested repeating group:\n" << order << std::endl;
return order;
}
};