Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2001-06

OOP for dummies...

20 messages · 9 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

Sorry for the large post. And my apologies if this is a simple question, but it sure stumps me.

The following is for Win32 and Go32 on a machine running Win2K Pro and FPC 1.0.4...

I'm attempting, for the first time, to use object oriented programming. Yes, I'm ashamed to admit it, but it's true. Up until recently I never thought I needed OOP, but the more I read about it the more I think that perhaps I might benefit from it. And speaking of reading... before anyone asks, yes I've read the docs on classes and objects -- a few times actually -- but I can't seem to grasp all the concepts, so I'm going to post this in hopes someone can point me in the right direction.

I'm trying to create my own dynamic array. Well, it's actually more like a dynamic array of records (I know dynamic arrays are coming in the next version of the compiler, but this is something I've been struggling with for a while so I want to see if I can solve my problem now). Basically, I want to be able to read a file into memory and have it stop only when I run out of memory and not when I've reached the limit of my array (array [1..whatever] of ^ToRecordType).

My research indicates that a class called TCollection has the possibility of doing what I want, but I can't figure out exactly how to do it. The attached examples programs both compile but do virtually nothing. One uses objects, the other classes. If someone has a few moments I would really appreciate it they could take a look and see what I'm doing wrong.

TIA.

Jim

Reply by email

Keith Bowes
to Unknown author
Show quoted text 14 lines
Sorry for the large post. And my apologies if this is a simple question, but it sure stumps me. The following is for Win32 and Go32 on a machine running Win2K Pro and FPC 1.0.4... I'm attempting, for the first time, to use object oriented programming. Yes, I'm ashamed to admit it, but it's true. Up until recently I never thought I needed OOP, but the more I read about it the more I think that perhaps I might benefit from it. And speaking of reading... before anyone asks, yes I've read the docs on classes and objects -- a few times

actually

-- but I can't seem to grasp all the concepts, so I'm going to post this

in

Show quoted text 7 lines
hopes someone can point me in the right direction. I'm trying to create my own dynamic array. Well, it's actually more like a dynamic array of records (I know dynamic arrays are coming in the next version of the compiler, but this is something I've been struggling with for a while so I want to see if I can solve my problem now). Basically, I want to be able to read a file into memory and have it stop only when I

run

Show quoted text 9 lines
out of memory and not when I've reached the limit of my array (array [1..whatever] of ^ToRecordType). My research indicates that a class called TCollection has the possibility of doing what I want, but I can't figure out exactly how to do it. The attached examples programs both compile but do virtually nothing. One uses objects, the other classes. If someone has a few moments I would really appreciate it they could take a look and see what I'm doing wrong.

OK, I've looked at your class sample (not the object one though, since I don't know much about objects). The biggest problem I see is in your file-reading loop; the Data field always contains the content of the last line. The updated file contains a loop that seems to read my sample text file fine, as well as a few other minor changes.

Show quoted text 5 lines
TIA. Jim

Reply by email

Unknown author
Show quoted text 6 lines
OK, I've looked at your class sample (not the object one though, since I don't know much about objects). The biggest problem I see is in your file-reading loop; the Data field always contains the content of the last line. The updated file contains a loop that seems to read my sample text file fine, as well as a few other minor changes.

Thanks for responding.

I took a look at what you had sent, but it appears as though you missed the original intent of my question. I can print the data as it's read from the file, but not after it has been. I'm going to use this for a file viewer, so I need to read the file into memory and then access the data via some type of index. In an array I can use ArrayName [Index] to point to any element of the array but I can't seem to duplicate that functionality for a 'dynamic' structure.

Jim

Reply by email

Full_Name
to Unknown author

I just got FPC to link against a C shared object that I created natively on BeOS. This is pretty amazing as it worked FIRST TIME!! (lol)

Just to share the info..

Created a simple Shared Object in the BeIDE.. consisted of the following:

/* begin libtest.c */

#define BUILDING_LIB 1
#include "libtest.h"
int TestFunc(void)
{
  return 20; //nominal value.. could be anything..
};

/* end libtest.c */

