fp universal library ?

classic Classic list List threaded Threaded
36 messages Options
12
Reply | Threaded
Open this post in threaded view
|

fp universal library ?

fredvs
Hello everybody ;-)

I have developed a fp-wrapper, uos, who links to the best audio open-source libraries:

>> https://github.com/fredvs/uos/

This main fp-wrapper uses other fp-wrappers who link to those audio-libraries.
Those fp-audio-wrappers have much more features than the c original audio-wrappers (dynamic loading, reference counters,...).

Now the questions:

1) Is it possible to create a library from main fp-uos wrapper (including audio-libraries fp-wrappers, not the audio-libraries them-self) ?

2) If 1) = true, could you give me some advice how to do it ?

3) May that fp-uos library be accessible for other languages (C, java,...) ?

4) If 3) = true, what is the best way to create foreign-wrappers to that fp-uos-library (wrappers for C, java,...) ?

Many thanks.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

leledumbo
Administrator
> 1) Is it possible to create a library from main fp-uos wrapper (including audio-libraries fp-wrappers, not the audio-libraries them-self) ?

Sure

> 2) If 1) = true, could you give me some advice how to do it ?

Err...read the documentation about how to write a (dynamic) library?

> 3) May that fp-uos library be accessible for other languages (C, java,...) ?

Yes

> 4) If 3) = true, what is the best way to create foreign-wrappers to that fp-uos-library (wrappers for C, java,...) ?

Provide procedural interface with common calling convention, cdecl and stdcall AFAIK are the most supported calling convention, almost (if not) every compiler understands them
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

Paul Breneman
In reply to this post by fredvs
On 01/29/2014 09:16 AM, Fred van Stappen wrote:

> Hello everybody ;-)
>
> I have developed a fp-wrapper, uos, who links to the best audio open-source libraries:
>
>>> https://github.com/fredvs/uos/
>
> This main fp-wrapper uses other fp-wrappers who link to those audio-libraries.
> Those fp-audio-wrappers have much more features than the c original audio-wrappers (dynamic loading, reference counters,...).
>
> Now the questions:
>
> 1) Is it possible to create a library from main fp-uos wrapper (including audio-libraries fp-wrappers, not the audio-libraries them-self) ?
>
> 2) If 1) = true, could you give me some advice how to do it ?
>
> 3) May that fp-uos library be accessible for other languages (C, java,...) ?
>
> 4) If 3) = true, what is the best way to create foreign-wrappers to that fp-uos-library (wrappers for C, java,...) ?
>
> Many thanks.

You might want to take a look here:
   http://winff.org

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs
> You might want to take a look here:
> http://winff.org

Oops, Winff is fp... i did not know that.
So, no excuse, it is possible to do a universal fp library.

@ Leledumdo and Paul => many thanks.

PS : Because of fp, i need to study lot other languages now :-).

Fred



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs
Hello everybody.

Ok, i jump...

I decide to begin the conversion of a unit (wrapper to other libraries + lot of functions who link those libraries) into a universal fp library.

After Googling a while (and finding very poor doc, only few for Delphi, with always the hypra-simple same example...), i do not find how to do with classes...

Here a test (who gives me error)...
__________________________________________
library libtest;

uses
  Classes, ctypes, Math, SysUtils;

type
   TMyClass = class(TThread)
  public
  enabled1: boolean;
  int1, int2: shortint;
  function function1(): integer; cdecl;
  end;

function TMyClass.function1(): integer; cdecl;
begin
if enabled1 then
result := int1 + int2 else result := -1;
end;

exports
  TMyClass.function1();

end.
_______________________________________________________

Stop compil showing error at dot of function TMyClass.

=> libtest.pas(14,18) Fatal: Syntax error, ":" expected but "." found

PS : I promise, if i can do the conversion, to create a nice wiki, for dummies like me : "How to create a universal (complicated) library with fpc"...

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

Michael Van Canneyt


On Sat, 1 Feb 2014, Fred van Stappen wrote:

> Hello everybody.
>
> Ok, i jump...

[snip]

>
> Stop compil showing error at dot of function TMyClass.
>
> => libtest.pas(14,18) Fatal: Syntax error, ":" expected but "." found
>
> PS : I promise, if i can do the conversion, to create a nice wiki, for dummies like me : "How to create a universal
> (complicated) library with fpc"...

You cannot export classes from a library. Only regular functions and procedures.

Michael.
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs
> Date: Sat, 1 Feb 2014 17:00:02 +0100
> From: [hidden email]

