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 <Ice/ObjectAdapter.h>
26 
28 #include <ArmarXCore/interface/serialization/JSONSerialization.h>
30 
31 #include "../MongoSerializer.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,
45  const std::string& segmentName,
46  const std::string& memoryName,
47  MemoryInterfacePrx memoryPrx,
48  EntityMemorySegmentInterfacePrx segmentPrx)
49  {
50  this->entityId = entity->getId();
51  this->entityName = entity->getName();
52  this->memory = memoryPrx;
53  this->segment = segmentPrx;
54  this->segmentName = segmentName;
55  this->memoryName = memoryName;
56  }
57 
58  EntityRef::EntityRef(const EntityRef& entityRef) :
59  IceUtil::Shared(entityRef), EntityRefBase(entityRef)
60  {
61  this->entityId = entityRef.entityId;
62  this->segment = entityRef.segment;
63  this->segmentName = entityRef.segmentName;
64  this->memory = entityRef.memory;
65  this->memoryName = entityRef.memoryName;
66  this->entityName = entityRef.entityName;
67  }
68 
69  void
70  memoryx::EntityRef::serialize(const armarx::ObjectSerializerBasePtr& serializer,
71  const Ice::Current& c) const
72  {
74  armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
75 
76  obj->setString("entityId", entityId);
77  obj->setString("entityName", entityName);
78  obj->setString("segmentName", this->segmentName);
79  obj->setString("memoryName", this->memoryName);
80  }
81 
82  void
83  memoryx::EntityRef::deserialize(const armarx::ObjectSerializerBasePtr& serializer,
84  const Ice::Current& c)
85  {
87  armarx::AbstractObjectSerializerPtr::dynamicCast(serializer);
88  entityId = obj->getString("entityId");
89  entityName = obj->getString("entityName");
90  memoryName = obj->getString("memoryName");
91  segmentName = obj->getString("segmentName");
92 
93 
94  if (c.adapter && c.adapter->getCommunicator())
95  {
96  memory = MemoryInterfacePrx::checkedCast(
97  c.adapter->getCommunicator()->stringToProxy(memoryName));
98 
99  if (memory)
100  {
101  segment = memoryx::EntityMemorySegmentInterfacePrx::checkedCast(
102  memory->getSegment(segmentName));
103  }
104  else
105  {
106  ARMARX_ERROR_S << "cannot create proxy to memory " << memoryName;
107  }
108 
109  if (!segment)
110  {
111  ARMARX_ERROR_S << "cannot create proxy to segment " << segmentName << " in memory "
112  << memoryName;
113  }
114  }
115  else
116  {
117  ARMARX_ERROR_S << "adapter or communicator zero - cannot create proxy to segment "
118  << segmentName;
119  }
120  }
121 
122  armarx::VariantDataClassPtr
123  memoryx::EntityRef::clone(const Ice::Current& c) const
124  {
125  return new EntityRef(*this);
126  }
127 
130  {
131  return this->clone();
132  }
133 
134  std::string
135  memoryx::EntityRef::output(const Ice::Current&) const
136  {
137  return entityName + "_" + entityId + " in segment (" + segmentName + ")";
138  }
139 
140  Ice::Int
141  memoryx::EntityRef::getType(const Ice::Current&) const
142  {
144  }
145 
146  bool
147  memoryx::EntityRef::validate(const Ice::Current&)
148  {
149  return true;
150  }
151 
152  EntityBasePtr
153  memoryx::EntityRef::getEntity(const Ice::Current&) const
154  {
155  if (segment)
156  {
157  return segment->getEntityById(entityId);
158  }
159  else
160  {
161  return nullptr;
162  }
163  }
164 
165  //"hey", you may say, "can't you just define operator== on EntityRefBase in memoryx/global ns?",
166  //but unfortunately Ice already defines those in the memx ns and it takes precedence over the global definitions
167  bool
168  EntityRef::equals(const EntityRefBasePtr& other, const Ice::Current&) const
169  {
170  return entityId == other->entityId && segmentName == other->segmentName &&
171  memoryName == other->memoryName;
172  }
173 } // namespace memoryx
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:83
memoryx::EntityRef::serialize
void serialize(const armarx::ObjectSerializerBasePtr &serializer, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: EntityRef.cpp:70
memoryx::EntityRef::equals
bool equals(const EntityRefBasePtr &other, const Ice::Current &c=Ice::emptyCurrent) const override
Definition: EntityRef.cpp:168
AbstractObjectSerializer.h
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:46
memoryx::EntityRef::getEntity
EntityBasePtr getEntity(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: EntityRef.cpp:153
memoryx::EntityRef::getType
Ice::Int getType(const Ice::Current &c=Ice::emptyCurrent) const override
Definition: EntityRef.cpp:141
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:46
IceInternal::Handle
Definition: forward_declarations.h:8
EntityRef.h
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:216
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:123
memoryx::EntityRef::ice_clone
Ice::ObjectPtr ice_clone() const override
Definition: EntityRef.cpp:129
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:917
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:135
memoryx::EntityRef::validate
bool validate(const Ice::Current &c=Ice::emptyCurrent) override
Definition: EntityRef.cpp:147