Add PbRemoveRepeatedFieldValue native (bug 6066, r=asherkin).
authorNicholas Hastings <skamonkey@gmail.com>
Fri Mar 21 13:23:48 2014 -0400 (2014-03-21)
changeset 4293f61ae385f12e
parent 4292 f6604240fae2
child 4294 fa845b627daf
Add PbRemoveRepeatedFieldValue native (bug 6066, r=asherkin).
core/UserMessagePBHelpers.h
core/smn_protobuf.cpp
plugins/include/core.inc
plugins/include/protobuf.inc
     1.1 --- a/core/UserMessagePBHelpers.h	Fri Mar 21 13:10:44 2014 -0400
     1.2 +++ b/core/UserMessagePBHelpers.h	Fri Mar 21 13:23:48 2014 -0400
     1.3 @@ -1093,6 +1093,24 @@
     1.4  		return msg->GetReflection()->FieldSize(*msg, field);
     1.5  	}
     1.6  
     1.7 +	inline bool RemoveRepeatedFieldValue(const char *pszFieldName, int index)
     1.8 +	{
     1.9 +		GETCHECK_FIELD();
    1.10 +		CHECK_FIELD_REPEATED();
    1.11 +		CHECK_REPEATED_ELEMENT(index);
    1.12 +
    1.13 +		// Protobuf guarantees that repeated field values will stay in order and so must we.
    1.14 +		const protobuf::Reflection *pReflection = msg->GetReflection();
    1.15 +		for (int i = index; i < elemCount - 1; ++i)
    1.16 +		{
    1.17 +			pReflection->SwapElements(msg, field, i, i + 1);
    1.18 +		}
    1.19 +
    1.20 +		pReflection->RemoveLast(msg, field);
    1.21 +
    1.22 +		return true;
    1.23 +	}
    1.24 +
    1.25  private:
    1.26  	protobuf::Message *msg;
    1.27  	PBHandleList childHandles;
     2.1 --- a/core/smn_protobuf.cpp	Fri Mar 21 13:10:44 2014 -0400
     2.2 +++ b/core/smn_protobuf.cpp	Fri Mar 21 13:23:48 2014 -0400
     2.3 @@ -716,6 +716,19 @@
     2.4  	return 1;
     2.5  }
     2.6  
     2.7 +static cell_t smn_PbRemoveRepeatedFieldValue(IPluginContext *pCtx, const cell_t *params)
     2.8 +{
     2.9 +	GET_MSG_FROM_HANDLE_OR_ERR();
    2.10 +	GET_FIELD_NAME_OR_ERR();
    2.11 +
    2.12 +	if (!msg->RemoveRepeatedFieldValue(strField, params[3]))
    2.13 +	{
    2.14 +		return pCtx->ThrowNativeError("Invalid field \"%s\" for message \"%s\"", strField, msg->GetProtobufMessage()->GetTypeName().c_str());
    2.15 +	}
    2.16 +
    2.17 +	return 1;
    2.18 +}
    2.19 +
    2.20  static cell_t smn_PbReadMessage(IPluginContext *pCtx, const cell_t *params)
    2.21  {
    2.22  	GET_MSG_FROM_HANDLE_OR_ERR();
    2.23 @@ -794,6 +807,7 @@
    2.24  	{"PbAddAngle",					smn_PbAddAngle},
    2.25  	{"PbAddVector",					smn_PbAddVector},
    2.26  	{"PbAddVector2D",				smn_PbAddVector2D},
    2.27 +	{"PbRemoveRepeatedFieldValue",	smn_PbRemoveRepeatedFieldValue},
    2.28  	{"PbReadMessage",				smn_PbReadMessage},
    2.29  	{"PbReadRepeatedMessage",		smn_PbReadRepeatedMessage},
    2.30  	{"PbAddMessage",				smn_PbAddMessage},
     3.1 --- a/plugins/include/core.inc	Fri Mar 21 13:10:44 2014 -0400
     3.2 +++ b/plugins/include/core.inc	Fri Mar 21 13:23:48 2014 -0400
     3.3 @@ -223,6 +223,7 @@
     3.4  	MarkNativeAsOptional("PbAddAngle");
     3.5  	MarkNativeAsOptional("PbAddVector");
     3.6  	MarkNativeAsOptional("PbAddVector2D");
     3.7 +	MarkNativeAsOptional("PbRemoveRepeatedFieldValue");
     3.8  	MarkNativeAsOptional("PbReadMessage");
     3.9  	MarkNativeAsOptional("PbReadRepeatedMessage");
    3.10  	MarkNativeAsOptional("PbAddMessage");
     4.1 --- a/plugins/include/protobuf.inc	Fri Mar 21 13:10:44 2014 -0400
     4.2 +++ b/plugins/include/protobuf.inc	Fri Mar 21 13:23:48 2014 -0400
     4.3 @@ -326,6 +326,17 @@
     4.4  native PbAddVector2D(Handle:pb, const String:field[], const Float:vec[2]);
     4.5  
     4.6  /**
     4.7 + * Removes a value by index from a protobuf message repeated field.
     4.8 + *
     4.9 + * @param pb			protobuf handle.
    4.10 + * @param field			Field name.
    4.11 + * @param index			Index into repeated field.
    4.12 + * @noreturn
    4.13 + * @error				Invalid or incorrect Handle, non-existent field, or incorrect field type.
    4.14 + */ 
    4.15 +native PbRemoveRepeatedFieldValue(Handle:pb, const String:field[], index);
    4.16 +
    4.17 +/**
    4.18   * Retrieve a handle to an embedded protobuf message in a protobuf message.
    4.19   *
    4.20   * @param pb			protobuf handle.