#1 2022-01-30 00:19:55

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Interface Based Services - send TStrings

Regarding the documentation I'm trying to send the TStringList (dictionary).

TTest = class(TInterfacedObject, ITest)
   procedure GetParams(out params: TStrings);
end;

procedure TTest.GetParams(out params: TStrings);
begin
  params := TStringlist.create;
  params.Values['name'] := 'John';
end;

how to free the params stringlist (do I have to free that list?)

Offline

#2 2022-01-30 06:54:29

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Interface Based Services - send TStrings

When called from a service, the out parameter is already allocation.
You don't need to write params := TStringList.Create.
And you don't need to free it: it will be freed by the framework.

Offline

#3 2022-01-30 11:29:07

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: Interface Based Services - send TStrings

OK, how to deal with stringlist as a part of record?

TVehicle = record
  Make: string;
  Model: string;
  TechnicalDetails: TStrings;
end;

TTest = class(TInterfacedObject, ITest)
   procedure GetVehicle(out vehicle: TVehicle);
end;

procedure TTest.GetVehicle(out vehicle: TVehicle);
begin
   vehicle.Make := 'volvo';
   vehicle.TechnicalDetails := TStringList.Create -> ????
   vehicle.TechnicalDetails['Engine'] := 'Diesel';
end;

The framework probably won't create/free that stringlist for me?

Last edited by radexpol (2022-01-30 11:29:34)

Offline

#4 2022-01-30 13:01:17

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: Interface Based Services - send TStrings

Let me try to answer myself.

TVehicle = record
  Make: string;
  Model: string;
  TechnicalDetails: IKeyValue<string, string>;
end;

TTest = class(TInterfacedObject, ITest)
   procedure GetVehicle(out vehicle: TVehicle);
end;

procedure TTest.GetVehicle(out vehicle: TVehicle);
begin
   vehicle.Make := 'volvo';
   vehicle.TechnicalDetails := Collections.NewKeyValue<string, string>;
   vehicle.TechnicalDetails.TryAdd('Engine', 'Diesel');
end;

can I use safely the code like above ?

Offline

#5 2022-01-30 13:04:18

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: Interface Based Services - send TStrings

radexpol wrote:

The framework probably won't create/free that stringlist for me?

I think the easiest will be:

  1. If you want to use a record, use TStringDynArray instead of TStrings.

  2. Or convert the record TVehicle to a class TVehicle = class(TSynPersistent/TObjectWithID) with published property TechnicalDetails: TStrings. Or have a look at the classes ...AutoCreateFields.

With best regards
Thomas

Offline

#6 2022-01-30 14:05:09

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Interface Based Services - send TStrings

TStringList is really a dinosaur, not to be used in normal code.

Thomas is right:
Use a TRawUtf8DynArray in the record.

Offline

#7 2022-01-30 17:35:13

radexpol
Member
From: Poland, Krk
Registered: 2019-11-29
Posts: 116

Re: Interface Based Services - send TStrings

How to store key+value in:

/// a dynamic array of UTF-8 encoded strings
  TRawUtf8DynArray = array of RawUtf8;

?

Offline

#8 2022-01-30 20:40:09

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: Interface Based Services - send TStrings

radexpol wrote:

How to store key+value in: ?

TSynDictionary has the functions SaveToJson() and LoadFromJson(). In unit mormot.core.collections you will find Collections.NewKeyValue<TKey, TValue>.

var
  list: IKeyValue<Int64, RawUtf8>;
begin
  list := Collections.NewKeyValue<Int64, RawUtf8>;

In the example you also have access to other TSynDictionary methods with list.Data.xxx. In the unit test.core.collections you can find an example of how to use it.

With best regards
Thomas

Offline

#9 2022-01-30 20:48:43

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Interface Based Services - send TStrings

Or just use a TDocVariantData stored into a variant.

It is very easy to use, with automatic memory management.
And the key+value will be serialized as a regular JSON object.

Offline

#10 2022-01-30 20:55:56

tbo
Member
Registered: 2015-04-20
Posts: 353

Re: Interface Based Services - send TStrings

ab wrote:

Or just use a TDocVariantData stored into a variant.

+1  I like DocVariants very much. Whenever you get stuck, a DocVariant comes along from somewhere. smile

Last edited by tbo (2022-01-30 21:11:42)

Offline

Board footer

Powered by FluxBB