/* begin libtest.h */

//conditional defines for building shared object/header
//I borrowed this from a tutorial on the 'net..

#if BUILDING_LIB
#define UGLY_IMPEXP __declspec(dllexport)
#else
#define UGLY_IMPEXP __declspec(dllimport)
#end

//the function..
int UGLY_IMPEXP TestFunc(void);

/* end libtest.h */

Then I wrote an import unit:

{* begin import unit *}

unit libtest;
interface
function TestFunc: integer; external 'libtest.so';
implementation
end.

{* end import unit *}

Then a little test proggy:

{* begin test proggy *}

program testlib; {original name I know ;) }
uses
  libtest;
begin
  if TestFunc = 20 then writeln('It works')
  else writeln('who knows?!?!');
end.

{* end test proggy *}

Compile with the folowing script:

(UNIT_PATH = path to the compiled units, eg './units/beos')

ppc386 -Cn -FuUNIT_PATH testlib.pas

gcc UNIT_PATH/sysbeos.o UNIT_PATH/prt0.o libtest.so testlib.o -otestlib

//

Does anybody have any ideas on how to take GCC out of the loop?? I'm guessing that there must be a way... either that or I'll symlink GCC into a nicer looking form ;)

I'm going to look into writing a *very* simple flattened API a-la Kylix/QT for BeOS in the next few weeks. (always loved BeOS ;).. I'm looking into finding a simple shared library that implements a well thought out and simple to use 'revised' BeOS GUI Wrapper. If anyone knows of one.. please let me know. I just want something that will avoid all the additional gumpf that BeOS needs to be done behind the scenes (simple forms, buttons, text/edit boxes, checks, combos etc.. nothing too fancy.) Probably end up learning the BeOS API though!! I'll wait for FPC to link against C++ Shared Libraries before I do anything spectacular ;)

Please feedback..

Matt

Reply by email

Gabor DEAK JAHN
to Unknown author: Thanks for responding. I took a look at what you had sent, but it appears as though you…

At 6/15/01 08:03 AM, you wrote:

Jim,

I need to read the file into memory and then access the data via some type of index. In an array I can use ArrayName [Index] to point to any

element of

the array but I can't seem to duplicate that functionality for a 'dynamic' structure.

Although TCollection is implemented as an object, there is nothing very OOP in the way you need to use it. You can think about it as an 'array of pointer' with an indeterminate number of elements, plus a bunch of subroutines handling it. Unlike with normal arrays, you cannot directly reference to its elements but you have methods (functions and subroutines) that provide all this functionality and more.

Let's suppose you have

var
   Coll : TCollection
   CollArray : array [1..MaxCollectionSize] of pointer;

Unlike a simple array, you have to initialize the collection:

   new (Coll);
   Coll.Init (Limit, Delta);

To put something into the collection at a given location, use

Coll.AtPut (Index, ItemPointer),

this would be equivalent to

   CollArray [Index] := ItemPointer

To read from a given location, use

   ItemPointer := Coll.At (Index),

which would be

   ItemPointer := CollArray [Index]

And, unlike with the array, you have to dispose of the collection at the end with

   Coll.Done;
   dispose (Coll);

Actually, the collection gives you more than that. There is Coll.Insert which keeps track of the items put into the collection and places the new item directly at the end; thus, if you want to place item after item into the collection, you don't have to keep track of the indexes, just call Insert repeatedly. Or there is AtInsert which inserts your new item at the position you specify, not overwriting what is already there but making room for it, shifting every item behind that by one position. Or if you have a pointer to an item, you can learn which index it has by calling Coll.IndexOf.

For a description of all methods, visit Units.pdf. You'll also find examples there.

Regards,

   Gabor DEAK JAHN

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
WWW: www.tramontana.co.hu
E-mail:

Reply by email

Florian Klaempfl
to Full_Name: I just got FPC to link against a C shared object that I created natively on BeOS. This is…
you wrote
Does anybody have any ideas on how to take GCC out of the loop?? I'm guessing that there must be a way... either that or I'll symlink GCC into a nicer looking form ;)

Use place
{$linklib 'testlib'}
in your pascal source.

I'll wait for FPC to link against C++ Shared Libraries before I do anything spectacular ;)

Hmmm, I don't know if we'll get this work :( Linking to C++ libs is quite complicated
and would require a lot of compiler changes.

Reply by email

Full_Name
to Florian Klaempfl

Quoting Florian Klaempfl :

Use place {$linklib 'testlib'} in your pascal source.

At the moment I still have to use gcc because I don't know how to link the console to the finished executable any other way :(

Mind you, I havent tried {$i whatever.o} yet, so that might work ;)

Hmmm, I don't know if we'll get this work :( Linking to C++ libs is quite complicated and would require a lot of compiler changes.

Looks like what I'm doing will be worthwhile then ;)

I need more information about how shared objects work. Does anybody know a good resource for Linux? (as BeOS is thin on the ground.) I can get C exports, but not using classes within the '.so'. I know it's possible - I'm going to take a look at the way allegro does it, but does anyone have any pointers.

(btw this is turning out to be an excellent way to brush up on my C/C++)

Matt

Reply by email

Unknown author

Gabor,

Thanks for responding. You gave a very lucid and helpful answer.

Show quoted text 7 lines
Although TCollection is implemented as an object, there is nothing very OOP in the way you need to use it. You can think about it as an 'array of pointer' with an indeterminate number of elements, plus a bunch of subroutines handling it. Unlike with normal arrays, you cannot directly reference to its elements but you have methods (functions and subroutines) that provide all this functionality and more.

I realize that a TCollection is not OOP, in the strictest sense, but it sounded good for a object beginner like me! :-)

Let's suppose you have var Coll : TCollection CollArray : array [1..MaxCollectionSize] of pointer;

This seems to imply a physical limit (MaxCollectionSize) has been defined. Is that correct? In rtl\inc\objects.pp I found the following:

  MaxBytes = 128*1024*1024;
  MaxCollectionSize = MaxBytes DIV SizeOf(Pointer);

if a Pointer is 4 bytes (just guessing) then the upper limit of a TCollection using MaxCollectionSize would be about 33,554,432?

Unlike a simple array, you have to initialize the collection: new (Coll); Coll.Init (Limit, Delta);

This makes sense. But what is the Limit and Delta for?

To put something into the collection at a given location, use Coll.AtPut (Index, ItemPointer)

So this would be the pointer to the "record" itself? For example, suppose I've defined the following:

const
  MAX_LINES = ???;      // if there's no "limit" what should this be?
type
  DataRecord  = packed object
                private
                  Data    : string;
                  Display : boolean;
                  Actual  : longint;
                  Color   : byte;
                end;
var
  Index        : longint;
  DataFromFile : array [0..MAX_LINES] of ^DataRecord;

With an array I can assign values like this:

DataFromFile [Index]^.Data    := 'Some data';
DataFromFile [Index]^.Display := TRUE;
DataFromFile [Index]^.Actual  := Index;
DataFromFile [Index]^.Color   := 23;

But using 'Coll.AtPut (Index, ItemPointer)' how can I assign values to the different "elements" in the DataRecord structure? Would it be something like:

Coll.AtPut (Index,ItemPointer.Data);
Coll.AtPut (Index,ItemPointer.Display);
Coll.AtPut (Index,ItemPointer.Actual);
Coll.AtPut (Index,ItemPointer.Color);

BTW; I can easily redo that DataRecord structure if needed. While the 4 pieces of information are essential I'm certainly not adverse to trying something different their.

To read from a given location, use ItemPointer := Coll.At (Index),

I guess if I can comprehend how to store values I can just reverse the logic to read them back?

Show quoted text 5 lines
And, unlike with the array, you have to dispose of the collection at the end with Coll.Done; dispose (Coll);

Yes, this I will do.

Show quoted text 10 lines
Actually, the collection gives you more than that. There is Coll.Insert which keeps track of the items put into the collection and places the new item directly at the end; thus, if you want to place item after item into the collection, you don't have to keep track of the indexes, just call Insert repeatedly. Or there is AtInsert which inserts your new item at the position you specify, not overwriting what is already there but making room for it, shifting every item behind that by one position. Or if you have a pointer to an item, you can learn which index it has by calling Coll.IndexOf.

This all sounds like what I would like to do, so I guess a TCollection is indeed what I need.

For a description of all methods, visit Units.pdf. You'll also find examples there.

I've done that a few times now, but unfortunately I was having a bit of a rough time figuring it all out. I'm going to read it again though, if for no other reason then to see how many more times it takes before I fully understand it. :-)

Thanks again.

Jim

Reply by email

Matt Emson
to Full_Name: Quoting Florian Klaempfl : At the moment I still have to use gcc because I don't know how…
I need more information about how shared objects work. Does anybody know a

good

resource for Linux? (as BeOS is thin on the ground.) I can get C exports,

but

not using classes within the '.so'. I know it's possible - I'm going to

take a

look at the way allegro does it, but does anyone have any pointers.

Furhter to the above - I've got some feedback from a BeOS dev list, and at least one positive response including an offer of some help.

My plan is to get a very simple prototype working, and to then put it opensource, LGPL'd, probably on sourceforge.

I remember somebody mentioning the BeOS port being integrated into the FPC chain - will there be a binary distro at some point on the FPC page? If you want help getting a software valet package (the standard BeOS package/distro format) I can help out. I've BeOS on both my desktop and work laptop, any time/place etc ;)

Matt

Reply by email

Michael Van Canneyt
to Matt Emson
Matt Emson wrote 19 lines
> I need more information about how shared objects work. Does anybody know a good > resource for Linux? (as BeOS is thin on the ground.) I can get C exports, but > not using classes within the '.so'. I know it's possible - I'm going to take a > look at the way allegro does it, but does anyone have any pointers. Furhter to the above - I've got some feedback from a BeOS dev list, and at least one positive response including an offer of some help. My plan is to get a very simple prototype working, and to then put it opensource, LGPL'd, probably on sourceforge. I remember somebody mentioning the BeOS port being integrated into the FPC chain - will there be a binary distro at some point on the FPC page? If you want help getting a software valet package (the standard BeOS package/distro format) I can help out. I've BeOS on both my desktop and work laptop, any time/place etc ;)

The core team is integrating the BeOS port in the compiler, but this takes some time. The main problem is probably the lack of computers with BeOS on it.

Michael.

Reply by email

Peter Vreman
Show quoted text 9 lines
> > I remember somebody mentioning the BeOS port being integrated into the FPC > chain - will there be a binary distro at some point on the FPC page? If you > want help getting a software valet package (the standard BeOS package/distro > format) I can help out. I've BeOS on both my desktop and work laptop, any > time/place etc ;) The core team is integrating the BeOS port in the compiler, but this takes some time. The main problem is probably the lack of computers with BeOS on it.

The BeOS port is already integrated into the compiler/rtl and Makefiles. But it's untested.

Reply by email

Full_Name
to Michael Van Canneyt: The core team is integrating the BeOS port in the compiler, but this takes some time. The…

Quoting Michael Van Canneyt :

Show quoted text 5 lines
The core team is integrating the BeOS port in the compiler, but this takes some time. The main problem is probably the lack of computers with BeOS on it.

If anyone wants help with obtaining the free version of BeOS, I can burn CD's of the windows installer. I can include the Dev tools on the CD as well. They are free too, but ~8mb download.

I can also give details of how to install the Personal version of BeOS into a bootable HDD partition.

I doubt I'll have time to do any integration though, I'll continue using the initial port for now.

Matt

Reply by email

Ramon Bosque
to Peter Vreman: The BeOS port is already integrated into the compiler/rtl and Makefiles. But it's…

Hello,

What is the procedure in order to test it? Is there any binary package? Or must I compile the source?
Is this topic better suited for the fpc-devel list?

Best regards

Ramon Bosque

