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

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;

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},

View File

@ -223,6 +223,7 @@ public __ext_core_SetNTVOptional()
MarkNativeAsOptional("PbAddAngle");
MarkNativeAsOptional("PbAddVector");
MarkNativeAsOptional("PbAddVector2D");
MarkNativeAsOptional("PbRemoveRepeatedFieldValue");
MarkNativeAsOptional("PbReadMessage");
MarkNativeAsOptional("PbReadRepeatedMessage");
MarkNativeAsOptional("PbAddMessage");

View File

@ -325,6 +325,17 @@ native PbAddVector(Handle:pb, const String:field[], const Float:vec[3]);
*/
native PbAddVector2D(Handle:pb, const String:field[], const Float:vec[2]);
/**
* Removes a value by index from a protobuf message repeated field.
*
* @param pb protobuf handle.
* @param field Field name.
* @param index Index into repeated field.
* @noreturn
* @error Invalid or incorrect Handle, non-existent field, or incorrect field type.
*/
native PbRemoveRepeatedFieldValue(Handle:pb, const String:field[], index);
/**
* Retrieve a handle to an embedded protobuf message in a protobuf message.
*