Recent

Author Topic: Storing object properties  (Read 10827 times)

KemBill

  • Jr. Member
  • **
  • Posts: 74
Storing object properties
« on: December 05, 2017, 12:34:00 pm »
I'm looking for a shorter way to store user preferences, in this exemple I must define a setter to each property in order to save it's value

Code: Pascal  [Select][+][-]
  1.   { TSomething }
  2.  
  3.   TSomething = class(TObject)
  4.   private
  5.     fSomething: integer;
  6.     procedure setSomething(AValue: integer);
  7.   public
  8.     property Something: integer read fSomething write setSomething;
  9.   end;
  10.  
  11. implementation
  12.  
  13. { TSomething }
  14.  
  15. procedure TSomething.setSomething(AValue: integer);
  16. begin
  17.   if fSomething=AValue then Exit;
  18.   fSomething:=AValue;
  19.   //Store something into registry or ini file
  20. end;
  21.  

I Hope that I am understandable (for example in PHP I would have used magic methods __set). Do someone know a better way ?

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Storing object properties
« Reply #1 on: December 05, 2017, 12:45:32 pm »
Provided you have generated RTTI (Published property, TComponent, {$M+}, published,not public, the easiest is to use writecomponentresfile and readcomponentresfile and family (TStream).
There are many examples for this, both for Delphi and Freepascal/Lazarus.

Both functions take just a filename and a TComponent as parameters. Those will also read/write the published properties of any children.
« Last Edit: December 05, 2017, 12:53:11 pm by Thaddy »
objects are fine constructs. You can even initialize them with constructors.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Storing object properties
« Reply #2 on: December 05, 2017, 04:01:37 pm »
Code: Pascal  [Select][+][-]
  1.  { TSomething }
  2.  
  3.   TSomething = class(TObject)
  4.   private
  5.     fSomething: integer;
  6.     procedure setSomething(AValue: integer);
  7.   public
  8.     procedure savePreference(const aRef : string; const aValue : integer);
  9.  
  10.     property Something: integer read fSomething;
  11.   end;
  12.  
  13. implementation
  14.  
  15. { TSomething }
  16.  
  17. procedure TSomething.savePreference(const aRef : string; const aValue : integer);
  18. begin
  19.   //write your value to registry / ini
  20.   case aRef of
  21.     'something' : fSomething := aValue;
  22.   end;
  23. end;
  24.  
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Storing object properties
« Reply #3 on: December 05, 2017, 04:16:57 pm »
@mangakissa,
That won't do. My pointer is a generic solution and way simpler to maintain.
It was also designed for the purpose.
objects are fine constructs. You can even initialize them with constructors.

KemBill

  • Jr. Member
  • **
  • Posts: 74
Re: Storing object properties
« Reply #4 on: December 05, 2017, 05:09:22 pm »
@Thaddy, I forgot to say that i need to store values in a human readable format  O:-)

@mangakissa
you make property read only and the only way to update it is to use savePreference. the main problem behind this, is that you need to write a proc for every type

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Storing object properties
« Reply #5 on: December 05, 2017, 05:12:20 pm »
@Thaddy, I forgot to say that i need to store values in a human readable format  O:-)
It is human readable: it is XML like, not a binary format, don't worry.... Those were the olden days...
objects are fine constructs. You can even initialize them with constructors.

Edson

  • Hero Member
  • *****
  • Posts: 1329
Re: Storing object properties
« Reply #6 on: December 05, 2017, 08:27:28 pm »
I use my library: https://github.com/t-edson/MiConfig

Just associate variables to an IniFile:

Code: Pascal  [Select][+][-]
  1. iniFile.Asoc_Str('MyText', @MyText, 'default');
  2. iniFile.Asoc_Int('MyNumber', @MyNumber, -1);
  3. ...
  4.  

Then you can move values to file, and vice versa:

Code: Pascal  [Select][+][-]
  1. iniFile.FileToProperties;
  2. iniFile.PropertiesToFile;
  3. ...
  4.  
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Storing object properties
« Reply #7 on: December 05, 2017, 09:36:48 pm »
@Edson

