Returning dynamic RCFClients

RCF support and general discussion.
Post Reply
kris24076
Posts: 2
Joined: Sat Mar 26, 2016 5:55 am

Returning dynamic RCFClients

Post by kris24076 »

Is there a way to return an RCFClient from an RCFClient member function?

I'm looking for something that kind of looks like the following example: http://www.cppremote.com/example5.html

jarl
Posts: 238
Joined: Mon Oct 03, 2011 4:53 am
Contact:

Re: Returning dynamic RCFClients

Post by jarl »

Hi,

All RcfClient<> classes are derived from RCF::I_RcfClient. So you could have a function which returns a I_RcfClient, or more specifically, a RcfClientPtr which is just a boost::shared_ptr<RCF::I_RcfClient> :

Code: Select all

#include <RCF/ClientStub.hpp>

RCF::RcfClientPtr someFactoryFunc()
{
    if (...)
    {
        return RCF::RcfClientPtr( new RcfClient<SomeInterface>(...) );
    }
    else
    {
        return RCF::RcfClientPtr( new RcfClient<AnotherInterface>(...) );
    }
}
Any code that calls this function would then have to use dynamic_cast<> to downcast the RcfClientPtr to an actual RcfClient<>, before it can make any calls.
Kind Regards

Jarl Lindrud
Delta V Software
http://www.deltavsoft.com

kris24076
Posts: 2
Joined: Sat Mar 26, 2016 5:55 am

Re: Returning dynamic RCFClients

Post by kris24076 »

Here is an example of what I'm trying to accomplish but can't seem to get working:

Code: Select all

#include <iostream>
#include "RCF/RCF.hpp"
#include "RCF/ClientStub.hpp"
#include "idl.h"

class GoodbyeWorldImpl
{
public:
	void Print(const std::string &s)
	{
		std::cout << "I_GoodbyeWorld service: " << s << std::endl;
	}
};

class HelloWorldImpl
{
private:
	GoodbyeWorldImpl m_cChild;

public:
	void Print(const std::string & s)
	{
		std::cout << "I_HelloWorld service: " << s << std::endl;
	}

	RCF::RcfClientPtr child()
	{
		return RCF::RcfClientPtr(new RcfClient<I_GoodbyeWorld>(m_cChild));
	}
};

int main()
{
	RCF::RcfInitDeinit rcfInit;

	HelloWorldImpl helloWorld;
	RCF::RcfServer server( RCF::TcpEndpoint(50001) );
	server.bind<I_HelloWorld>(helloWorld);
	server.start();

	std::cout << "Press Enter to exit..." << std::endl;
	std::cin.get();

	return 0;
}
Thanks for your assistance.

jarl
Posts: 238
Joined: Mon Oct 03, 2011 4:53 am
Contact:

Re: Returning dynamic RCFClients

Post by jarl »

OK, I see what you mean. This isn't really possible, and I wouldn't recommend structuring a system like that either.

I think a simpler way of implementing a solution is to just return whatever information you need, in a struct of some sort, and then you can have a factory function on the callers side, to instantiate the relevant RcfClient<>, using the information returned from the call. That way, any complexities around which interface to use, which endpoint to pass to it, any further options that need to be applied, etc , can all be handled directly by your application, rather than RCF.
Kind Regards

Jarl Lindrud
Delta V Software
http://www.deltavsoft.com

Post Reply