Mega Search
23.2 Million


Sign Up

Make a donation  
TTask::Future: how to use in C++  
News Group: embarcadero.public.cppbuilder.language.cpp

Hi All

Can anyone explain how I can translate this piece of code from Delphi to C++?

{code:delphi}
program Project1;

{$APPTYPE CONSOLE}

{$R *.res}
uses
  Winapi.Windows, System.SysUtils, System.Threading;
var
  AValue: IFuture ;
begin
  try
    AValue := TTask.Future(function: Integer
       begin
        Result := 10;
        Sleep( 5000 );
       end);
    Writeln( AValue.Value );
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
{code}

I'm trying to port this example [http://goo.gl/DokhIF] in C++, but I can't figure out 
how to treat the anonymous method in TTask.Future initialization.

http://goo.gl/DokhIF

I guess that I have to use the C++ TMethodRef class, but I don't know how.

Thanks!

Vinz

Vote for best question.
Score: 0  # Vote:  0
Date Posted: 21-Dec-2014, at 2:34 PM EST
From: Vincenzo Zafferani
 
Re: TTask::Future: how to use in C++  
News Group: embarcadero.public.cppbuilder.language.cpp
> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> Sorry, I can't provide an answer for that.  I don't use the System.Threading 
> library, and I have never seen HIDESBASE used on an abstract method before.

Ok. Thanks anyway.

Vinz

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 22-Dec-2014, at 11:30 PM EST
From: Vincenzo Zafferani
 
Re: TTask::Future: how to use in C++  
News Group: embarcadero.public.cppbuilder.language.cpp
Vincenzo wrote:

> I've tried your code. Now I'm faced with this error:
> 
> [bcc32 Error] System.Threading.hpp(639): E2113 Virtual function
> '_fastcall IFuture__1::Start()' conflicts with base class 'ITask'

Sorry, I can't provide an answer for that.  I don't use the System.Threading 
library, and I have never seen HIDESBASE used on an abstract method before.

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 22-Dec-2014, at 1:44 AM EST
From: Remy Lebeau (TeamB)
 
Re: TTask::Future: how to use in C++  
News Group: embarcadero.public.cppbuilder.language.cpp
Remy, thank you for you reply.

> {quote:title=Remy Lebeau (TeamB) wrote:}{quote}
> {code}
>     DelphiInterface< IFuture_1 >  AValue;
> {code}

I've tried your code. Now I'm faced with this error:

[bcc32 Error] System.Threading.hpp(639): E2113 Virtual function '_fastcall IFuture__1::Start()' conflicts with base class 'ITask'

It is related to this row in System.Threading.hpp:

{code}
	HIDESBASE virtual System::DelphiInterface > __fastcall Start(void) = 0 ;
{code}

Now my code looks like this one:

{code}
__interface IMyFutureIntf : public IInterface
{
public:
    virtual int __fastcall Invoke() = 0;
};

class TMyFutureImpl : public TInterfacedObject, public IMyFutureIntf
{
public:
    HRESULT STDMETHODCALLTYPE QueryInterface (const GUID& riid, void** ppvObject)
{ return TInterfacedObject::QueryInterface (riid, ppvObject); }
    ULONG STDMETHODCALLTYPE AddRef() { return TInterfacedObject::_AddRef(); }
    ULONG STDMETHODCALLTYPE Release() { return TInterfacedObject::_Release();
}
    int __fastcall Invoke()
    {
        Sleep( 5000 );
        return 10;
    }
};

void __fastcall TForm1::Button2Click(TObject *Sender)
{
   DelphiInterface< IFuture__1 >  AValue;
   DelphiInterface proc = new TMyFutureImpl;
   AValue = TTask::Future(proc);
   ShowMessage( AValue->Value );
}
{code}

I had to change the IFuture_1 (as in your example) in IFuture__1, in order to compile the snippet.

Can you point me in some direction in order to resolve this last issue?

Thanks in advance.

Vinz

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 21-Dec-2014, at 11:37 PM EST
From: Vincenzo Zafferani
 
Re: TTask::Future: how to use in C++  
News Group: embarcadero.public.cppbuilder.language.cpp
Vincenzo wrote:

> Can anyone explain how I can translate this piece of code from
> Delphi to C++?

> I'm trying to port this example [http://goo.gl/DokhIF] in C++, but I
> can't figure out how to treat the anonymous method in TTask.Future
> initialization.

Unfortunately, it requires extra work to use anonymous methods in C++.  Did 
you read Embarcadero's documentation about that topic?

How to Handle Delphi Anonymous Methods in C++
http://docwiki.embarcadero.com/RADStudio/XE7/en/How_to_Handle_Delphi_Anonymous_Methods_in_C%2B%2B

For example:
 
{code}
#include 

__interface IMyFutureIntf : public IInterface
{
public:
    virtual int __fastcall Invoke() = 0;
};

class TMyFutureImpl : public TInterfacedObject, public IMyFutureIntf
{
public:
    HRESULT STDMETHODCALLTYPE QueryInterface (const GUID& riid, void** ppvObject) 
{ return TInterfacedObject::QueryInterface (riid, ppvObject); }
    ULONG STDMETHODCALLTYPE AddRef() { return TInterfacedObject::_AddRef(); }
    ULONG STDMETHODCALLTYPE Release() { return TInterfacedObject::_Release(); 
}
 
    int __fastcall Invoke()
    {
        Sleep( 5000 );
        return 10;
    }
};

....
{
    DelphiInterface< IFuture_1 >  AValue;

    try
    {
        DelphiInterface proc = new TMyFutureImpl;
        AValue = TTask::Future(proc);
        // use AValue->Value as needed...
    }
    catch (const Exception &E)
    {
        // use E as needed...
    }
}
....
{code}

--
Remy Lebeau (TeamB)

Vote for best answer.
Score: 0  # Vote:  0
Date Posted: 21-Dec-2014, at 11:04 PM EST
From: Remy Lebeau (TeamB)