This sample demonstrates how to iterate over all fields of a FIX message.
#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;
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) {
std::for_each(instance.begin(), instance.end(), OutputFunctor(messageLevel_));
}
};
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 {
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;
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");
Group partiesGroup = order.setGroup(FIX50::Tags::NoPartyIDs, 3);
std::for_each(partiesGroup.begin(), partiesGroup.end(),
SetGroupInstanceFieldsFunctor(FIX50::Tags::PartyID, dictionary));
Group partiesSubGroup = partiesGroup[0].setGroup(FIX50::Tags::NoPartySubIDs, 6);
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;
}
};