OnixS C++ FIX Engine  4.12.0
API Documentation
Message Fields Iteration Sample

This sample demonstrates how to iterate over all fields of a FIX message.

Source code:

#include <OnixS/FIXEngine/FIX/FIX50.h>
#include "../../Common/Helpers.h"
#include "../../Common/Settings.h"
using namespace Settings;
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;
if(field.value.toGroup(group)) {
messageLevel_.append(MessageLevelIndent);
std::clog << messageLevel_ << "Begin of group:" << std::endl;
// Iterate over all group instances of the repeating group.
std::for_each(group.begin(), group.end(), OutputFunctor(messageLevel_));
std::clog << messageLevel_ << "End of group" << std::endl;
messageLevel_.erase(messageLevel_.size() - MessageLevelIndent.size());
}
}
void operator()(const GroupInstance & instance) {
// Iterate over all fields of the group instance.
std::for_each(instance.begin(), instance.end(), OutputFunctor(messageLevel_));
}
};
class SetGroupInstanceFieldsFunctor
{
size_t groupInstanceCounter_;
const Tag TagForSetting;
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 {
EngineSettings settings;
settings.listenPort(-1)
.licenseStore(LicenseStore);
Engine::init(settings);
Message msg = createMessageWithNestedRepeatingGroups();
std::clog << "\n\nIterating...\n" << std::endl;
std::clog << "Begin of message:" << std::endl;
// Iterate over all fields of the message.
std::for_each(msg.begin(), msg.end(), OutputFunctor(""));
std::clog << "End of message" << std::endl;
}
catch(const std::exception & ex) {
processSampleException(ex.what());
}
Engine::shutdown();
return 0;
}
namespace {
Message createMessageWithNestedRepeatingGroups()
{
Message order(FIX50::Values::MsgType::NewOrderSingle, ProtocolVersion::FIX_50);
Dictionary dictionary = order.dictionary();
order.set(FIX50::Tags::ClOrdID, dictionary.fieldName(FIX50::Tags::ClOrdID) + "Value");
const size_t CustomTag = 10000;
order.set(CustomTag, "CustomTag");
// Create the repeating group.
Group partiesGroup = order.setGroup(FIX50::Tags::NoPartyIDs, 3);
// Set the given field in all group instances of the repeating group.
std::for_each(partiesGroup.begin(), partiesGroup.end(),
SetGroupInstanceFieldsFunctor(FIX50::Tags::PartyID, dictionary));
// Create the nested repeating group.
Group partiesSubGroup = partiesGroup[0].setGroup(FIX50::Tags::NoPartySubIDs, 6);
// Set the given field in all group instances of the nested repeating group.
std::for_each(partiesSubGroup.begin(), partiesSubGroup.end(),
SetGroupInstanceFieldsFunctor(FIX50::Tags::PartySubID, dictionary));
std::clog << "\n\nMessage with nested repeating group:\n" << order << std::endl;
return order;
}
};