[fpc-pascal] Re: using macros
On 1 okt '12, dhkblas...@zeelandnet.nl wrote: > I need to translate this: #define mymalloc(x,y) (x*)malloc((y)* sizeof(x)) > > Is it possible using macro's in FPC or do I need to convert it to a procedure? How would I cast the pointer in the latter case (without having to change the sourcecode significantly)? > > Regards, Darius Thanks all, I decided to use an inline function and not to cast the return pointer. However, I've got another interesting one. Does someone have an idea how I could implement this? #define MYPRINT(var1) printf(# var1 ":%dn", var1) Calling the macro as such : MYPRINT(sizeof(int)); will yield: sizeof(int):4, which is useful for debugging. Will I need RTTI for this? I never used it before so I might be wrong. Darius ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] using macros
In our previous episode, dhkblas...@zeelandnet.nl said: A bit late, but: > I need to translate this: #define mymalloc(x,y) (x*)malloc((y)* > sizeof(x)) Impossible. 1. FPC macros can't be parameterized 2. less problematic, but macros don't import over unit borders, you'd have to put the macro in an inc file and include it everyhwere. The usual trick, converting to procedure is hard because 3. There is no way to pass something typeless to a function and then get the size of it. This make emulating it with functions hard. 4. the generic part (returning a typed pointer) will be impossible, requiring a cast. > Is it possible using macro's in FPC or do I need to convert > it to a procedure? Neither is possible I think for the general case. The most sane way is to make it a three parameter function and pass sizeof(x); That will require two fixes though, every z:=malloc(x,y) will have to be changed to z:=px(malloc(x,y,sizeof(x))); > How would I cast the pointer in the latter case (without having to change > the sourcecode significantly)? You can't. Macros are illdefined, and thus hard to port or convert. GIGO principle. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Re: using macros
Those macros are simply to reduce typing, better use true debugger. Even if RTTI can (which I believe not), it would be an abuse of the feature. Macros in FPC aren't as flexible as in C for a purpose IMHO: it's often abused as the way you show. -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/using-macros-tp5711420p5711433.html Sent from the Free Pascal - General mailing list archive at Nabble.com. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] using macros
In our previous episode, Marco van de Voort said: > 3. There is no way to pass something typeless to a function and then get >the size of it. This make emulating it with functions hard. Btw, Modula2 allows to pass any type of defined size to an ARRAY OF BYTE open array (and thus such functions also get the size). Its one of those things I missed going back to Pascal. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal] Using database-specific functionality with TSQLConnector
Hi list, For a fpcunit database test listener I'm implementing multi-connector support using TSQLConnector. This seems to work nicely. If the Firebird connector is chosen and the hostname is empty, I assume Firebird embedded is used and if not present, a database needs to be created using .CreateDB For this, I need TIBConnection-specific CreateDB. Currently I've managed to do that with the Proxy property of the TSQLConnector, which however is protected. Also it appears CheckProxy is needed: { THackSQLConnector } THackSQLConnector = class(TSQLConnector) //we really only want access to a working proxy private function GetProxyConnector: TSQLConnection; public Property ProxyConnection : TSQLConnection Read GetProxyConnector; end; function THackSQLConnector.GetProxyConnector: TSQLConnection; begin CheckProxy; //seems to be required to get valid proxy result:=inherited Proxy; end; and var FConn: THackSQLConnector; ... IBConn:TIBConnection; ... IBConn:=TIBConnection(FConn.ProxyConnection); IBConn.UserName:=FConn.UserName; IBConn.Password:=FConn.Password; IBConn.DatabaseName:=FConn.DatabaseName; ... IBConn.CreateDB; Is that the best/easiest way? Would it make sense to make the proxy code in TSQLConnector public? ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal