SegmentedMemory.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 MemoryX::Core
17 * @author ALexey Kozlov ( kozlov at kit dot edu)
18 * @date 2013
19 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20 * GNU General Public License
21 */
22 
23 #include <mutex>
24 
25 #include "SegmentedMemory.h"
26 
27 #include <ArmarXCore/interface/core/Log.h>
29 
30 namespace memoryx
31 {
33  {
34  }
35 
37  {
38  }
39 
40  AbstractMemorySegmentPrx SegmentedMemory::addSegment(const std::string& segmentName, const AbstractMemorySegmentPtr& segment, const ::Ice::Current& c)
41  {
42  std::unique_lock lock(segmentsMutex);
43  // check if a segment with the same name already exists in memory
44  MemorySegmentMap::const_iterator it = segments.find(segmentName);
45 
46  if (it != segments.end())
47  {
48  throw SegmentAlreadyExistsException();
49  }
50 
51  // assign name
52  segment->setSegmentName(segmentName);
53  segment->setParentMemory(this);
54 
55  auto id = segment->getIceId();
56  // register proxy
57  AbstractMemorySegmentPrx segmentProxy = AbstractMemorySegmentPrx::uncheckedCast(getObjectAdapter()->add(segment, id));
58 
59  // insert into known segments
60  MemorySegmentEntry segmentEntry;
61  segmentEntry.proxy = segmentProxy;
62  segmentEntry.pointer = segment;
63 
64  segments[segmentName] = segmentEntry;
65 
66  return segmentProxy;
67  }
68 
69  bool SegmentedMemory::hasSegment(const std::string& segmentName, const ::Ice::Current& c) const
70  {
71  std::unique_lock lock(segmentsMutex);
72  return segments.find(segmentName) != segments.end();
73  }
74 
75  AbstractMemorySegmentPrx SegmentedMemory::getSegment(const std::string& segmentName, const ::Ice::Current& c) const
76  {
77  std::unique_lock lock(segmentsMutex);
78 
79  // find segment
80  MemorySegmentMap::const_iterator iter = segments.find(segmentName);
81 
82  if (iter == segments.end())
83  {
84  ARMARX_ERROR_S << "Could not find segment with name " << segmentName << std::endl;
85  throw InvalidEntityException();
86  }
87 
88  return iter->second.proxy;
89  }
90 
91  void SegmentedMemory::removeSegment(const std::string& segmentName, const ::Ice::Current& c)
92  {
93  std::unique_lock lock(segmentsMutex);
94 
95  MemorySegmentMap::iterator iter = segments.find(segmentName);
96 
97  if (iter != segments.end())
98  {
99  AbstractMemorySegmentPrx proxy = iter->second.proxy;
100 
101  // remove from adapter
102  Ice::Identity id = proxy->ice_getIdentity();
103  getObjectAdapter()->remove(id);
104 
105  // remove from known segments
106  segments.erase(iter);
107  }
108  }
109 
110  NameList SegmentedMemory::getSegmentNames(const ::Ice::Current&) const
111  {
112  std::unique_lock lock(segmentsMutex);
113 
114  NameList result;
115 
116  for (MemorySegmentMap::const_iterator it = segments.begin(); it != segments.end(); ++it)
117  {
118  result.push_back(it->first);
119  }
120 
121  return result;
122  }
123 
124  AbstractMemorySegmentPtr SegmentedMemory::getSegmentPtr(const std::string& segmentName)
125  {
126  std::unique_lock lock(segmentsMutex);
127 
128  // find segment
129  MemorySegmentMap::iterator iter = segments.find(segmentName);
130 
131  if (iter == segments.end())
132  {
133  ARMARX_ERROR_S << "Could not find segment with name " << segmentName << std::endl;
134  throw InvalidEntityException();
135  }
136 
137  return iter->second.pointer;
138  }
139 
140 
141  EntityBasePtr memoryx::SegmentedMemory::findEntityById(const std::string& entityId, const Ice::Current& c)
142  {
143  NameList names = getSegmentNames();
144 
145  for (auto name : names)
146  {
147  EntityMemorySegmentInterfacePtr segment = memoryx::EntityMemorySegmentInterfacePtr::dynamicCast(getSegmentPtr(name));
148 
149  if (!segment)
150  {
151  continue;
152  }
153 
154  auto entity = segment->getEntityById(entityId);
155 
156  if (entity)
157  {
158  return entity;
159  }
160  }
161 
162  return NULL;
163  }
164 
165 
166  EntityRefBasePtr memoryx::SegmentedMemory::findEntityRefById(const std::string& entityId, const Ice::Current& c)
167  {
168  NameList names = getSegmentNames();
169 
170  for (auto name : names)
171  {
172  EntityMemorySegmentInterfacePtr segment = memoryx::EntityMemorySegmentInterfacePtr::dynamicCast(getSegmentPtr(name));
173 
174  if (!segment)
175  {
176  continue;
177  }
178 
179  auto entity = segment->getEntityRefById(entityId);
180 
181  if (entity)
182  {
183  return entity;
184  }
185  }
186 
187  return NULL;
188  }
189 }
memoryx::SegmentedMemory::segments
MemorySegmentMap segments
Definition: SegmentedMemory.h:68
memoryx::SegmentedMemory::SegmentedMemory
SegmentedMemory()
Definition: SegmentedMemory.cpp:32
SegmentedMemory.h
memoryx::SegmentedMemory::getSegmentPtr
AbstractMemorySegmentPtr getSegmentPtr(const std::string &segmentName)
Definition: SegmentedMemory.cpp:124
memoryx::SegmentedMemory::addSegment
AbstractMemorySegmentPrx addSegment(const std::string &segmentName, const AbstractMemorySegmentPtr &segment, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: SegmentedMemory.cpp:40
memoryx::SegmentedMemory::segmentsMutex
std::shared_mutex segmentsMutex
Definition: SegmentedMemory.h:69
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
memoryx::SegmentedMemory::getObjectAdapter
virtual Ice::ObjectAdapterPtr getObjectAdapter() const =0
memoryx::SegmentedMemory::hasSegment
bool hasSegment(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: SegmentedMemory.cpp:69
memoryx::MemorySegmentEntry::proxy
AbstractMemorySegmentPrx proxy
Definition: SegmentedMemory.h:37
GfxTL::Identity
void Identity(MatrixXX< N, N, T > *a)
Definition: MatrixXX.h:523
memoryx::SegmentedMemory::findEntityRefById
EntityRefBasePtr findEntityRefById(const std::string &entityId, const Ice::Current &c=Ice::emptyCurrent) override
Definition: SegmentedMemory.cpp:166
memoryx::MemorySegmentEntry
Definition: SegmentedMemory.h:35
memoryx::SegmentedMemory::findEntityById
EntityBasePtr findEntityById(const std::string &entityId, const Ice::Current &c=Ice::emptyCurrent) override
Definition: SegmentedMemory.cpp:141
ARMARX_ERROR_S
#define ARMARX_ERROR_S
Definition: Logging.h:209
Component.h
memoryx::MemorySegmentEntry::pointer
AbstractMemorySegmentPtr pointer
Definition: SegmentedMemory.h:38
armarx::viz::data::ElementFlags::names
const simox::meta::IntEnumNames names
Definition: json_elements.cpp:14
memoryx::SegmentedMemory::removeSegment
void removeSegment(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) override
Definition: SegmentedMemory.cpp:91
memoryx::SegmentedMemory::getSegment
AbstractMemorySegmentPrx getSegment(const std::string &segmentName, const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: SegmentedMemory.cpp:75
memoryx::SegmentedMemory::~SegmentedMemory
~SegmentedMemory() override
Definition: SegmentedMemory.cpp:36
memoryx::SegmentedMemory::getSegmentNames
NameList getSegmentNames(const ::Ice::Current &=Ice::emptyCurrent) const override
Definition: SegmentedMemory.cpp:110