Recent

Author Topic: Can this C struct be expressed in FPC Pascal ?  (Read 19279 times)

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #45 on: July 07, 2019, 11:18:08 am »
Sorry of being off topic.
Or maybe I should start a new thread.

I am an autodidact and programming is not my real job, so my knowledge is very limited.

Long years ago, in the era of 80 x 25 text mode I was thinking to build my own TUI. I knew Turbo Vision but I never learned how to use it. I like do something challenging, I decided to build my own version of turbo vision. I've just finished to learn single/double linked list, pointer, direct vga memory mapping, etc but I haven't known OOP. No matter how hard I tried, I just can't figure out how to build my TUI module.

Not until I found OOP. I got a book, which teaches you how to build your own simple Turbo Vision using C++. I can't use C/++ but from the book I learned the concept of OOP. Not long later I really managed to build my own TUI. It maybe not good as the others, but it works and looking good in my eyes. As a creative person, I added some animation effects and audio.

In my school age, I had plenty of time. But now, I have very limited time for programming. Currently I'm building my own OpenGL-based GUI using OOP concept.

I like OOP because it really helps me to solve some complicated cases. But I don't abuse it, I won't use it if it is not really needed. I believe everything good can become bad if you misuse it.

I often hear some said OOP is bad. But their explanations are out of my ability to understand. My glGUI progress is slow because I have limited time for working on it. I am neutral, I'm not a OOP fanatic but I can't write GUI without using OOP.

What I want to ask is:
Can anyone write me a simple code of GUI or TUI module without using OOP?

So I can learn from the code and rewrite my glGUI without using OOP. By experience it myself I can understand how good or bad OOP is.

I'm sure writing GUI module without using OOP is possible but I believe it should be very hard. Please prove me wrong.
« Last Edit: July 07, 2019, 11:23:17 am by Handoko »

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #46 on: July 07, 2019, 11:28:43 am »
It is possible. One guy had even a near complete professional GUI CAD program in the Mac dialect.

His parameter lists were awfully long though, and he used ISO style procedure callbacks (where procedures can pass a nested procedure to a callbacks which then can use the outer procedure/variables). The callbacks are effectively virtual methods.

Before I used language OO, I already used  OO in a way too in procedural programming. Grouping things into records, functions that always take such record as first parameter.  Records with callbacks in them.  etc.

« Last Edit: July 07, 2019, 03:15:15 pm by marcov »

julkas

  • Guest
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #47 on: July 07, 2019, 11:34:09 am »
@Handoko Clear and argumented post. Agree. +5.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #48 on: July 07, 2019, 11:35:11 am »
What I want to ask is:
Can anyone write me a simple code of GUI or TUI module without using OOP?

So I can learn from the code and rewrite my glGUI without using OOP. By experience it myself I can understand how good or bad OOP is.

I'm sure writing GUI module without using OOP is possible but I believe it should be very hard. Please prove me wrong.

There wer lots of TUI frameworks and components way before Borland "invented" TurboVision. TurboPower had their own, and there were TechnoJock's Toolkit, and quite a lot more, even as far back as Turbo Pascal 3. And that's not counting the "screen generators", or the frameworks for other Pascals, like the ones for Pascal MT+, JRT, etc.

And the same for GUIs. Once TP for Windows appeared everyone started adapting their TUIs and building encapsulations for the Windows API. Although that was on the era of TP6, when objects where already quite stablished and Turbo Vision was ... almost king, so a lot of effort went towards building "Windows Vision"s :)

And then came Delphi and it ruined it all :D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #49 on: July 07, 2019, 11:38:57 am »
If you want to see how to program a GUI without OOP, look at Win16. Well, behind the screens that still works today, but you really don't want to do that.

My previous job was bugfixing a 25 years old C++ Windows app, and I added stuff like unicode controls, drag-and-drop, etc. So I had to do that the old way. It is complex, error prone and a lot of work.

Edit: Of course I encapsulated that into some nice classes to use it.
« Last Edit: July 07, 2019, 11:45:24 am by SymbolicFrank »

Peter H

  • Sr. Member
  • ****
  • Posts: 272
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #50 on: July 07, 2019, 11:59:09 am »

I often hear some said OOP is bad. But their explanations are out of my ability to understand. My glGUI progress is slow because I have limited time for working on it. I am neutral, I'm not a OOP fanatic but I can't write GUI without using OOP.

What I want to ask is:
Can anyone write me a simple code of GUI or TUI module without using OOP?

So I can learn from the code and rewrite my glGUI without using OOP. By experience it myself I can understand how good or bad OOP is.

I'm sure writing GUI module without using OOP is possible but I believe it should be very hard. Please prove me wrong.

I believe, a GUI or a GUI item is an object, like most external devices, might it be a CNC machine, a printer or a moon rocket...
It has an internal state (member variables) and these must store and reflect the state of the external object and for this handler procedures are used.
Usually these routines are not reentrant, because the external object is not reentrant.
So if you do it with a non-objectoriented language, then you must mimic object orientation.
If typeextending and inheritance is needed, then you must use typecasting.....

BTW, I am autodidact too and I did embedded programming in C and assembler wrote software to control and visualize a high pressure autoclav, in Turbo Pascal 30 years ago. There was no internet and I learned out of books and journals. I got no formal education but formal selfstudy and got my education on the job.
But I had formal education on electronics.
And sometimes it is important to know the problem that has to be solved. All formal education doesnt help if you have to solve a problem that you dont understand, this is why most drivers for UARTs are buggy.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #51 on: July 07, 2019, 12:33:19 pm »
It would be nice if Fpc had a C:Array[] of Dword; so that it can generate a location without an instance.
No. This feature would only have a rather restricted use (namely as the last field of records) and as others demonstrated there are solutions that make use of existing language features to provide the same result. They might involve a bit more typing, but they work now.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #52 on: July 07, 2019, 03:17:04 pm »
It would be nice if Fpc had a C:Array[] of Dword; so that it can generate a location without an instance.
No. This feature would only have a rather restricted use (namely as the last field of records) and as others demonstrated there are solutions that make use of existing language features to provide the same result. They might involve a bit more typing, but they work now.

And that style was popular 20-30 years ago, and is enshrined in the apis from that era. But I'm not sure that much new code in this style is written. (as in trying to access variable number items via the header record in front of them)

Probably it invites not writing safe code.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #53 on: July 07, 2019, 04:31:06 pm »
So I guess the juror have tallied their votes.

 They would rather use twisted hacks that makes it readable only by those that know the code inside/out, if they
can successfully get it to work in a particular instance thus making it hard to read by those that would like the see a nice straight forward approach.

 The lot of you may not like the C approach but this I can tell you, its a lot cleaner and simpler to understand
over what hacks, if workable, that are offered here.

 There are some things in C that are clearly laid out no matter how old they are. Other languages do not
need to copy the syntax but they should be able to offer a straight solution to it.

 So I've seen here so far, over lapping Records which requires more work to implement and less readable.

 I've seen some undocumented use of an Empty Record which is fine but it still needs a type cast over it
to take advantage of it and maybe helper functions. Still way too much code and very unreadable afterwards.


 And then I offered the -1..1- offset array in the variant record which does work in most cases but I am
reading articles on the bug site that indicate not being able to index them out of range even with
range checking off in coming releases of fpc.

 So what's it going to be, a campaign to ensure fpc makes everyone's life a wreck when implementing
techniques that are only possible in other languages ?

 You can improve fpc without loosing the pascal language interface.. Personally I hate C and Especially
C++ in many cases, but there so cavets to the language..

The only true wisdom is knowing you know nothing

SymbolicFrank

  • Hero Member
  • *****
  • Posts: 1313
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #54 on: July 07, 2019, 04:58:14 pm »
You can always do it yourself.

jamie

  • Hero Member
  • *****
  • Posts: 6091
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #55 on: July 07, 2019, 05:10:21 pm »
I don't want a personized version of fpc for myself.

I want one that offers equal solutions that everyone can use..

I don't consider myself as a selfish person so why would I have a pascal compiler onboard that
only "I" can use, I would like the community to be able to share in the fruits of advancement without
loosing their love for the Pascal verbosity syntax.

 I am sure you can understand that but if one would just sit down and take a good long look at the
