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