<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">2016-04-10 14:45 GMT+02:00 Florian Klämpfl <span dir="ltr"><<a href="mailto:florian@freepascal.org" target="_blank">florian@freepascal.org</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><span class="">
</span>I think this is the wrong way:<br>
- AddRef means only to increase the ref. count of the passed data structure<br>
- Copy is no deep copy, it means only: copy the current data structure, if it references managed<br>
types, their ref. count is increased but they are not copied<br>
<br>
Example with dyn. arrays:<br>
- AddRef increases the ref. count of the passed array, not its members<br>
- Copy creates a copy of the array, if the members of the array are managed, their ref. count is<br>
increased<br>
<br>
So I would call the operator Copy, in case of custom implemented ref. counted records, this would be<br>
an AddRef operation. For a real copy, a procedure like Clone should be declared.<br clear="all"></blockquote><div><br></div><div>Proposed implementation is compatible with internal rtti.inc usage (FPC_ADDREF and FPC_COPY). In my example I mean dyn array of records and related usage of operators only in context of items of those array. <br><br></div><div>Important note:<br></div><div>operator Initialize is called after system int_initialize for required record fields<br>operator Finalize is called before system int_finalize for required record fields<br></div><div>operator AddRef is called after int_addref for required fields<br></div><div><div>operator Copy is called after fpc_Copy_internal for required fields<br><br></div><div>note for note:<br></div><div>in current FPC implementation int_initialize = FPC_INITIALIZE, int_finalize = FPC_FINALIZE, int_addref = FPC_ADDREF, fpc_Copy_internal = FPC_COPY<br></div><div><br></div></div><div>Let me explain this with complex example:<br><br></div><div>=== code begin ===<br></div><div>type<br></div><div> TFoo = record // record with all management operators. Existence of management operators means that the record became managed<br></div><div> public<br><div> {... some field definitions ... }<br></div> private<br></div><div> class operator Initialize(var aFoo: TFoo);<br> class operator Finalize(var aFoo: TFoo);<br> class operator AddRef(var aFoo: TFoo);<br> class operator Copy(constref aSrc: TFoo; var aDst: TFoo);<br></div> end;<br><div><br>procedure TestValue(Value: TFoo); begin end;<br>procedure TestVar(var Value: TFoo); begin end;<br>procedure TestConst(const Value: TFoo); begin end;<br>procedure TestOut(out Value: TFoo); begin end;<br>procedure TestConstref(constref Value: TFoo); begin end;<br><br> TFooArray = array of TFoo;<br></div><div class="gmail_extra"><br>var<br> Foos: TFooArray;<br> Foos2: TFooArray;<br>begin<br> SetLength(Foos, 5); // call 5x FPC_INITIALIZE and 5x TFoo.Initialize<br> SetLength(Foos, 6); // call 1x FPC_INITIALIZE and 1x TFoo.Initialize<br> SetLength(Foos, 5); // call 1x FPC_FINALIZE and 1x TFoo.Finalize<br> Foos2 := Copy(Foos); // call 5x FPC_ADDREF and 5x TFoo.AddRef<br> Foos2[0] := Foos[1]; // call 1x FPC_COPY and 1x TFoo.Copy<br><br> // call 1x FPC_ADDREF and 1x TFoo.AddRef<br> TestValue(Foos2[1]); <br> // call 1x FPC_FINALIZE and 1x TFoo.Finalize<br><br></div></div></div><div class="gmail_extra"> // ... none<br></div><div class="gmail_extra"> TestVar(Foos2[1]);<br><div class="gmail_extra"> // ... none<br><br></div><div class="gmail_extra"> // ... none<br></div> TestConst(Foos2[1]);<br><div class="gmail_extra"> // ... none<br></div><br> // call 1x FPC_FINALIZE and 1x TFoo.Finalize<br> // call 1x FPC_INITIALIZE and 1x TFoo.Initialize<br> TestOut(Foos2[1]);<br><div class="gmail_extra"> // ... none<br></div><br><div class="gmail_extra"> // ... none<br></div> TestConstref(Foos2[1]);<br><div class="gmail_extra"> // ... none<br><br></div>end; // call 10x FPC_FINALIZE and 10x TFoo.Finalize (for Foos and Foos2)<br></div><div class="gmail_extra">=== code end ===<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">As far as I understand your proposal is to use TFoo.Clone instead of TFoo.Copy and TFoo.Copy instead of TFoo.AddRef. That is correct? IMO a little confusing from system.pp point of view ;)<br></div><div class="gmail_extra"><br>-- <br><div class="gmail_signature"><div dir="ltr"><div>Best regards,<br>Maciej Izak</div></div></div>
</div></div>