Add PbRemoveRepeatedFieldValue native (bug 6066, r=asherkin).

This commit is contained in:
Nicholas Hastings
2014-03-21 13:23:48 -04:00
parent fb0fd0c6d2
commit 9215ddcf8a
4 changed files with 44 additions and 0 deletions
+18
View File
@@ -1093,6 +1093,24 @@ public:
return msg->GetReflection()->FieldSize(*msg, field);
}
inline bool RemoveRepeatedFieldValue(const char *pszFieldName, int index)
{
GETCHECK_FIELD();
CHECK_FIELD_REPEATED();
CHECK_REPEATED_ELEMENT(index);
// Protobuf guarantees that repeated field values will stay in order and so must we.
const protobuf::Reflection *pReflection = msg->GetReflection();
for (int i = index; i < elemCount - 1; ++i)
{
pReflection->SwapElements(msg, field, i, i + 1);
}
pReflection->RemoveLast(msg, field);
return true;
}
private:
protobuf::Message *msg;
PBHandleList childHandles;
+14
View File
@@ -716,6 +716,19 @@ static cell_t smn_PbAddVector2D(IPluginContext *pCtx, const cell_t *params)
return 1;
}
static cell_t smn_PbRemoveRepeatedFieldValue(IPluginContext *pCtx, const cell_t *params)
{
GET_MSG_FROM_HANDLE_OR_ERR();
GET_FIELD_NAME_OR_ERR();
if (!msg->RemoveRepeatedFieldValue(strField, params[3]))
{
return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
}
return 1;
}
static cell_t smn_PbReadMessage(IPluginContext *pCtx, const cell_t *params)
{
GET_MSG_FROM_HANDLE_OR_ERR();
@@ -794,6 +807,7 @@ REGISTER_NATIVES(protobufnatives)
{"PbAddAngle", smn_PbAddAngle},
{"PbAddVector", smn_PbAddVector},
{"PbAddVector2D", smn_PbAddVector2D},
{"PbRemoveRepeatedFieldValue", smn_PbRemoveRepeatedFieldValue},
{"PbReadMessage", smn_PbReadMessage},
{"PbReadRepeatedMessage", smn_PbReadRepeatedMessage},
{"PbAddMessage", smn_PbAddMessage},