From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,2f84446733b06aca X-Google-Attributes: gid103376,public X-Google-Thread: 109fba,2f84446733b06aca X-Google-Attributes: gid109fba,public From: Stephen Leake Subject: Re: Converting C++ class to Ada Date: 1996/12/11 Message-ID: <32AECEBE.2B5D@gsfc.nasa.gov>#1/1 X-Deja-AN: 203508170 references: <32ADF183.7195@lmtas.lmco.com> content-type: text/plain; charset=us-ascii organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA mime-version: 1.0 reply-to: Stephen.Leake@gsfc.nasa.gov newsgroups: comp.lang.ada,comp.lang.c++ x-mailer: Mozilla 3.0 (Win95; U) Date: 1996-12-11T00:00:00+00:00 List-Id: Ken Garlington wrote: > > In an attempt to better understand both C++ and Ada, I've been > converting some C++ code into Ada. I'm struggling with this particular > class: > > class CTableXY > { > public: > CMotor& XMotor; > CMotor& YMotor; > CSwitch& XLimit; > CSwitch& YLimit; > CTableXY( CMotor& xmotor, CMotor& ymotor, > CSwitch& xlim, CSwitch& ylim ) : > XMotor(xmotor), YMotor(ymotor), > XLimit(xlim), YLimit(ylim) {} > void SetVelocityX( float vel ) > { XMotor.SetSpeed( vel ); } > void SetVelocityY( float vel ) > { YMotor.SetSpeed( vel ); } > int GetSwitchX( void ) { return > XLimit.isOpen(); } > int GetSwitchY( void ) { return > YLimit.isOpen(); } > }; > > It seems to me that the straightforward Ada translation is something > like: > > with Motor, Switch; > package Table_XY is > > type Motor_Access_Type is access all Motor.Object_Type'Class; > type Switch_Access_Type is access all Switch.Object_Type'Class; > > type Object_Type is record > X_Motor : Motor_Access_Type; > Y_Motor : Motor_Access_Type; > X_Limit : Switch_Access_Type; > Y_Limit : Switch_Access_Type; > end record; > > procedure Set_Velocity_X ( Object : in out Object_Type; > Velocity : in Motor.Velocity_Type ); > -- and so forth > > end Table_XY; I'm not clear why a class would have reference members, but if I were building this type, the components of Object_Type would NOT be access types; I want the class to have control over when they change. I'll ignore this issue in the following. > > My main issues: > > (1) What do I do about the constructor? As I understand C++, > I need to force the user to supply values for the components of > Object_Type when something of that type is declared. How do I do > this? I believe the only way to force the user to provide values when declaring an object is to use discriminants. However, this is not what you want to do, because you cannot easily change the discriminants later. If you make Object_Type private, you can include an Initialized component that defaults to False, and then provide an Initialize procedure that sets all the other components. Then all the subprograms that operate on Object_Type can check Initialize and raise an exception. Not the same as C++, but similar. Ahah! the Initialized component could be a discriminant without defaults; then the user must provide a full aggregate when the object is declared: type Object_Type (Initialized : BOOLEAN) is record case Initialized is when False => null; when True => X_Motor : Motor_Access_Type; Y_Motor : Motor_Access_Type; X_Limit : Switch_Access_Type; Y_Limit : Switch_Access_Type; end case; end record; > > (2) If I'm reading it correctly, the C++ interface exposes the details > of > the table, although it provides procedures to manipulate it. > However, > I'm thinking of making Object_Type limited private anyway, since > otherwise it seems silly to have the procedures/functions that are > declared here. (More of a style issue than anything...) > > Any comments? Definitely make it private, and probably limited. If you are controlling a motor, you cannot let the user change the velocity without checking the limits! Similarly, only one copy of the "current velocity" may exist for each motor; assignment is a bad idea. > > -- > LMTAS - The Fighter Enterprise - "Our Brand Means Quality" > For job listings, other info: http://www.lmtas.com or > http://www.lmco.com -- - Stephe