WHY? There is a standard for this as I pointed out. AND it is just XML?
objects are fine constructs. You can even initialize them with constructors.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Storing object properties
« Reply #8 on: December 05, 2017, 10:07:11 pm »
@Edson

WHY? There is a standard for this as I pointed out. AND it is just XML?
nope its plain old text files, oh wait its plain old ini files oh wait its plain old JSON files oh wait its a key value store oh wait that windows registry well depends on the OS you are using I guess just don't waste the end users disk space with garbage (XML).
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Thaddy

  • Hero Member
  • *****
  • Posts: 19273
  • Glad to be alive.
Re: Storing object properties
« Reply #9 on: December 05, 2017, 10:17:59 pm »
@Edson

WHY? There is a standard for this as I pointed out. AND it is just XML?
nope its plain old text files, oh wait its plain old ini files oh wait its plain old JSON files oh wait its a key value store oh wait that windows registry well depends on the OS you are using I guess just don't waste the end users disk space with garbage (XML).
I am not discussing the format, but it it is human readable and editable in any simple editor. Even edlin..... see https://en.wikipedia.org/wiki/Edlin
Perfect forms editor... better than "copy con.... ctrr-z" don't you think? :D
objects are fine constructs. You can even initialize them with constructors.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Storing object properties
« Reply #10 on: December 05, 2017, 10:28:46 pm »
@Edson

WHY? There is a standard for this as I pointed out. AND it is just XML?
nope its plain old text files, oh wait its plain old ini files oh wait its plain old JSON files oh wait its a key value store oh wait that windows registry well depends on the OS you are using I guess just don't waste the end users disk space with garbage (XML).
I am not discussing the format, but it it is human readable and editable in any simple editor. Even edlin..... see https://en.wikipedia.org/wiki/Edlin
Perfect forms editor... better than "copy con.... ctrr-z" don't you think? :D
well I never liked edlin so I only used it once, I used "copy con " extensively for admin scripts even after I discovered the norton/midnight commander and its build in editor.  ;) I would stick with ini files for readable config files.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Edson

  • Hero Member
  • *****
  • Posts: 1329
Re: Storing object properties
« Reply #11 on: December 06, 2017, 02:53:58 am »
@Edson
WHY? There is a standard for this as I pointed out. AND it is just XML?

This library not only associate:

Quote
Disk <---> Properties

It's intended to achieve a real association between:

Quote
Disk <---> Properties <---> Visual Component

So it can help on creating setting dialogs. An association for a property, a control, and disk can be done in one line of code, including range checking, default value.

And the file can be INI or XML.

Is there some standar way to do the same?
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Storing object properties
« Reply #12 on: December 06, 2017, 03:38:07 am »
Is there some standar way to do the same?
It probably won't do as a standard way as there are many roads that leads to Rome but have a look at TXMLPropStorage and/or TIniPropStorage
« Last Edit: December 06, 2017, 03:42:04 am by molly »

Edson

  • Hero Member
  • *****
  • Posts: 1329
Re: Storing object properties
« Reply #13 on: December 06, 2017, 04:29:12 am »
It probably won't do as a standard way as there are many roads that leads to Rome but have a look at TXMLPropStorage and/or TIniPropStorage

They only do:  Disk <---> Properties
Lazarus 2.2.6 - FPC 3.2.2 - x86_64-win64 on Windows 10

molly

  • Hero Member
  • *****
  • Posts: 2330
Re: Storing object properties
« Reply #14 on: December 06, 2017, 04:48:59 am »
They only do:  Disk <---> Properties
And from properties you can go to variables. Although that is a bit redundant. Seems you are not aware of TDataModule and storing your (global) settings in there using properties ? Whether or not you associate those properties to anything else is up to yourself.

edit:
Disk <---> Properties <---> Visual Component
So it can help on creating setting dialogs. An association for a property, a control, and disk can be done in one line of code, including range checking, default value.
XMLPorpstorage by default does:
Disk <---> Properties of any (visual) Component
« Last Edit: December 06, 2017, 05:26:27 am by molly »

 

TinyPortal © 2005-2018