> To: [hidden email]
> Subject: Re: [fpc-pascal] fp universal library ?
>
>
>
> On Sat, 1 Feb 2014, Fred van Stappen wrote:
>
> > Hello everybody.
> >
> > Ok, i jump...
>
> [snip]
>
> >
> > Stop compil showing error at dot of function TMyClass.
> >
> > => libtest.pas(14,18) Fatal: Syntax error, ":" expected but "." found
> >
> > PS : I promise, if i can do the conversion, to create a nice wiki, for dummies like me : "How to create a universal
> > (complicated) library with fpc"...
>
> You cannot export classes from a library. Only regular functions and procedures.
>
> Michael.

Aaaaarg, ok and many thanks to answer.

>> Only regular functions and procedures.

Ok but those regular functions and procedures may use other custom classes ?

Many thanks.



_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

Michael Van Canneyt


On Sat, 1 Feb 2014, Fred van Stappen wrote:

> > Date: Sat, 1 Feb 2014 17:00:02 +0100
> > From: [hidden email]
> > To: [hidden email]
> > Subject: Re: [fpc-pascal] fp universal library ?
> >
> >
> >
> > On Sat, 1 Feb 2014, Fred van Stappen wrote:
> >
> > > Hello everybody.
> > >
> > > Ok, i jump...
> >
> > [snip]
> >
> > >
> > > Stop compil showing error at dot of function TMyClass.
> > >
> > > => libtest.pas(14,18) Fatal: Syntax error, ":" expected but "." found
> > >
> > > PS : I promise, if i can do the conversion, to create a nice wiki, for dummies like me : "How to create a universal
> > > (complicated) library with fpc"...
> >
> > You cannot export classes from a library. Only regular functions and procedures.
> >
> > Michael.
>
> Aaaaarg, ok and many thanks to answer.
>
> >> Only regular functions and procedures.
>
> Ok but those regular functions and procedures may use other custom classes ?

Not as arguments or result of the function.

How the function is implemented internally is another matter, but you cannot export classes.

You can export pointers, but you should make it so that the caller cannot cast them back to class instances.
That may work for simple cases, but in it's generality it will not work.

If you want to make a generally usable library, restrict your exported interface to pointers and records.

Michael.

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs
> > > On Sat, 1 Feb 2014, Fred van Stappen wrote:
> > >

> > > > Hello everybody.
> > > >
> > > > Ok, i jump...
> > >
> > > [snip]
> > >
> > > >
> > > > Stop compil showing error at dot of function TMyClass.
> > > >
> > > > => libtest.pas(14,18) Fatal: Syntax error, ":" expected but "." found
> > > >
> > > > PS : I promise, if i can do the conversion, to create a nice wiki, for dummies like me : "How to create a universal
> > > > (complicated) library with fpc"...
> > >
> > > You cannot export classes from a library. Only regular functions and procedures.
> > >
> > > Michael.
> >
> > Aaaaarg, ok and many thanks to answer.
> >
> > >> Only regular functions and procedures.
> >
> > Ok but those regular functions and procedures may use other custom classes ?
>
> Not as arguments or result of the function.
>
> How the function is implemented internally is another matter, but you cannot export classes.
>
> You can export pointers, but you should make it so that the caller cannot cast them back to class instances.
> That may work for simple cases, but in it's generality it will not work.
>
> If you want to make a generally usable library, restrict your exported interface to pointers and records.
>
> Michael.


Yep, thanks Michael.

Hum, may i export dynamic array ?

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

Sven Barth-2
On 01.02.2014 17:20, Fred van Stappen wrote:
> Yep, thanks Michael.
>
> Hum, may i export dynamic array ?

No, you'll need to export these as (typed) pointers and a count.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs
> > Yep, thanks Michael.
> >
> > Hum, may i export dynamic array ?
>
> No, you'll need to export these as (typed) pointers and a count.
>
> Regards,
> Sven

Re-Aaaaargh, ok, (and i understand now why the library-examples are so simple :-) ).

Many thanks for that brilliant explanations.

Ok, so i have to re-consider all the unit and forget the oop way and re-write more procedural (Yes/No ?).


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

dmitry boyarintsev
On Sun, Feb 2, 2014 at 2:44 AM, Fred van Stappen <[hidden email]> wrote:

Many thanks for that brilliant explanations.

Ok, so i have to re-consider all the unit and forget the oop way and re-write more procedural (Yes/No ?).

Yes. The simpler - the better.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

hinst
In reply to this post by fredvs


02.02.2014, 02:45, "Fred van Stappen" <[hidden email]>:

>> > Yep, thanks Michael.
>> >
>> > Hum, may i export dynamic array ?
>>
>> No, you'll need to export these as (typed) pointers and a count.
>>
>> Regards,
>> Sven
>
> Re-Aaaaargh, ok, (and i understand now why the library-examples are so simple :-) ).
>
> Many thanks for that brilliant explanations.
>
> Ok, so i have to re-consider all the unit and forget the oop way and re-write more procedural (Yes/No ?).

I think it should be layered. I tried to visualize the idea; take a look plz
http://s17.postimg.org/oc24gbc27/Untitled_Diagram.png
One should not write all units in procedural style to make library accessible from other languages. A better approach is to create API layers. For example to make FPC library accessible from Java and C# consider creating the following layers:
FPC Library core <-> FPC Library procedural layer
Procedural layer to Java OO
Procedural layer to C#
it should be like first API descends from OO level with FPC objects to procedural layer to make it accessible from java, then there should be java layer which "converts" it back from procedural flattened level to OO level with Java objects.

AFAIK some libraries are designed like this.
To further generalize the idea, consider this scheme:
Language A OO <-> Language A proc. <-> dynamic lib <-> Language B proc. <-> Language B OO

So the point is: of course you should NOT rewrite ALL units. Create a procedural bridge instead and then bridges for other languages like I tried to explain
Of course choosing the right approach for this task it is up to you because no one knows your library better than you
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs
> From: [hidden email]
> To: [hidden email]

> Date: Sun, 2 Feb 2014 15:31:00 +0400
> Subject: Re: [fpc-pascal] fp universal library ?
>
>
>
> 02.02.2014, 02:45, "Fred van Stappen" <[hidden email]>:
> >> > Yep, thanks Michael.
> >> >
> >> > Hum, may i export dynamic array ?
> >>
> >> No, you'll need to export these as (typed) pointers and a count.
> >>
> >> Regards,
> >> Sven
> >
> > Re-Aaaaargh, ok, (and i understand now why the library-examples are so simple :-) ).
> >
> > Many thanks for that brilliant explanations.
> >
> > Ok, so i have to re-consider all the unit and forget the oop way and re-write more procedural (Yes/No ?).
>
> I think it should be layered. I tried to visualize the idea; take a look plz
> http://s17.postimg.org/oc24gbc27/Untitled_Diagram.png
> One should not write all units in procedural style to make library accessible from other languages. A better approach is to create API layers. For example to make FPC library accessible from Java and C# consider creating the following layers:
> FPC Library core <-> FPC Library procedural layer
> Procedural layer to Java OO
> Procedural layer to C#
> it should be like first API descends from OO level with FPC objects to procedural layer to make it accessible from java, then there should be java layer which "converts" it back from procedural flattened level to OO level with Java objects.
>
> AFAIK some libraries are designed like this.
> To further generalize the idea, consider this scheme:
> Language A OO <-> Language A proc. <-> dynamic lib <-> Language B proc. <-> Language B OO
>
> So the point is: of course you should NOT rewrite ALL units. Create a procedural bridge instead and then bridges for other languages like I tried to explain
> Of course choosing the right approach for this task it is up to you because no one knows your library better than you

Hello Hinstance and many thanks for advices.

What i will do is, like you said, not try to transform the actual working unit into a library but create a new file ( like the header file in c ) and in uses add the working unit. And that header file will be the "library" file with link to the working unit, with simple function-procedure...

I will give you news later.

Thanks


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs
In reply to this post by hinst
Hello everybody.

Here next episode of the conversion of a useful fp unit into a universal library...

The goal is to make a library from a unit used by programs.
That unit has lot of complicated procedures-functions, inside some home-made classes, using many dynamic arrays who vary in length and data in real time. Also that procedures call other procedures from foreign dynamic loaded libraries and sent it back to other foreign libraries (after some modification by self-unit of course).

Lets call that unit 'myunit'.

Here first working steps: please advice if im on the wrong way... Many thanks.

1) Create a new file, name it mylib.pas and add this :

library mylib ;

use
myunit; /// the famous unit with all the procedures/functions, classes,...

begin
end.

(You have already done all the work !)

2) Add the functions-procedures you want from myunit in your library-code:

- Here example for general procedure, not inside a class:

library mylib ;

uses
myunit;

procedure mylibprocedure(); cdecl;
begin
  myunit.myprocedure(); /// a general procedure
end;

exports 
mylibprocedure ;
 
begin
end.

- Here example for function inside a class of myunit:

library mylib ;

uses
myunit;

function mylibclassfunction() : integer; cdecl;
var
myclass : TMyUnitClass; /// class defined in myunit (if can be a variable outside the function)

