This sample demonstrates how to iterate over all fields of a FIX message.
#include "../../Common/Helpers.h"
#include "../../Common/Settings.h"
#include <algorithm>
#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;
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());
}
}
std::for_each(instance.
begin(), instance.
end(), OutputFunctor(messageLevel_));
}
};
class SetGroupInstanceFieldsFunctor
{
size_t groupInstanceCounter_;
public:
SetGroupInstanceFieldsFunctor(
Tag tagForSetting,
const Dictionary & dictionary)
: groupInstanceCounter_(0),
TagForSetting(tagForSetting),
MessageDictionary(dictionary) {}
std::stringstream ss;
ss << groupInstanceCounter_++;
instance.
set(TagForSetting, MessageDictionary.
fieldName(TagForSetting) +
"Value" + ss.str());
}
};
};
int main()
{
try {
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(Values::MsgType::NewOrderSingle, ProtocolVersion::FIX_44);
order.
set(Tags::ClOrdID, dictionary.
fieldName(Tags::ClOrdID) +
"Value");
const size_t CustomTag = 10000;
order.
set(CustomTag,
"CustomTag");
Group partiesGroup = order.setGroup(Tags::NoPartyIDs, 3);
std::for_each(partiesGroup.
begin(), partiesGroup.
end(),
SetGroupInstanceFieldsFunctor(Tags::PartyID, dictionary));
Group partiesSubGroup = partiesGroup[0].setGroup(Tags::NoPartySubIDs, 6);
std::for_each(partiesSubGroup.begin(), partiesSubGroup.end(),
SetGroupInstanceFieldsFunctor(Tags::PartySubID, dictionary));
std::clog << "\n\nMessage with nested repeating group:\n" << order << std::endl;
return order;
}
};