.accoding to your suggestion,
i found in the class RecordSet ,RepeatedFieldPtr<Record*> record_ was
defined,and it stored its submessage :record,it alse provied
mutable_record() api ,
it just returned RepeatedFieldPtr<Record*> *
i also found when we use this api : Record *pRecordB = recordsetB-
>add_record(),it actually do this record_->Add()
so in my test ,i did like this:
recordsetB->mutable_record()->AddAllocated(pRecordA)
when i release recordsetB,first i user this api :
while(recordsetB->mutable_record()->size() > 0)
{
    recordsetB->mutable_record()->releaseLast();
}
delete recordsetB;
recordsetB= NULL;
delete recordsetA;
recordsetB = NULL;

by doing this ,i'll never use copyfrom ,and it actually did!

so i have a question,actually there are two method to add pointer
record Add()、AddAllocated(),and first is public to user like us ,but
the second is not.
to avoid conflict when we release RecordSet,
we have to do one more step,if record is allocated,we use
releaseLast(),and if record is generated by itself ,we can release it
directly.



On 7月12日, 下午8时46分, Pherl Liu <[email protected]> wrote:
> In C++ protobuf, each message owns its fields. That said, two different
> messages cannot share a sub-message, which will complicate the system when
> releasing resources.
>
> In your case, if you always need to copy the records, there may be a better
> design. For instance, keep the records in a global pool, and change the
> RecordSet to only contain the ids, i.e. change the repeated Records into
> repeated int32. Also if you don't need to use them simultaneously, you can
> use add_allocated_record() and swap.
>
> On Sun, Jul 10, 2011 at 4:04 PM, [email protected] <[email protected]>wrote:
>
>
>
> > hello:
>
> > we defined two object in field.proto
> > message Record{
> > optional int id = 1;
> > }
> > message RecordSet{
> > repeated Record record= 1;
> > }
>
> > so when we define a Record pointer,we do like this:
> > RecordSet recordsetA,recordsetB;
> > Recrod *pRecordA = recordsetA.add_record();
> > pRecordA->set_id(111);
>
> > actually ,recordsetB has the same member as fieldsetA,we can only do
> > like this:
> > Record *pRecordB = recordsetB.add_record();
> > pRecordB->CopyFrom(*pRecordA);
>
> > we found that it cost  much time to  copy.
>
> > if there is this method,we can solve this problem:
>
> > recordasetB.add_record(pRecordA);
>
> > in this way ,we don't need to copy。
>
> > i've read topic in this forum, add_record() this api just for thread
> > safe and don't worry about the pRecordA being accidently changed。
>
> > so hope for your answer.
>
> > thans!!
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Protocol Buffers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> >http://groups.google.com/group/protobuf?hl=en.- 隐藏被引用文字 -
>
> - 显示引用的文字 -

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/protobuf?hl=en.

Reply via email to