Earlier message 33 lines
----- Original Message ----- From: "Peter Vreman" To: Sent: Wednesday, June 20, 2001 9:59 AM Subject: Re: [fpc-pascal]More BeOS fun.. > > > > > > I remember somebody mentioning the BeOS port being integrated into the FPC > > > chain - will there be a binary distro at some point on the FPC page? If you > > > want help getting a software valet package (the standard BeOS package/distro > > > format) I can help out. I've BeOS on both my desktop and work laptop, any > > > time/place etc ;) > > > > The core team is integrating the BeOS port in the compiler, but this takes some > > time. The main problem is probably the lack of computers with BeOS on it. > > The BeOS port is already integrated into the compiler/rtl and Makefiles. But it's > untested. > > > > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Full_Name
to Peter Vreman: The BeOS port is already integrated into the compiler/rtl and Makefiles. But it's…

Quoting Peter Vreman :

The BeOS port is already integrated into the compiler/rtl and Makefiles. But it's untested.

That's what I thought. The offer for BeOS personal stands to anybody interested.

If you want me to have a go at building a binary and and installer, I'll take a look at it when I get a chance. I need to get the latest version anyway for the development work I have planned.

Matt

Reply by email

Full_Name
to Ramon Bosque: What is the procedure in order to test it? Is there any binary package? Or must I compile…

Quoting Ramon Bosque :

Hello, What is the procedure in order to test it?

Download the port. Install. Compile test programs.

Is there any binary package?

There is a peliminary binary distro, yes.

Or must I compile the source?

You can do this too.

Is this topic better suited for the fpc-devel list?

I'm not an FPC developer; furthermore I'm only really interested in getting stuff I compile with the BeOS port working, not the BeOS port itself. From what I've seen the basic BeOS port works fine as is. It's just lacking in library support.

The email had to go somewhere ;) Besides, other ports etc get mentioned on this list, so why not BeOS?

Matt

Reply by email

Unknown author

Okay, I've read the section in units.pdf about objects -- again -- and tried some of the things that Gabor had suggested. The attached source file is the culmination of all that, but it still doesn't work.  :-(

The code that gives me problems has been commented out, along with the compiler errors that were generated. Based upon the errors I guess I'm a bit more confused then I thought.

Any help would be appreciated. Thanks again...

Jim

Reply by email

Ramon Bosque
to Full_Name: Quoting Ramon Bosque : Download the port. Install. Compile test programs. There is a…

Hello,

I have searched in the freeascal ftp server but I haven't found the BeOS version, although it seems that it is being included in the 1.1 branch. I have downloaded the zip file from pixel32.box.sk . Have you used that version? It is a tar file. Is there an installation program?

Best regards

Ramon Bosque

Earlier message 42 lines
----- Original Message ----- From: "Full_Name" To: Sent: Wednesday, June 20, 2001 1:13 PM Subject: Re: [fpc-pascal]More BeOS fun.. > Quoting Ramon Bosque : > > > Hello, > > > > What is the procedure in order to test it? > > Download the port. Install. Compile test programs. > > > Is there any binary package? > > There is a peliminary binary distro, yes. > > > Or must I compile the source? > > You can do this too. > > > Is this topic better suited for the fpc-devel list? > > I'm not an FPC developer; furthermore I'm only really interested in getting > stuff I compile with the BeOS port working, not the BeOS port itself. From what > I've seen the basic BeOS port works fine as is. It's just lacking in library > support. > > The email had to go somewhere ;) Besides, other ports etc get mentioned on this > list, so why not BeOS? > > Matt > > _______________________________________________ > fpc-pascal maillist - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Full_Name
to Ramon Bosque

Quoting Ramon Bosque :

Show quoted text 8 lines
Hello, I have searched in the freeascal ftp server but I haven't found the BeOS version, although it seems that it is being included in the 1.1 branch. I have downloaded the zip file from pixel32.box.sk . Have you used that version? It is a tar file. Is there an installation program?

If you unpack the tar file with Expand-o-matic you'll end up with a folder called '1.00'. All I then did was make a symlink (rightclick on file, and select 'Create Link') in the '1.00' folder of all the files in '1.00/units/beos' and used the following to compile:

