EntityRef.cpp
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * ArmarX is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * ArmarX is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * @package ArmarX::
17 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
18 * @date 2014
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include "EntityRef.h"
24 
25 #include "../MongoSerializer.h"
26 
28 #include <ArmarXCore/interface/serialization/JSONSerialization.h>
30 
31 #include <Ice/ObjectAdapter.h>
32 
33 namespace memoryx
34 {
36  {
37  }
38 
39  EntityRef::EntityRef(const std::string entityName)
40  {
41  this->entityName = entityName;
42  }
43 
44  EntityRef::EntityRef(EntityBasePtr entity, const std::string& segmentName, const std::string& memoryName, MemoryInterfacePrx memoryPrx, EntityMemorySegmentInterfacePrx segmentPrx)
45  {
46  this->entityId = entity->getId();
47  this->entityName = entity->getName();
48  this->memory = memoryPrx;
49  this->segment = segmentPrx;
50  this->segmentName = segmentName;
51  this->memoryName = memoryName;
52  }
53 
54  EntityRef::EntityRef(const EntityRef& entityRef) :
55  IceUtil::Shared(entityRef),
56  EntityRefBase(entityRef)
57  {
58  this->entityId = entityRef.entityId;
59  this->segment = entityRef.segment;
60  this->segmentName = entityRef.segmentName;
61  this->memory = entityRef.memory;
62  this->memoryName = entityRef.memoryName;
63  this->entityName = entityRef.entityName;
64  }
65 
66  void memoryx::EntityRef::serialize(const armarx::ObjectSerializerBasePtr& serializer, const Ice::Current& c) const
67  {
68  armarx::AbstractObjectSerializerPtr obj = armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
69 
70  obj->setString("entityId", entityId);
71  obj->setString("entityName", entityName);
72  obj->setString("segmentName", this->segmentName);
73  obj->setString("memoryName", this->memoryName);
74  }
75 
76  void memoryx::EntityRef::deserialize(const armarx::ObjectSerializerBasePtr& serializer, const Ice::Current& c)
77  {
78  armarx::AbstractObjectSerializerPtr obj = armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
79  entityId = obj->getString("entityId");
80  entityName = obj->getString("entityName");
81  memoryName = obj->getString("memoryName");
82  segmentName = obj->getString("segmentName");
83 
84 
85  if (c.adapter && c.adapter->getCommunicator())
86  {
87  memory = MemoryInterfacePrx::checkedCast(c.adapter->getCommunicator()->stringToProxy(memoryName));
88 
89  if (memory)
90  {
91  segment = memoryx::EntityMemorySegmentInterfacePrx::checkedCast(memory->getSegment(segmentName));
92  }
93  else
94  {
95  ARMARX_ERROR_S << "cannot create proxy to memory " << memoryName;
96  }
97 
98  if (!segment)
99  {
100  ARMARX_ERROR_S << "cannot create proxy to segment " << segmentName << " in memory " << memoryName;
101  }
102  }
103  else
104  {
105  ARMARX_ERROR_S << "adapter or communicator zero - cannot create proxy to segment " << segmentName;
106  }
107  }
108 
109  armarx::VariantDataClassPtr memoryx::EntityRef::clone(const Ice::Current& c) const
110  {
111  return new EntityRef(*this);
112  }
113 
115  {
116  return this->clone();
117  }
118 
119  std::string memoryx::EntityRef::output(const Ice::Current&) const
120  {
121  return entityName + "_" + entityId + " in segment (" + segmentName + ")";
122  }
123 
124  Ice::Int memoryx::EntityRef::getType(const Ice::Current&) const
125  {
127  }
128 
129  bool memoryx::EntityRef::validate(const Ice::Current&)
130  {
131  return true;
132  }
133 
134  EntityBasePtr memoryx::EntityRef::getEntity(const Ice::Current&) const
135  {
136  if (segment)
137  {
138  return segment->getEntityById(entityId);
139  }
140  else
141  {
142  return nullptr;
143  }
144  }
145 
146  //"hey", you may say, "can't you just define operator== on EntityRefBase in memoryx/global ns?",
147  //but unfortunately Ice already defines those in the memx ns and it takes precedence over the global definitions
148  bool EntityRef::equals(const EntityRefBasePtr& other, const Ice::Current&) const
149  {
150  return entityId == other->entityId && segmentName == other->segmentName && memoryName == other->memoryName;
151  }
152 }
armarx::VariantType::EntityRef
const armarx::VariantTypeId EntityRef
Definition: EntityRef.h:30
memoryx::EntityRef::deserialize
void deserialize(const armarx::ObjectSerializerBasePtr &serializer, const Ice::Current &c=Ice::emptyCurrent) override
Definition: EntityRef.cpp:76
memoryx::EntityRef::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: EntityRef.cpp:66
memoryx::EntityRef::equals
bool equals(const EntityRefBasePtr &other, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: EntityRef.cpp:148
AbstractObjectSerializer.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::EntityRef::getEntity
EntityBasePtr getEntity(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: EntityRef.cpp:134
memoryx::EntityRef::getType
Ice::Int getType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: EntityRef.cpp:124
IceUtil
Definition: Instance.h:21
memoryx::EntityRef
The EntityRef class is used to store references to Entities as values in other Entity instances.
Definition: EntityRef.h:45
IceInternal::Handle
Definition: forward_declarations.h:8
EntityRef.h
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
memoryx::EntityRef::EntityRef
EntityRef()
Definition: EntityRef.cpp:35
memoryx::EntityRef::clone
armarx::VariantDataClassPtr clone(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: EntityRef.cpp:109
memoryx::EntityRef::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: EntityRef.cpp:114
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
armarx::aron::type::ObjectPtr
std::shared_ptr< Object > ObjectPtr
Definition: Object.h:36
Logging.h
memoryx::EntityRef::output
std::string output(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: EntityRef.cpp:119
memoryx::EntityRef::validate
bool validate(const Ice::Current &c=Ice::emptyCurrent) override
Definition: EntityRef.cpp:129