begin
   result := -1 ;
   myclass := TMyUnitClass.Create; 
   result := myclass.myunitclassfunction() ; // a function of myclass 
end;

exports 
mylibclassfunction ;
 
begin
end.

3) Add this compiler option : -fPIC (otherwise it does not compile on my 64 bit Linux)

4) Cross your fingers and compile it.

... And you know what : it compiles and it works !

I’m very happy with fpc, all is simple and accessible.

And, with the recent optimizations, fpc is formula 1 (f1pc)...

...............

Now the boring question :
Is it normal, even without debug-infos, with -Xs parameters, stripped,... that this library :

Library test ;
begin
end;

is 200k big ! ( mine, nude, only with "uses uos" is more than 1 mega ! ).

Many thanks for wonderful f1pc.

Fred

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs
In reply to this post by hinst
Yep, a other useful tip :

To avoid future problems, use name mangling :

Like that :

exports

myFunc name 'myFunction' ;

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

Christo Crause
In reply to this post by fredvs
On Sun, 2014-02-02 at 23:13 +0100, Fred van Stappen wrote:

> - Here example for function inside a class of myunit:
>
> library mylib ;
>
> uses
> myunit;
>
> function mylibclassfunction() : integer; cdecl;
> var
> myclass : TMyUnitClass; /// class defined in myunit (if can be a
> variable outside the function)
>
> begin
>    result := -1 ;
>    myclass := TMyUnitClass.Create;  
>    result := myclass.myunitclassfunction() ; // a function of
> myclass  
> end;
>
> exports  
> mylibclassfunction ;
>  
> begin
> end.
>

Now what I would like in the compiler is a warning: potential memory
leak at myclass := TMyUnitClass.Create;

;)

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs

 > - Here example for function inside a class of myunit:

> >
> > library mylib ;
> >
> > uses
> > myunit;
> >
> > function mylibclassfunction() : integer; cdecl;
> > var
> > myclass : TMyUnitClass; /// class defined in myunit (if can be a
> > variable outside the function)
> >
> > begin
> > result := -1 ;
> > myclass := TMyUnitClass.Create;
> > result := myclass.myunitclassfunction() ; // a function of
> > myclass
> > end;
> >
> > exports
> > mylibclassfunction ;
> >
> > begin
> > end.
> >
>
> Now what I would like in the compiler is a warning: potential memory
> leak at myclass := TMyUnitClass.Create;
>
> ;)


Yep, before to get that sympathetic message, i changed the code, now all the class.create are done inside the unit used by the library and i do not get the message...

Thanks.


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs
> Now what I would like in the compiler is a warning: potential memory
> leak at myclass := TMyUnitClass.Create;
>
> ;)

Oops, prior message was cut...

Yep, before to get that sympathetic message, i changed the code, now all the class.create are done inside the unit used by the library and i will not get the message because i will not forget to free it before to close... ;-)

Thanks.


_______________________________________________ fpc-pascal maillist - [hidden email] http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: fp universal library ?

fredvs
In reply to this post by fredvs

Hello everybody.

Here next episode of the conversion of a useful fp unit into a universal library...

After a hard battle, ... i win. But mainly because of the extremely high quality of fpc. The compiler gives me useful tips to realise that unique library.
Unique because that dynamic loaded library load other libraries dynamically too.

Here some tips :

- If you use classes and threads in the unit you want to convert into library, it is a good idea to use dynamic arrays inside the unit. So the user dont have to create that classes in his program, only set length of array inside the unit (without to forget class.create, of course). That mainly for foreign language who will use your library (they could not create your fpc class but if all is done inside the library, no problem).

- If you use threads, do not forget to add, in linux , in uses section :  cthreads.

- Warning : the exported name of function/proc are case sensible... Good to know before to loose (like me) lot of hair.

- The most strange (for me) is to create a "Pascal header". Usually, for library, i used h2pas, or c2pas, but here no ;-) . So no big problem to create the wrapper.

- Now i have to find : Pas2C, Pas2Java, Pas2Basic, Pas2Net,... (or some brave heart to help me to create wrappers for foreign language.)

Conclusion : fpc is FABULOUS.

Many thanks to all the creators and congrat for the high care you have done for Libraries compilation too.

PS : Im very, very happy, till now, all my wishes become truth with fpc.

PS : I have tested the lib and it works ( dynamic load other lib too ).
I can play songs with effects perfectly. Now i have to find a way to export-import the data-buffer (dynamic array of float) . But that is for the "custom user DSP" feature, all the bild-in DSP provided by uos-unit are working perfectly.

Fred.


 

 

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
12