diff --git a/src/liboncilla/Oncilla.cpp b/src/liboncilla/Oncilla.cpp index ea80150..e68a61b 100644 --- a/src/liboncilla/Oncilla.cpp +++ b/src/liboncilla/Oncilla.cpp @@ -1,174 +1,174 @@ /* * Oncilla.cpp * * Created on: 15 dec. 2011 * Author: Alexandre Tuleu, Arne Nordmann */ #include "Oncilla.h" #include namespace rci { namespace oncilla { ::rci::oncilla::Oncilla::SetOfBackend Oncilla::s_backends; bool ::rci::oncilla::Oncilla::s_throwExceptionOnInitialization(true); void Oncilla::throwExceptionOnInitialization(bool throwOn) { s_throwExceptionOnInitialization = throwOn; } bool Oncilla::willThrowOnInitialization() { return s_throwExceptionOnInitialization; } std::string Oncilla::nameOfLeg(Leg l){ static const char * legNames[NUM_LEGS + 1] = { "Left Fore", //LEFT_FORE "Right Fore", //RIGHT_FORE "Left Hind", //LEFT_HIND "Right Hind", //RIGHT_HIND "Undefined" }; //oncly consider index in range [0 NUM_LEGS], defaulting to NUM_LEGS if out of bound. int index((l < 0 ) ? NUM_LEGS : (l > NUM_LEGS ? NUM_LEGS : l)); return legNames[index]; } Oncilla::Oncilla(const OncillaBackend::Ptr & backend) : d_backend(backend){ init(); } Oncilla::Oncilla(){ init(); } Oncilla::~Oncilla() { } #define CatchedNodeCreation(NodeName,leg) do {\ NodeName::Ptr res ## NodeName ; \ try{ \ res ## NodeName = d_backend->Create ## NodeName(leg, Oncilla::nameOfLeg(leg) + "Oncilla " + #NodeName ); \ + if (! res ## NodeName ) {\ + d_ ## NodeName ## Errors[leg] = Oncilla::RuntimeErrPtr(new std::runtime_error("Backend did not created the node nor thrown exception for node " + Oncilla::nameOfLeg(leg) + " " + #NodeName )); \ + } \ } catch( const std::exception & e) { \ res ## NodeName = NodeName::Ptr(); \ d_ ## NodeName ## Errors[leg] = Oncilla::RuntimeErrPtr(new std::runtime_error(e.what())); \ } \ - if (! res ## NodeName) {\ - d_ ## NodeName ## Errors[leg] = Oncilla::RuntimeErrPtr(new std::runtime_error("Backend did not created the node nor thrown exception for node " + Oncilla::nameOfLeg(leg) + " " + #NodeName )); \ - } \ if (Oncilla::willThrowOnInitialization() && d_ ## NodeName ## Errors[leg] ) { \ throw *(d_ ## NodeName ## Errors[leg]); \ } \ d_ ## NodeName ##s.push_back(res ## NodeName );\ }while(0) void Oncilla::init() { if(!d_backend){ BackendLoader bl; d_backend = bl.SuitableBackend(); } //If the backend get destroyed, then the plugin is dl_close(). So if //we get some reference that are get from Oncilla object, and Oncilla //object is destroyed, then we are sure the plugin is still loaded. //we are not sure only if the shared_ptr are put in statix variable. s_backends.insert(d_backend); //never removed until end of program. d_synchronizer = d_backend->CreateSynchronizer(); d_L0s.reserve(4); d_L0Errors.resize(4); d_L1s.reserve(4); d_L1Errors.resize(4); d_L2s.reserve(4); d_L2Errors.resize(4); d_L3s.reserve(4); d_L3Errors.resize(4); LIBONCILLA_FOREACH_LEG(l){ CatchedNodeCreation(L0,l); CatchedNodeCreation(L1,l); CatchedNodeCreation(L2,l); CatchedNodeCreation(L3,l); } try { d_trunk = d_backend->CreateTrunk(); + if(!d_trunk) { + d_trunkError = RuntimeErrPtr(new std::runtime_error("Backend did not created Trunk nor throw exception.")); + } } catch (const std::exception & e) { d_trunk = rci::oncilla::Trunk::Ptr(); d_trunkError = RuntimeErrPtr(new std::runtime_error(e.what())); } - if(!d_trunk) { - d_trunkError = RuntimeErrPtr(new std::runtime_error("Backend did not created Trunk nor throw exception.")); - } if(willThrowOnInitialization() && d_trunkError) { throw *d_trunkError; } d_supervisor = Supervisor::Ptr(new Supervisor(*d_backend)); d_synchronizer->calibrateIfNeeded(); } #define CheckAndThrow(NodeName,leg) do{\ if(!d_ ## NodeName ## s[leg]) {\ throw *(d_ ## NodeName ## Errors[leg]);\ }\ }while(0) const L0::Ptr & Oncilla::getL0(Leg l) const { CheckAndThrow(L0,l); return d_L0s[l]; } const L1::Ptr & Oncilla::getL1(Leg l) const { CheckAndThrow(L1,l); return d_L1s[l]; } const L2::Ptr & Oncilla::getL2(Leg l) const { CheckAndThrow(L2,l); return d_L2s[l]; } const L3::Ptr & Oncilla::getL3(Leg l) const { CheckAndThrow(L3,l); return d_L3s[l]; } const Trunk::Ptr & Oncilla::getTrunk() const { if(!d_trunk){ throw *d_trunkError; } return d_trunk; } const rci::oncilla::Synchronizer::Ptr & Oncilla::getSynchronizer() const { if(!d_synchronizer){ throw std::logic_error("Internal error : synchronizer seems unitialized" ", but it should never happen. Please report this bug."); } return this->d_synchronizer; } const rci::oncilla::Supervisor::Ptr & Oncilla::getSupervisor() const { return d_supervisor; } } } diff --git a/src/liboncilla/Supervisor.cpp b/src/liboncilla/Supervisor.cpp index 42bf81b..423e48a 100644 --- a/src/liboncilla/Supervisor.cpp +++ b/src/liboncilla/Supervisor.cpp @@ -1,110 +1,109 @@ /* * Supervisor.cpp * * Created on: Jan 21, 2013 * Author: tuleu */ #include "Supervisor.h" #include #include "Oncilla.h" namespace rci { namespace oncilla { Supervisor::Supervisor(OncillaBackend & backend) : d_l4s(4,SupervisorL4::Ptr()) , d_trunk() , d_world(){ d_l4s.resize(4); d_l4Errors.resize(4); LIBONCILLA_FOREACH_LEG(l) { SupervisorL4::Ptr res; try { res = backend.CreateSupervisorL4(l, Oncilla::nameOfLeg(l) + " Oncilla Supervised L4"); + if(!res) { + d_l4Errors[l] = RuntimeErrPtr(new std::runtime_error("Backend did not create " + Oncilla::nameOfLeg(l) + " SupervisorL4 node nor throw exception.")); + } } catch(const std::exception & e) { res = SupervisorL4::Ptr(); d_l4Errors[l] = RuntimeErrPtr(new std::runtime_error(e.what())); } - if(!res) { - d_l4Errors[l] = RuntimeErrPtr(new std::runtime_error("Backend did not create " + Oncilla::nameOfLeg(l) + " SupervisorL4 node nor throw exception.")); - } - d_l4s[l] = res; if(Oncilla::willThrowOnInitialization() && d_l4Errors[l]) { throw *(d_l4Errors[l]); } } try { d_trunk = backend.CreateSupervisorTrunk(); + if(!d_trunk) { + d_trunkError = RuntimeErrPtr(new std::runtime_error("Backend did not created SupervisorTrunk node nor throw exception.")); + } } catch(const std::exception & e) { d_trunk = SupervisorTrunk::Ptr(); d_trunkError = RuntimeErrPtr(new std::runtime_error(e.what())); } - if(!d_trunk) { - d_trunkError = RuntimeErrPtr(new std::runtime_error("Backend did not created SupervisorTrunk node nor throw exception.")); - } try{ d_world = backend.CreateSupervisorWorld(); + if (!d_world ) { + d_worldError = RuntimeErrPtr(new std::runtime_error("Backend did not created SupervisorWorld node nor throw exception.")); + } } catch (const std::exception & e) { d_world = SupervisorWorld::Ptr(); d_worldError = RuntimeErrPtr(new std::runtime_error(e.what())); } - if (!d_world ) { - d_worldError = RuntimeErrPtr(new std::runtime_error("Backend did not created SupervisorWorld node nor throw exception.")); - } if(!Oncilla::willThrowOnInitialization()) { return; } if(d_trunkError) { throw *d_trunkError; } if(d_worldError) { throw *d_worldError; } } Supervisor::~Supervisor(){ } const SupervisorTrunk::Ptr & Supervisor::getTrunk(){ if (!d_trunk) { throw *d_trunkError; } return d_trunk; } const SupervisorL4::Ptr & Supervisor::getL4(Leg l){ if (l >= NUM_LEGS){ std::ostringstream os; os << "Leg " << l << "is out of range [0 3]."; throw std::out_of_range(os.str()); } if(!d_l4s[l]) { throw *(d_l4Errors[l]); } return d_l4s[l]; } const SupervisorWorld::Ptr & Supervisor::getWorld(){ if(!d_world){ throw * d_worldError; } return d_world; } } /* namespace oncilla */ } /* namespace rci */