Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: XE8: How to create a const & property ?


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Nov 11, 2015 9:57 AM Last Post By: Remy Lebeau (Te...
Jan Dijkstra

Posts: 206
Registered: 11/4/99
XE8: How to create a const & property ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 2:23 AM
To briefly explain what I want, it's best to provide an except of my code.

class PACKAGE TMyCollectionItem : public TCollectionItem
{
private:
 
  SNumUnit FUnit;
 
  SNumUnit const & __fastcall GetUnit (void);
  void  __fastcall SetUnit (SNumUnit const &unit);
 
public:
  
  __property SNumUnit const & Unit = { read = GetUnit, write = SetUnit };
 
};
 
 
SNumUnit const & __fastcall TMyCollectionItem::GetUnit (void)
{
  return FUnit;
}
 
void  __fastcall TMyCollectionItem::SetUnit (SNumUnit const &unit)
{
  FUnit = unit;
}

Basically, I want to provide access to the internal structure FUnit, without providing the ability to directly alter the structure's fields. The property getter does the job perfectly. The property setter routine also compiles without any problems. It even generates the correct assembly code to do the job using the structure's assignment operator with all the CPU registers setup correctly.

However, when I actually try to assign to the property (something like SNumUnit unit; myItem->Unit = unit; ), the compiler generates an error, specifically

[bcc32 Fatal Error] source_file(line_no): F1001 Internal code generator error

Is there any way to make the above work? Where I have a const & reference to an internal structure inside a TObject derived class, but can alter the structure's contents through a property setter routine to produce the proper side effect, which in this case is a call to Changed
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: XE8: How to create a const & property ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 10, 2015 9:38 AM   in response to: Jan Dijkstra in response to: Jan Dijkstra
Jan wrote:

However, when I actually try to assign to the property (something like
SNumUnit unit; myItem->Unit = unit; ), the compiler generates an
error, specifically

[bcc32 Fatal Error] source_file(line_no): F1001 Internal code
generator error

Try using a pointer instead of a reference:

class PACKAGE TMyCollectionItem : public TCollectionItem
{
private:
  SNumUnit FUnit;
 
  SNumUnit const * __fastcall GetUnit (void);
  void  __fastcall SetUnit (SNumUnit const *unit);
 
public:
    __property SNumUnit const * Unit = { read = GetUnit, write = SetUnit };
};
 
SNumUnit const * __fastcall TMyCollectionItem::GetUnit (void)
{
    return &FUnit;
}
 
void  __fastcall TMyCollectionItem::SetUnit (SNumUnit const *unit)
{
    if (unit)
        FUnit = *unit;
}


--
Remy Lebeau (TeamB)
Jan Dijkstra

Posts: 206
Registered: 11/4/99
Re: XE8: How to create a const & property ?  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2015 1:24 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Jan wrote:

However, when I actually try to assign to the property (something like
SNumUnit unit; myItem->Unit = unit; ), the compiler generates an
error, specifically

[bcc32 Fatal Error] source_file(line_no): F1001 Internal code
generator error

Try using a pointer instead of a reference:

class PACKAGE TMyCollectionItem : public TCollectionItem
{
private:
  SNumUnit FUnit;
 
  SNumUnit const * __fastcall GetUnit (void);
  void  __fastcall SetUnit (SNumUnit const *unit);
 
public:
    __property SNumUnit const * Unit = { read = GetUnit, write = SetUnit };
};
 
SNumUnit const * __fastcall TMyCollectionItem::GetUnit (void)
{
    return &FUnit;
}
 
void  __fastcall TMyCollectionItem::SetUnit (SNumUnit const *unit)
{
    if (unit)
        FUnit = *unit;
}


--
Remy Lebeau (TeamB)

This does indeed work.

Does this mean that there is a bug in the code generator when using a & instead of a *

I would very much prefer using the & variant, as all my SNumUnit structures are either locals (in a function body) or fields of larger objects. They will never be allocated on the heap.

Edit: Right now I've sort of worked around the problem by simply making the SetUnit property setter public, and calling it directly instead of implied through assigning to the property.

Edited by: Jan Dijkstra on Nov 11, 2015 1:25 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: XE8: How to create a const & property ? [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2015 9:57 AM   in response to: Jan Dijkstra in response to: Jan Dijkstra
Jan wrote:

Does this mean that there is a bug in the code generator when using
a & instead of a *

Sounds like it, yes.

I would very much prefer using the & variant, as all my SNumUnit
structures are either locals (in a function body) or fields of larger
objects. They will never be allocated on the heap.

Data does not have to be on the heap to use a pointer to it. However, TMyCollectionItem
is a TObject descendant, so TMyCollectionItem objects will always be created
on the heap, never on the stack, and so the FHeap member will always be on
the heap.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02