RTC Forums

Subscription => Support => Topic started by: joepasquariello on February 12, 2013, 06:56:20 AM



Title: Example of TRtcFunction returning TRtcRecord
Post by: joepasquariello on February 12, 2013, 06:56:20 AM
I'm a new user of RTC SDK, and I find there are no examples of TRtcFunction OnExecute handlers that store anything but a simple type (Integer, DateTime, etc) in the Result. Can anyone provide an example of returning a Record, even a simple one two fields?

I'm actually a C++ Builder user, but a Delphi example would be helpful. This is what I'm doing in C++, but I don't understand whether I need to allocate memory for the record structure, as shown below.

Joe

void __fastcall TRtcServerForm::GetRecordExecute(TRtcConnection *Sender, TRtcFunctionInfo *Param,
          TRtcValue *Result)
{
  TRtcRecord *RtcRecord = new TRtcRecord;
  RtcRecord->asBoolean["field1"] = true;
  RtcRecord->asInteger["field2"] = 1000;
  RtcRecord->asFloat  ["field3"] = 1.1;
  Result->asRecord = RtcRecord;
  delete RtcRecord;
}


Title: Re: Example of TRtcFunction returning TRtcRecord
Post by: joepasquariello on February 12, 2013, 07:32:53 AM
After a little more thought, I've found this works, which makes more sense. Is this the correct approach? It's much simpler, and I assume that RTC will deallocate the TRtcRecord after it's done sending?

Joe

void __fastcall TRtcServerForm::GetRecordExecute(TRtcConnection *Sender, TRtcFunctionInfo *Param,
          TRtcValue *Result)
{
  TRtcRecord *RtcRecord = Result->NewRecord();
  RtcRecord->asBoolean["field1"] = true;
  RtcRecord->asInteger["field2"] = 1000;
  RtcRecord->asFloat  ["field3"] = 1.1;
}


Title: Re: Example of TRtcFunction returning TRtcRecord
Post by: cesarliws on February 12, 2013, 04:41:31 PM
Hi  joepasquariello,

You are right, RTC manages the life cicle of the child objects.
All child objects are destroyed with the parent object and Result is being passed in as a parameter, so it will be destroyed by the sender.

You can read more about Creating and destroying TRtcValue objects here in the FAQ (http://realthinclient.com/sdkarchive/index6ba16ba1.html).


Title: Re: Example of TRtcFunction returning TRtcRecord
Post by: joepasquariello on February 12, 2013, 08:28:48 PM
Thank you, Cesar. I read the article at the link you provided, and I think I understand it now. The article covers Record, Array, and DataSet, but does not mention ByteArray, which seems to be slightly different. Since NewRecord() assigns to the Value field and also returns a pointer to the record, I'm clear that the two snippets below are equivalent. Having the local pointer somewhat simplifies the syntax in C++, similar to using "with" in Delphi, if you add a lot of fields to the record, or if multiple levels are created.

  TRtcRecord *RtcRecord = Result->NewRecord();
  RtcRecord->asBoolean["field1"] = true;
 
  Result->NewRecord();
  Result->asRecord->asBoolean["field1"] = true;

For the RtcByteArray, since the value returned by NewByteArray() is a DynamicArray, and not a pointer, are the two snippets below equivalent? The value in ByteArray must be a reference to the object created in NewByteArray()?

  Rtctypes::RtcByteArray ByteArray = Result->NewByteArray( 1024*128 );
  for (int i=0; i<ByteArray.Length; i++)
    ByteArray = (Byte)i;

  Result->NewByteArray( 1024*128 );
  for (int i=0; i<Result->asByteArray.Length; i++)
    Result->asByteArray = (Byte)i;


Title: Re: Example of TRtcFunction returning TRtcRecord
Post by: cesarliws on February 12, 2013, 09:41:15 PM
Hi joepasquariello,

You missed the [] assignment in both snippets

Code:
Rtctypes::RtcByteArray ByteArray = Result->NewByteArray( 1024*128 ); 
  for (int i=0; i<ByteArray.Length; i++)
    ByteArray[i] = (Byte)i; // <- [i] was missing

Other than that, the two snippets will produce equivalent results.

I'll do more tests and if I find something useful I post another reply.


Title: Re: Example of TRtcFunction returning TRtcRecord
Post by: joepasquariello on February 13, 2013, 12:59:28 AM
Thanks again, Cesar. I don't know what happened to the "". I must have deleted them by accident. I do have them in the actual code.

Joe



Title: Re: Example of TRtcFunction returning TRtcRecord
Post by: D.Tkalcec (RTC) on February 13, 2013, 09:32:48 AM
You are right. Your code did include [ i ] (without spaces), but this is a BB tag used by this Forum for italic text and is the reason for the rest of your post (after the first [ i ]) to look like this. I didn't notice this either, until you wrote that you did actually use "" in your code. (notice the italic text and the missing [ i ]).

When posting code snippets, to avoid the Forum from interpreting part of your code as BB tags, you should use the "Insert Code" button (the "#" icon above). This will insert [ code ] (without spaces) before your code snippet and [ /code ] (also without spaces) after your code, disabling BB tag interpretation between the two. Here is how your two ByteArray examples would look like by selecting your code and using the "Insert Code" (#) button above:
Code:
  Rtctypes::RtcByteArray ByteArray = Result->NewByteArray( 1024*128 );
  for (int i=0; i<ByteArray.Length; i++)
    ByteArray[i] = (Byte)i;

  Result->NewByteArray( 1024*128 );
  for (int i=0; i<Result->asByteArray.Length; i++)
    Result->asByteArray[i] = (Byte)i;

I've made the same mistake a number of times when posting code examples, but I always keep forgetting it.

Best Regards,
Danijel Tkalcec


Title: Re: Example of TRtcFunction returning TRtcRecord
Post by: joepasquariello on February 14, 2013, 10:41:46 PM
Ah, thanks, Danijel. I will try to remember to use the Insert Code button.

Joe