diff --git a/src/liboncilla/Oncilla.cpp b/src/liboncilla/Oncilla.cpp index 72885b0..ac491dc 100644 --- a/src/liboncilla/Oncilla.cpp +++ b/src/liboncilla/Oncilla.cpp @@ -1,111 +1,100 @@ /* * Oncilla.cpp * * Created on: 15 dec. 2011 * Author: Alexandre Tuleu, Arne Nordmann */ #include "Oncilla.h" #include namespace rci { namespace oncilla { ::rci::oncilla::Synchronizer::Ptr Oncilla::s_synchronizer; -Oncilla::LegNames Oncilla::s_legNames; -const std::string & Oncilla::nameOfLeg(Leg l){ - if(s_legNames.empty()){ - s_legNames.push_back("Left Fore"); - s_legNames.push_back("Right Fore"); - s_legNames.push_back("Left Hind"); - s_legNames.push_back("Right Hind"); - s_legNames.push_back("Undefined"); - } - if(l >= NUM_LEGS){ - return s_legNames.back(); - } - return s_legNames[l]; +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(){ if(!s_synchronizer){ if(!d_backend){ BackendLoader bl; //we have to keep alive the pointer or dlclose() will be called once //We free him. Or we could add BackendLoader as a member of Oncilla // I prefer only to have the Object creator interface. d_backend = bl.SuitableBackend(); } s_synchronizer = d_backend->CreateSynchronizer(); init(); } } Oncilla::~Oncilla() { } -#define CREATE_NODES(LegName,Leg)do{\ - d_L0s.push_back(d_backend->CreateL0(Leg,LegName " Oncilla L0" ));\ - d_L1s.push_back(d_backend->CreateL1(Leg,\ - LegName " Oncilla L1"));\ - d_L2s.push_back(d_backend->CreateL2(Leg,\ - LegName " Oncilla L2"));\ - d_L3s.push_back(d_backend->CreateL3(Leg,LegName " Oncilla L3"));\ -}while(0) - void Oncilla::init() { d_L0s.reserve(4); d_L1s.reserve(4); d_L2s.reserve(4); d_L3s.reserve(4); for(rci::oncilla::Leg l = LEFT_FORE; l < NUM_LEGS; ++l){ - d_L0s.push_back(d_backend->CreateL0(l,rci::oncilla::LegNames[l] + std::string(" Oncilla L0"))); - d_L1s.push_back(d_backend->CreateL1(l,rci::oncilla::LegNames[l] + std::string(" Oncilla L1"))); - d_L2s.push_back(d_backend->CreateL2(l,rci::oncilla::LegNames[l] + std::string(" Oncilla L2"))); - d_L3s.push_back(d_backend->CreateL3(l,rci::oncilla::LegNames[l] + std::string(" Oncilla L3"))); + d_L0s.push_back(d_backend->CreateL0(l,Oncilla::nameOfLeg(l) + " Oncilla L0")); + d_L1s.push_back(d_backend->CreateL1(l,Oncilla::nameOfLeg(l) + " Oncilla L1")); + d_L2s.push_back(d_backend->CreateL2(l,Oncilla::nameOfLeg(l) + " Oncilla L2")); + d_L3s.push_back(d_backend->CreateL3(l,Oncilla::nameOfLeg(l) + " Oncilla L3")); } d_trunk = d_backend->CreateTrunk(); } const L0::Ptr & Oncilla::getL0(Leg l) const { return this->d_L0s[l]; } const L1::Ptr & Oncilla::getL1(Leg l) const { return this->d_L1s[l]; } const L2::Ptr & Oncilla::getL2(Leg l) const { return this->d_L2s[l]; } const L3::Ptr & Oncilla::getL3(Leg l) const { return this->d_L3s[l]; } const Trunk::Ptr & Oncilla::getTrunk() const { return this->d_trunk; } const rci::oncilla::Synchronizer::Ptr & Oncilla::getSynchronizer() const { if(!s_synchronizer){ throw std::logic_error("Internal error : synchronizer seems unitialized" ", but it should never happen. Please report this bug."); } return this->s_synchronizer; } } } diff --git a/src/liboncilla/Oncilla.h b/src/liboncilla/Oncilla.h index 1a496e8..cfc8b01 100644 --- a/src/liboncilla/Oncilla.h +++ b/src/liboncilla/Oncilla.h @@ -1,64 +1,62 @@ /* * Oncilla.h * * Created on: 15 dec. 2011 * Author: Alexandre Tuleu, Arne Nordmann */ #pragma once #include #include #include #include "common.h" #include "Supervisor.h" #include #include #include namespace rci { namespace oncilla { class BackendLoader; class Oncilla { public: typedef boost::shared_ptr Ptr; - static const std::string & nameOfLeg(Leg l); + static std::string nameOfLeg(Leg l); Oncilla(); virtual ~Oncilla(); const L0::Ptr & getL0(Leg l) const; const L1::Ptr & getL1(Leg l) const; const L2::Ptr & getL2(Leg l) const; const L3::Ptr & getL3(Leg l) const; const Trunk::Ptr & getTrunk() const; const rci::oncilla::Synchronizer::Ptr & getSynchronizer() const; const rci::oncilla::Supervisor::Ptr & getSupervisor() const; private: typedef std::vector LegNames; - static LegNames s_legNames; - void init(); static rci::oncilla::Synchronizer::Ptr s_synchronizer; std::vector d_L0s; std::vector d_L1s; std::vector d_L2s; std::vector d_L3s; Trunk::Ptr d_trunk; OncillaBackend::Ptr d_backend; }; } } diff --git a/src/liboncilla/Supervisor.cpp b/src/liboncilla/Supervisor.cpp index 5115031..b49a363 100644 --- a/src/liboncilla/Supervisor.cpp +++ b/src/liboncilla/Supervisor.cpp @@ -1,27 +1,28 @@ /* * 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()){ for(rci::oncilla::Leg l = LEFT_FORE; l < rci::oncilla::NUM_LEGS ; ++l){ - d_l4s[l] = backend.CreateSupervisorL4(l,LegNames[l] + std::string(" Oncilla Supervised L4")); + d_l4s[l] = backend.CreateSupervisorL4(l,Oncilla::nameOfLeg(l) + " Oncilla Supervised L4"); } } Supervisor::~Supervisor(){ // TODO Auto-generated destructor stub } } /* namespace oncilla */ } /* namespace rci */ diff --git a/src/liboncilla/common.h b/src/liboncilla/common.h index b7ca4aa..d4f8afd 100644 --- a/src/liboncilla/common.h +++ b/src/liboncilla/common.h @@ -1,33 +1,27 @@ /** * \file common.h * * \date Jun 28, 2012 * \author Alexandre Tuleu */ #ifndef LIBONCILLA_COMMON_H_ #define LIBONCILLA_COMMON_H_ namespace rci{ namespace oncilla{ enum Leg { LEFT_FORE = 0, RIGHT_FORE = 1, LEFT_HIND = 2, RIGHT_HIND = 3, NUM_LEGS = 4 }; - static const char* LegNames[NUM_LEGS] = { - "Left Fore", - "Right Fore", - "Left Hind", - "Right Hind" - }; } } #endif // LIBONCILLA_COMMON_H_