Recent

Author Topic: advanced records, case integer set default?  (Read 5355 times)

trn76

  • New Member
  • *
  • Posts: 40
advanced records, case integer set default?
« on: May 02, 2016, 04:21:19 pm »
Hi people.

In FPC v3.0, I am using advanced record and my structure and code look something like this:

Code: Pascal  [Select][+][-]
  1. TGxColor = packed record
  2.    case Integer of
  3.       0 : (blue, green, red, alpha: byte);
  4.       1 : (Bc, Gc, Rc, Ac: byte);
  5.       2 : (BGRA: UInt32);  // Default;
  6. end;
  7.  
  8. ...
  9.  
  10. var color: TGxColor;
  11.  
  12. // I would love to do this:
  13. color := $121212FF;
  14.  
  15. // but I am forced to do this:
  16. color.BGRA := $121212FF;
  17.  

As  you see in the code, I've tried the Default keyword but still no go...
Anyone know if this is possible? Maybe trivial, but still.  :P


...Thanks 

airpas

  • Full Member
  • ***
  • Posts: 179
Re: advanced records, case integer set default?
« Reply #1 on: May 02, 2016, 04:54:55 pm »
you can implement the assignment operator in this case
some thing like this :

operator := (cl : uint32):tGxColor;
begin
  result.BGRA := cl;
end;

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: advanced records, case integer set default?
« Reply #2 on: May 02, 2016, 06:57:02 pm »
Anyone know if this is possible? Maybe trivial, but still.  :P
I've never known if it's possible, where do you read if such a feature is implemented?

trn76

  • New Member
  • *
  • Posts: 40
Re: advanced records, case integer set default?
« Reply #3 on: May 02, 2016, 07:23:06 pm »
Quote
I've never known if it's possible, where do you read if such a feature is implemented?
I've read the whats new section, wiki and blasted over the manual. I guess thats where you find many things about FPC and Laz. - Did I understand your q?

trn76

  • New Member
  • *
  • Posts: 40
Re: advanced records, case integer set default?
« Reply #4 on: May 02, 2016, 07:25:35 pm »
you can implement the assignment operator in this case
some thing like this :

operator := (cl : uint32):tGxColor;
begin
  result.BGRA := cl;
end;

Thank you, I did not think about that.
Only issue I have with overloading operators are how slow they are, but I will check when I get home and report back.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: advanced records, case integer set default?
« Reply #5 on: May 03, 2016, 09:22:44 am »
Quote
I've never known if it's possible, where do you read if such a feature is implemented?
I've read the whats new section, wiki and blasted over the manual. I guess thats where you find many things about FPC and Laz. - Did I understand your q?
Could you point to a specific page?

Thaddy

  • Hero Member
  • *****
  • Posts: 14383
  • Sensorship about opinions does not belong here.
Re: advanced records, case integer set default?
« Reply #6 on: May 03, 2016, 10:37:35 am »
Only issue I have with overloading operators are how slow they are, but I will check when I get home and report back.
" how slow they are"? is a programmer error. They are as fast as the algorithm you program, silly..
The generated code is not slow unless you are a moron/monkey/donkey that makes it slow on purpose.

First check what's under the hood, then make statements. The option is -a the output is in an *.s file

Why? Because it is evaluated at COMPILE time. >:( >:D
« Last Edit: May 03, 2016, 10:40:57 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

hnb

  • Sr. Member
  • ****
  • Posts: 270
Re: advanced records, case integer set default?
« Reply #7 on: May 03, 2016, 11:32:22 am »
Anyone know if this is possible? Maybe trivial, but still.  :P
I've never known if it's possible, where do you read if such a feature is implemented?

Maybe he saw my posts on mailing list. "default" is part of my work on smart pointers. The work is almost done.

The "default" modifier works fine also for mentioned structure. The correct code is:

Code: Pascal  [Select][+][-]
  1.   TGxColor = packed record
  2.    case Integer of
  3.       0 : (blue, green, red, alpha: byte);
  4.       1 : (Bc, Gc, Rc, Ac: byte);
  5.       2 : (BGRA: UInt32 default);
  6.   end;
« Last Edit: May 03, 2016, 12:22:31 pm by hnb »
Checkout NewPascal initiative and donate beer - ready to use tuned FPC compiler + Lazarus for mORMot project

best regards,
Maciej Izak

 

TinyPortal © 2005-2018