protobuf根据字段名修改值

int CardMgr::getValue(int nCardId, string strParam)
{
	PlayerDbMsg::CardBean* pCardBean = getCard(nCardId);
	if(NULL == pCardBean)
		return 0;
	const google::protobuf::Descriptor* des = pCardBean->GetDescriptor();
	const google::protobuf::FieldDescriptor* fdes = des->FindFieldByName(strParam);
	if(NULL == fdes)
		return 0;
	if(fdes->type()!= google::protobuf::FieldDescriptor::TYPE_INT32)
		return 0;
	const google::protobuf::Reflection* pReflection = pCardBean->GetReflection();
	return pReflection->GetInt32(*pCardBean, fdes);
}

bool CardMgr::setValue(int nCardId, string strParam, int nValue)
{
	PlayerDbMsg::CardBean* pCardBean = getCard(nCardId);
	if(NULL == pCardBean)
		return false;
	const google::protobuf::Descriptor* des = pCardBean->GetDescriptor();
	const google::protobuf::FieldDescriptor* fdes = des->FindFieldByName(strParam);
	if(NULL == fdes)
		return false;
	if(fdes->type()!= google::protobuf::FieldDescriptor::TYPE_INT32)
		return false;
	const google::protobuf::Reflection* pReflection = pCardBean->GetReflection();
	pReflection->SetInt32(pCardBean, fdes, nValue);
	return true;
}

你可能感兴趣的:(protobuf,算法,javascript,开发语言)