language differences, the good and the bad, with a few and only a very few additions to Fpc, it would motivate
a lot of other language users over to fpc advanced version of pascal.

 I work in C/C++ at work and like I said there some things that are greate about it, but over all when it
comes to working with Classes/Objects, I'll pick Pascal/Fpc any day and there aren't many C/C++ out there
that have RAD interfaces. I have Builder on board but its kind of dated but use it to maintain some apps only
because I was asked to do it that way instead of Delphi years back.

 The guide lines of tighten the rope around coders necks for type-ness ect can endup biting you in the long run.



The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11383
  • FPC developer.
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #56 on: July 07, 2019, 05:14:20 pm »
I've seen some undocumented use of an Empty Record which is fine but it still needs a type cast over it
to take advantage of it and maybe helper functions. Still way too much code and very unreadable afterwards.

That was me, and it was answer to a direct question how to create an empty symbol at the end. I don't use it in production code, it is something from unittests checking record sizes etc.

Personally I think that the savings are very low, or even not existing. Specially since there probably is already some pointer to where you are in the buffer that can be incremented. (this kind of structures is mostly used to decode buffers, like a fileheader and some variable entries, used in e.g. CHM).  9 times out of ten you need to do anything anyway, to proper check your buffer boundaries, so the savings are only in very simplistic cases.

p.s. the best thing about C: if somebody whines for an extension they probably get the answer: sure, write a paper to present for the future standard C '24 or so.
« Last Edit: July 07, 2019, 05:15:57 pm by marcov »

Thaddy

  • Hero Member
  • *****
  • Posts: 14210
  • Probably until I exterminate Putin.
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #57 on: July 07, 2019, 05:16:52 pm »
Defining sub types of a variant record as variant record themselves is actually more readable than C(++) union syntax, because the type is defined more precise. And can be in any order.,
Specialize a type, not a var.

440bx

  • Hero Member
  • *****
  • Posts: 3946
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #58 on: July 07, 2019, 05:47:48 pm »
The lot of you may not like the C approach but this I can tell you, its a lot cleaner and simpler to understand over what hacks, if workable, that are offered here.

There are some things in C that are clearly laid out no matter how old they are. Other languages do not need to copy the syntax but they should be able to offer a straight solution to it.

So I've seen here so far, over lapping Records which requires more work to implement and less readable.
Those are good points and I share your viewpoint (which, just like yours, isn't worth anything) but.... the reality is and will very likely continue to be :
No. This feature would only have a rather restricted use (namely as the last field of records) and as others demonstrated there are solutions that make use of existing language features to provide the same result. They might involve a bit more typing, but they work now.
Making something simpler, cleaner, easier to read and understand is not what drives the inclusion of a feature in FPC.   It gives the impression that the people who are responsible for updating the C standard are just producing useless things no one needs. 

Bottom line is: be content there is a way of doing it.  That's in most cases all you're going to get along with a "bonus" like "+=" or ">>" and some other thing like that, you have absolutely no need for.  To keep you away from posting obvious improvements like you did, they'll change the semantics of some keyword (like "var" for instance") so it generates piles of useless hints you have to wade through.  That will keep you busy doing something "useful".


(FPC v3.0.4 and Lazarus 1.8.2) or (FPC v3.2.2 and Lazarus v3.2) on Windows 7 SP1 64bit.

Peter H

  • Sr. Member
  • ****
  • Posts: 272
Re: Can this C struct be expressed in FPC Pascal ?
« Reply #59 on: July 07, 2019, 06:15:27 pm »
My two pence.
I believe this is pretty good selfdocumenting.
Of course it would be nicer, if this could be written inline as property. (I love properties ::) )

Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4. {$modeswitch advancedrecords}
  5.  
  6. type
  7.   mystruct = packed record
  8.   a,b: DWORD;
  9.   function c: PDWORD; inline; // return adress after record
  10. end;
  11.  
  12. function mystruct.c: PDWORD;
  13. begin
  14.   {$T+} // @ now gives typed pointers
  15.   c := PDWORD(@self+1);
  16. end;
  17.  
« Last Edit: July 07, 2019, 06:28:37 pm by Peter H »

 

TinyPortal © 2005-2018