ppc386 -Cn -Fu./units/beos test.pas
gcc ./sysbeos.o ./prt0.o ./test.o -o test

where 'test' is your unit name...

If you use multiple units, they must be listed in order :(

The guy from pixel32 promised info, but it never came.

Matt

Reply by email

Gabor DEAK JAHN
to Unknown author: Thanks for responding. You gave a very lucid and helpful answer. I realize that a…

At 6/19/01 11:35 AM, you wrote:

James,

I realize that a TCollection is not OOP, in the strictest sense, but it sounded good for a object beginner like me! :-)

It is OOP, it is implemented as an object after all, just that it does not present many OOP challenges.

if a Pointer is 4 bytes (just guessing) then the upper limit of a TCollection using MaxCollectionSize would be about 33,554,432?

I guess so, I never calculated but you can always learn the actual value with a simple writeln (MaxCollectionSize).

This makes sense. But what is the Limit and Delta for?

Limit is the initial size and Delta is the increment it will be enlarged by if you go beyond Limit. They serve reasons of efficiency. If you know that you'll have around 5,000 items why waste memory for much more? Similarly, if you do go above the initial Limit, it is much more efficient to ask for a larger additional chunk (in this example, for instance, Delta could be 1,000), then Limit will practically become 6,000 and you have 1,000 more items to go before the Collection needs housekeeping again.

Coll.AtPut (Index,ItemPointer.Data);

Not exactly, you add all the information you want to your record first, then put only its address into the collection,

Think about it this way: let's suppose Pascal is modified and does not allow constructing an array of records, only of simple types like integers or pointers. What you could do then would be to create an array of pointers, not the record themselves, which point to somewhere in memory where the actual contents of the record is stored.

type
   PDataRecord = ^TDataRecord
   TDataRecord = record ...
   TArray = array [1..MaxSize] of PDataRecord;
var
   DataRecord : PDataRecord;
   DataArray : TDataArray;

You would do the same then, for each element of the array, you have to ask for a record in memory:

   new (DataRecord);

fill it with the required contents:

   DataRecord^.Data := 'Some data';
   DataRecord^.Display := TRUE;
   DataRecord^.Actual  := Index;
   DataRecord^.Color := 23;

then place its address into the array:

   DataArray [Index] := DataRecord;

With the Collection, it is the same, only the last line is different: you simply use a different syntax to put something into a collection than into an array. But its meaning is more or less the same:

   Coll.AtPut (Index, DataRecord);

At then end, you have to dispose of your records as well, not only the Collection.

I guess if I can comprehend how to store values I can just reverse the

logic to read them back?

You get the pointer back, then use it to access your data:

DataRecord := Coll.At (Index),
writeln (DataRecord^.Data);
if DataRecord^.Display then ...

Bye,
Gábor

------------------------------------------------------------------- Gabor DEAK JAHN -- Budapest, Hungary.
E-mail:

Reply by email

Ramon Bosque
to Full_Name: Quoting Ramon Bosque : If you unpack the tar file with Expand-o-matic you'll end up with…

Thank you. I will try it.

Ramon

Earlier message 33 lines
----- Original Message ----- From: "Full_Name" To: Sent: Wednesday, June 20, 2001 5:32 PM Subject: Re: [fpc-pascal]More BeOS fun.. > Quoting Ramon Bosque : > > > Hello, > > > > I have searched in the freeascal ftp server but I haven't found the > > BeOS > > version, although it seems that it is being included in the 1.1 branch. > > I > > have downloaded the zip file from pixel32.box.sk . Have you used that > > version? It is a tar file. Is there an installation program? > > If you unpack the tar file with Expand-o-matic you'll end up with a folder > called '1.00'. All I then did was make a symlink (rightclick on file, and > select 'Create Link') in the '1.00' folder of all the files > in '1.00/units/beos' and used the following to compile: > > ppc386 -Cn -Fu./units/beos test.pas > gcc ./sysbeos.o ./prt0.o ./test.o -o test > > where 'test' is your unit name... > > If you use multiple units, they must be listed in order :( > > The guy from pixel32 promised info, but it never came. > > Matt

Reply by email