Skip to content

Commit

Permalink
EasyProxy improvements
Browse files Browse the repository at this point in the history
* complete proxy pattern by allowing self and element assignment
* move exception to shared_ptr to simplify life cicle management
  • Loading branch information
varhub committed Dec 14, 2015
1 parent fa9755c commit f675a69
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 8 deletions.
4 changes: 2 additions & 2 deletions include/easyiceconfig/EasyProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ namespace EasyIce{
/// http://en.cppreference.com/w/cpp/language/using_declaration
/// Option 1
/// just add this element to current namespace
using easyiceconfig::proxies::EasyProxy;
//using easyiceconfig::proxies::EasyProxy;

/// Option 2 (fallback)
/// import whole namespace in current one
//using namespace easyiceconfig::proxies;
using namespace easyiceconfig::proxies;


}//NS
Expand Down
29 changes: 23 additions & 6 deletions include/easyiceconfig/proxies.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <Ice/Communicator.h>
#include <Ice/Ice.h>
#include <IceUtil/IceUtil.h>
#include <boost/shared_ptr.hpp>

namespace easyiceconfig {
namespace proxies{
Expand Down Expand Up @@ -72,19 +73,20 @@ template<typename Prx>
*/
class EasyProxy {
public:
/**
* Constructor to allow lazy initialization
*/
EasyProxy(){}

EasyProxy(const Ice::CommunicatorPtr &ic, const std::string proxyStr, bool stringIsAlreadyProxy){
try {
proxy = easyiceconfig::proxies::createProxy<Prx>(ic, proxyStr, stringIsAlreadyProxy);
}catch (Ice::Exception& e){
ex = e.ice_clone();
ex = boost::shared_ptr<Ice::Exception>(e.ice_clone());
}
}

virtual ~EasyProxy(){
if (ex){
delete ex;
ex = 0;
}
}

operator bool (){
Expand All @@ -99,6 +101,21 @@ class EasyProxy {
return proxy.operator->();
}

/**
* complements "proxy pattern" by allow direct assignment.
*/
Prx& operator= (const Prx &prx){
proxy = prx;
return proxy;
}

EasyProxy<Prx>& operator= (const EasyProxy<Prx> &other){
proxy = other.proxy;
ex = other.ex;

return *this;
}

Ice::Exception& exception(){
assert(ex != 0);
return *ex;
Expand All @@ -107,7 +124,7 @@ class EasyProxy {

protected:
Prx proxy;
Ice::Exception *ex;
boost::shared_ptr<Ice::Exception> ex;

};

Expand Down
8 changes: 8 additions & 0 deletions src/tests/test_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,13 @@ int main(int argc, char* argv[]){
}else
std::cout<<proxy.exception()<<std::endl;


// copying
EasyIce::EasyProxy<IceMX::MetricsPrx> p2;
p2 = EasyIce::EasyProxy<IceMX::MetricsPrx>(ic, "bad endpoint definition", true);
try{
p2 = EasyIce::createProxy<IceMX::MetricsPrx>(ic, "bad endpoint definition", true);
}catch(Ice::Exception){}

ic->shutdown();
}

0 comments on commit f675a69

Please sign in to comment.