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
Returning dynamic RCFClients
Re: Returning dynamic RCFClients
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> :
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.
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>(...) );
}
}
Re: Returning dynamic RCFClients
Here is an example of what I'm trying to accomplish but can't seem to get working:
Thanks for your assistance.
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;
}
Re: Returning dynamic RCFClients
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.
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.