diff --git a/src/liboncilla/Supervisor.cpp b/src/liboncilla/Supervisor.cpp index 729d6b5..42bf81b 100644 --- a/src/liboncilla/Supervisor.cpp +++ b/src/liboncilla/Supervisor.cpp @@ -1,45 +1,110 @@ /* * 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(backend.CreateSupervisorTrunk()) - , d_world(backend.CreateSupervisorWorld()){ + , d_trunk() + , d_world(){ + d_l4s.resize(4); + d_l4Errors.resize(4); + LIBONCILLA_FOREACH_LEG(l) { - d_l4s[l] = backend.CreateSupervisorL4(l, - Oncilla::nameOfLeg(l) + " Oncilla Supervised L4"); + SupervisorL4::Ptr res; + try { + res = backend.CreateSupervisorL4(l, + Oncilla::nameOfLeg(l) + " Oncilla Supervised L4"); + } 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(); + } 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(); + } 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 */ diff --git a/src/liboncilla/Supervisor.h b/src/liboncilla/Supervisor.h index 697e012..a6c1dc9 100644 --- a/src/liboncilla/Supervisor.h +++ b/src/liboncilla/Supervisor.h @@ -1,41 +1,48 @@ /* * Supervisor.h * * Created on: Jan 21, 2013 * Author: tuleu */ #ifndef SUPERVISOR_H_ #define SUPERVISOR_H_ #include #include "common.h" +#include #include class OncillaBackend; namespace rci { namespace oncilla { class Supervisor { public: typedef boost::shared_ptr Ptr; Supervisor(OncillaBackend & backend); virtual ~Supervisor(); const SupervisorTrunk::Ptr & getTrunk(); const SupervisorL4::Ptr & getL4(Leg l); const SupervisorWorld::Ptr & getWorld(); private: + typedef std::tr1::shared_ptr RuntimeErrPtr; + typedef std::vector ListOfError; + std::vector d_l4s; + ListOfError d_l4Errors; SupervisorTrunk::Ptr d_trunk; + RuntimeErrPtr d_trunkError; SupervisorWorld::Ptr d_world; + RuntimeErrPtr d_worldError; }; } /* namespace oncilla */ } /* namespace rci */ #endif /* SUPERVISOR_H_ */