[fpc-pascal] Property Tags
Jorge Aldo G. de F. Junior
jagfj80 at gmail.com
Sun Aug 1 00:52:56 CEST 2010
I'm writing a ActiveRecord system for Freepascal (Something similar to
what RubyOnRails do), and i am having some problems (I'm not following
MVC logic, more of a Exposed Object Logic) :
1 - How to simulate multiple inheritance ? Is there an alternative way
(Some other techniques, like interfaces etc) ?
I have, for exemple, in the visual components the following :
TVisualComponent wich can be hidden or shown (Property Visible)
and another component called TCollapsibleComponent wich can have its
childs shown (while keeping itself visible) or hidden.
So, sometimes i want another component to descend from
TVisualComponent, sometimes i want a component to descend from
TCollapsibleComponent, while sometimes i want it to descend from
I solved this by making TCollpasibleComponent descent from
TVisualComponent, but... this is quite hackish, as if i want the
descendent to not allow visibility toggles i have to override the
method wich makes it visible or not and replace it with a dummy method
wich does nothing :
TVisibleComponent = Class(something)
Procedure DoVisible; Virtual;
Procedure DoInvisible; Virtual;
TCollapsibleComponent = Class(TVisibleComponent)
Procedure DoCollapse; Virtual;
Procedure DoUncollapse; Virtual;
TSomeComponentWichShouldOnlyBeCollapsible = Class(TCollapsibleComponent)
Procedure DoInvisible; Override; // <- dummy method wich dont call
inherited so as to deny invisibility
In this case the problem is easily solved, but there are other parts
of the class tree of my project that makes this a headache (i have to
make properties dissappear from published section etc, write dummy
There are any plas to add mixins to freepascal ?
2 - Published properties tagging :
Some time ago i saw some discussion about adding property tags to
freepascal. This is a GREAT idea !
In the model i am using, the published properties of an object can go
to various places :
They can be edited in runtime by their name (something like
myobject.properties[<nameofpublished>] := 'something')
They can be stored in a database,
They can be shown in the screen,
They can represent a many to one relationship between object collecions, etc.
So, currently i have to use some hacks to keep track of wich
properties do what :
TMyObject = Class(TExposedProperties) // this object can end up in the
screen template,, edited back thru a CGI Post, in a database or in
streamed to a session file
fEditable: TArrayOfString; // Holds wich property can be edited by CGI
fStreamable : TArrayOfString; // holds wich property goes to/from
the session data stream
fPersist : TArrayOfString; // holds wich property goes to/from the database
fTemplate : TArrayOfString; // holds wich property goes to the template system
Property SomeProp : Integer Read Getter Write Setter;
TMyObjectChild = Class(TMyObject)
AddTemplate('someprop'); For each property...
And this is not much maintanable...
What i propose :
a kind of visibility overloading.
the objectpascal syntax for class is like this (not exactly) :
<classname> = Class(<ancestor>)
[ <visibility> ]
<classfield> [ ; <classfield> [...] ]
i propose :
<classname> = Class(<ancestor>)
Tag <visibilitytag> [ Write fVisibilityWriter ] ;
[ <tag> ] Property <nameeetc>;
MyClass = Class(TObject)
fPrivatefield : Integer;
Procedure NewStreamableProperty(Const aPropname : String;
aPropkind : TRTTIInfo);
Tag Streamable Write NewStreamableProperty;
Streamable Property PrivateField Read fPrivateField Write fPrivateField;
The property is internally tagged with "streamable" for later use and
after the object is constructed (with RTTI Info) the writer (if
defined), NewStreamableProperty, is called with Property PrivateField
as parameters (his name and TRTTInfo), so as to allow the programmer
to store this information for his own use (this allows derived classes
to simply tag its properties and allow the root class to manage this).
The writer is just a way to tell wich method to call to inform that
the property is tagged, but is optional, the real tagging should be
interntal to TObject to allow the following :
If PrivateField Is Streamable Then // <- overload "is" to allow
comparing properties to tags...
Tags should be like sets :
Myclass = Class(TObject)
fField : Integer;
Streamable Storable Visible Editable Property Field: Integer Read
fField Write fField;
Well, this is just a proposal, any other form of property Tagging
would help me. the experience tells me that this is usefull. The only
other way around without property tagging is using some RTTI Hacks :
TEditableInteger = Integer;
TStreamableInteger = Integer;
TEditableStreamableInteger = Integer;
And looking the RTTI for the property type name (THIS SUUUUUX A LOT,
imagine the ammount of combination types needed...)
3 - Better rtti :
Sometimes i need to invoke methods by name, and sincerely i never
discovered the correct way to do this with RTTI...
I am currently using a hack based on DispatchStr, and this works quite
well, apart from the hackish way to return results (i can pass a Array
of Const as parameters, but its not easy to return data back...)...
if you could add something similar to DispatchStr with the ability to
return variants, this would be ok to me...
TObjectMessage = Record
MessageString : String;
MessageParameters : Array Of Variant; // <- original was a pointer
MyObject = Class(TObject)
Function SomeMethod(Var Message : TObjectMessage): Variant; Message
then use :
Data := MyObject.DispatchStr('onsomething', [ 'parameter1', parameter2 ]);
would be ok and solve most of my problems...
Well, i i know you program for free and you are not at my disposal
etc. But i am trying to share some experience here from what i
discovered while trying to develop this activerecords thing...
thank you in advance,
More information about the fpc-pascal