Skip to content

Commit

Permalink
Updated entity enable/disable logic to be consistent with fffd3e5
Browse files Browse the repository at this point in the history
  • Loading branch information
allsey87 committed Mar 30, 2018
1 parent 4fa7b0a commit d14ebd8
Show file tree
Hide file tree
Showing 12 changed files with 235 additions and 208 deletions.
81 changes: 52 additions & 29 deletions src/plugins/simulator/entities/directional_led_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,25 @@ namespace argos {
/****************************************/

void CDirectionalLEDEntity::Destroy() {
if(HasMedium()) {
RemoveFromMedium();
}
Disable();
}

/****************************************/
/****************************************/

void CDirectionalLEDEntity::SetEnabled(bool b_enabled) {
CEntity::SetEnabled(m_cColor != CColor::BLACK);
/* Perform generic enable behavior */
CEntity::SetEnabled(b_enabled);
if(b_enabled) {
/* Enable entity in medium */
if(m_pcMedium && GetIndex() >= 0)
m_pcMedium->AddEntity(*this);
}
else {
/* Disable entity in medium */
if(m_pcMedium)
m_pcMedium->RemoveEntity(*this);
}
}

/****************************************/
Expand All @@ -92,35 +101,21 @@ namespace argos {
/****************************************/
/****************************************/

void CDirectionalLEDEntity::AddToMedium(CDirectionalLEDMedium& c_medium) {
if(HasMedium()) RemoveFromMedium();
m_pcMedium = &c_medium;
c_medium.AddEntity(*this);
Enable();
}

/****************************************/
/****************************************/

void CDirectionalLEDEntity::RemoveFromMedium() {
try {
GetMedium().RemoveEntity(*this);
m_pcMedium = nullptr;
Disable();
}
catch(CARGoSException& ex) {
THROW_ARGOSEXCEPTION_NESTED("Can't remove directional LED entity \"" << GetContext() + GetId() << "\" from medium.", ex);
CDirectionalLEDMedium& CDirectionalLEDEntity::GetMedium() const {
if(m_pcMedium == nullptr) {
THROW_ARGOSEXCEPTION("directional LED entity \"" << GetContext() <<
GetId() << "\" has no associated medium.");
}
return *m_pcMedium;
}

/****************************************/
/****************************************/

CDirectionalLEDMedium& CDirectionalLEDEntity::GetMedium() const {
if(m_pcMedium == nullptr) {
THROW_ARGOSEXCEPTION("Directional LED entity \"" << GetContext() + GetId() << "\" has no medium associated.");
}
return *m_pcMedium;

void CDirectionalLEDEntity::SetMedium(CDirectionalLEDMedium& c_medium) {
if(m_pcMedium != nullptr && m_pcMedium != &c_medium)
m_pcMedium->RemoveEntity(*this);
m_pcMedium = &c_medium;
}

/****************************************/
Expand Down Expand Up @@ -167,7 +162,35 @@ namespace argos {
/****************************************/
/****************************************/

REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(CDirectionalLEDEntity);
class CSpaceOperationAddCDirectionalLEDEntity : public CSpaceOperationAddEntity {
public:
void ApplyTo(CSpace& c_space, CDirectionalLEDEntity& c_entity) {
/* Add entity to space - this ensures that the directional LED entity
* gets an id before being added to the directional LED medium */
c_space.AddEntity(c_entity);
/* Enable the directional LED entity, if it's enabled - this ensures that
* the entity gets added to the directional LED medium if it's enabled */
c_entity.SetEnabled(c_entity.IsEnabled());
}
};

class CSpaceOperationRemoveCDirectionalLEDEntity : public CSpaceOperationRemoveEntity {
public:
void ApplyTo(CSpace& c_space, CDirectionalLEDEntity& c_entity) {
/* Disable the entity - this ensures that the entity is
* removed from the directional LED medium */
c_entity.Disable();
/* Remove the directional LED entity from space */
c_space.RemoveEntity(c_entity);
}
};

REGISTER_SPACE_OPERATION(CSpaceOperationAddEntity,
CSpaceOperationAddCDirectionalLEDEntity,
CDirectionalLEDEntity);
REGISTER_SPACE_OPERATION(CSpaceOperationRemoveEntity,
CSpaceOperationRemoveCDirectionalLEDEntity,
CDirectionalLEDEntity);

/****************************************/
/****************************************/
Expand Down
29 changes: 9 additions & 20 deletions src/plugins/simulator/entities/directional_led_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,6 @@ namespace argos {
return "directional_led";
}

/**
* Adds the directional LEDs to the wanted directional LED medium.
* If this LED has already been added to a medium, the LED is
* removed from that medium and then added to the passed one.
* This behavior is to enforce that, at any time, an LED is
* under the control of (at most) a single medium.
* @param c_medium The LED medium.
* @see CDirectionalLEDMedium
*/
void AddToMedium(CDirectionalLEDMedium& c_medium);

/**
* Removes the directional LEDs from the specified directional LED medium.
* @param c_medium The LED medium.
* @see CDirectionalLEDMedium
*/
void RemoveFromMedium();

/**
* Returns <tt>true</tt> if this directional LED is associated to a medium.
* @return <tt>true</tt> if this directional LED is associated to a medium.
Expand All @@ -130,12 +112,19 @@ namespace argos {
}

/**
* Returns the medium associated to this LED.
* @return The medium associated to this LED.
* Returns the medium associated to this directional LED.
* @return The medium associated to this directional LED.
* @see CDirectionalLEDMedium
*/
CDirectionalLEDMedium& GetMedium() const;

/**
* Sets the medium associated to this entity.
* @param c_medium The medium to associate to this entity.
* @see CDirectionalLEDMedium
*/
void SetMedium(CDirectionalLEDMedium& c_medium);

protected:

CRadians m_cObservableAngle;
Expand Down
26 changes: 10 additions & 16 deletions src/plugins/simulator/entities/directional_led_equipped_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ namespace argos {
* 3. the "body" is an embodied entity
* If any of the above is false, this line will bomb out.
*/
CEmbodiedEntity& cBody = GetParent().GetComponent<CEmbodiedEntity>("body");
CEmbodiedEntity& cBody =
GetParent().GetComponent<CEmbodiedEntity>("body");
/* Add the LED to this container */
m_vecInstances.emplace_back(*pcLED,
cBody.GetAnchor(strAnchorId),
Expand Down Expand Up @@ -99,7 +100,9 @@ namespace argos {
/****************************************/

void CDirectionalLEDEquippedEntity::Enable() {
CEntity::Enable();
/* Perform generic enable behavior */
CComposableEntity::Enable();
/* Enable anchors */
for(SInstance& s_instance : m_vecInstances) {
s_instance.Anchor.Enable();
}
Expand All @@ -109,7 +112,9 @@ namespace argos {
/****************************************/

void CDirectionalLEDEquippedEntity::Disable() {
CEntity::Disable();
/* Perform generic disable behavior */
CComposableEntity::Disable();
/* Disable anchors */
for(SInstance& s_instance : m_vecInstances) {
s_instance.Anchor.Disable();
}
Expand Down Expand Up @@ -196,21 +201,10 @@ namespace argos {
/****************************************/
/****************************************/

void CDirectionalLEDEquippedEntity::AddToMedium(CDirectionalLEDMedium& c_medium) {
void CDirectionalLEDEquippedEntity::SetMedium(CDirectionalLEDMedium& c_medium) {
for(SInstance& s_instance : m_vecInstances) {
s_instance.LED.AddToMedium(c_medium);
s_instance.LED.SetMedium(c_medium);
}
Enable();
}

/****************************************/
/****************************************/

void CDirectionalLEDEquippedEntity::RemoveFromMedium() {
for(SInstance& s_instance : m_vecInstances) {
s_instance.LED.RemoveFromMedium();
}
Disable();
}

/****************************************/
Expand Down
12 changes: 3 additions & 9 deletions src/plugins/simulator/entities/directional_led_equipped_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,11 @@ namespace argos {
void SetLEDColors(const std::vector<CColor>& vec_colors);

/**
* Adds the LEDs to the wanted LED medium.
* @param c_medium The LED medium.
* Sets the medium associated to this entity.
* @param c_medium The medium to associate to this entity.
* @see CDirectionalLEDMedium
*/
void AddToMedium(CDirectionalLEDMedium& c_medium);

/**
* Removes the LEDs from the associated LED medium.
* @see CDirectionalLEDMedium
*/
void RemoveFromMedium();
void SetMedium(CDirectionalLEDMedium& c_medium);

virtual std::string GetTypeDescription() const {
return "directional_leds";
Expand Down
77 changes: 62 additions & 15 deletions src/plugins/simulator/entities/radio_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,40 +43,59 @@ namespace argos {
/****************************************/
/****************************************/

void CRadioEntity::AddToMedium(CRadioMedium& c_medium) {
if(HasMedium()) RemoveFromMedium();
m_pcMedium = &c_medium;
c_medium.AddEntity(*this);
Enable();
void CRadioEntity::Reset() {
/* Erase received messages */
m_vecData.clear();
}

/****************************************/
/****************************************/

void CRadioEntity::RemoveFromMedium() {
try {
GetMedium().RemoveEntity(*this);
m_pcMedium = nullptr;
Disable();
void CRadioEntity::Destroy() {
Disable();
}

/****************************************/
/****************************************/

void CRadioEntity::SetEnabled(bool b_enabled) {
/* Perform generic enable behavior */
CEntity::SetEnabled(b_enabled);
if(b_enabled) {
/* Enable entity in medium */
if(m_pcMedium && GetIndex() >= 0)
m_pcMedium->AddEntity(*this);
}
catch(CARGoSException& ex) {
THROW_ARGOSEXCEPTION_NESTED("Can't remove radio entity \"" << GetContext() + GetId() << "\" from medium.", ex);
else {
/* Disable entity in medium */
if(m_pcMedium)
m_pcMedium->RemoveEntity(*this);
}
}

/****************************************/
/****************************************/

CRadioMedium& CRadioEntity::GetMedium() const {
if(m_pcMedium == nullptr) {
THROW_ARGOSEXCEPTION("Radio entity \"" << GetContext() + GetId() << "\" has no medium associated.");
THROW_ARGOSEXCEPTION("radio entity \"" << GetContext() << GetId() <<
"\" has no associated medium.");
}
return *m_pcMedium;
}

/****************************************/
/****************************************/

void CRadioEntity::SetMedium(CRadioMedium& c_medium) {
if(m_pcMedium != nullptr && m_pcMedium != &c_medium)
m_pcMedium->RemoveEntity(*this);
m_pcMedium = &c_medium;
}

/****************************************/
/****************************************/

void CRadioEntitySpaceHashUpdater::operator()(CAbstractSpaceHash<CRadioEntity>& c_space_hash,
CRadioEntity& c_element) {
/* Calculate the position of the radop in the space hash */
Expand Down Expand Up @@ -112,7 +131,35 @@ namespace argos {
/****************************************/
/****************************************/

REGISTER_STANDARD_SPACE_OPERATIONS_ON_ENTITY(CRadioEntity);
class CSpaceOperationAddCRadioEntity : public CSpaceOperationAddEntity {
public:
void ApplyTo(CSpace& c_space, CRadioEntity& c_entity) {
/* Add entity to space - this ensures that the radio entity
* gets an id before being added to the radio medium */
c_space.AddEntity(c_entity);
/* Enable the radio entity, if it's enabled - this ensures that
* the entity gets added to the radio if it's enabled */
c_entity.SetEnabled(c_entity.IsEnabled());
}
};

class CSpaceOperationRemoveCRadioEntity : public CSpaceOperationRemoveEntity {
public:
void ApplyTo(CSpace& c_space, CRadioEntity& c_entity) {
/* Disable the entity - this ensures that the entity is
* removed from the radio medium */
c_entity.Disable();
/* Remove the radio entity from space */
c_space.RemoveEntity(c_entity);
}
};

REGISTER_SPACE_OPERATION(CSpaceOperationAddEntity,
CSpaceOperationAddCRadioEntity,
CRadioEntity);
REGISTER_SPACE_OPERATION(CSpaceOperationRemoveEntity,
CSpaceOperationRemoveCRadioEntity,
CRadioEntity);

/****************************************/
/****************************************/
Expand Down
31 changes: 12 additions & 19 deletions src/plugins/simulator/entities/radio_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ namespace argos {

virtual void Init(TConfigurationNode& t_tree);

virtual void Reset() {}
virtual void Reset();

virtual void Destroy();

virtual void SetEnabled(bool b_enabled);

/**
* Returns a constant reference to the received data.
Expand Down Expand Up @@ -104,24 +108,6 @@ namespace argos {
return "radio";
}

/**
* Adds the radios to the specified radio medium.
* If this radio has already been added to a medium, the radio is
* first removed from that medium and then added to the passed one.
* This behavior is to enforce that, at any time, a radio is
* under the control of (at most) a single medium.
* @param c_medium The radio medium.
* @see CRadioMedium
*/
void AddToMedium(CRadioMedium& c_medium);

/**
* Removes the radios from the specified radio medium.
* @param c_medium The radio medium.
* @see CRadioMedium
*/
void RemoveFromMedium();

/**
* Returns <tt>true</tt> if this radio is associated to a medium.
* @return <tt>true</tt> if this radio is associated to a medium.
Expand All @@ -138,6 +124,13 @@ namespace argos {
*/
CRadioMedium& GetMedium() const;

/**
* Sets the medium associated to this entity.
* @param c_medium The medium to associate to this entity.
* @see CRadioMedium
*/
void SetMedium(CRadioMedium& c_medium);

protected:

Real m_fRange;
Expand Down
Loading

0 comments on commit d14ebd8

Please sign in to comment.