2
一般來說,我做了以下內容:在C++ BLPAPI有沒有辦法避免使用hasElement或捕捉錯誤
if (msg.hasElement(BID_SIZE))
{
bid_sz=msg.getElementAsInt32(BID_SIZE);
}
如果我不使用檢查首先使用hasElement - 那麼字段不存在會扔
一般來說,我做了以下內容:在C++ BLPAPI有沒有辦法避免使用hasElement或捕捉錯誤
if (msg.hasElement(BID_SIZE))
{
bid_sz=msg.getElementAsInt32(BID_SIZE);
}
如果我不使用檢查首先使用hasElement - 那麼字段不存在會扔
而不是使用getElementAsInt32
使用getValueAs
的錯誤:
Element field;
int err=msg.asElement().getElement(&field, BID_SIZE);
if (!err)
{
int valerr=field.getValueAs(&bid_sz); // will call getValueAs for the type of bid
}
這將避免在消息中尋找BID_SIZE兩次,而不會拋出w ^沒有找到母雞場。
另外,您可以通過各個領域的循環,並檢查它是哪場:
Element asElem=msg.asElement();
size_t num=asElem.numElements();
for (size_t i=0; i < num; ++i)
{
Element field=asElem.getElement(i);
Name field_name=field.name();
if (field_name == BID_SIZE)
{
bid_sz=field.getValueAsInt32();
}
// check other fields
// put more likely fields at the top
}