RobotPool.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2017, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarX
19  * @author Mirko Waechter( mirko.waechter at kit dot edu)
20  * @date 2018
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 #include "RobotPool.h"
25 
26 #include <VirtualRobot/CollisionDetection/CollisionChecker.h>
27 
30 
31 namespace armarx
32 {
33 
34  RobotPool::RobotPool(VirtualRobot::RobotPtr robot, size_t defaultSize) :
35  baseRobot(robot)
36  {
37  std::vector<VirtualRobot::RobotPtr> tempVec;
38  for (size_t i = 0; i < defaultSize; ++i)
39  {
40  tempVec.push_back(getRobot());
41  }
42  }
43 
45  {
46  std::scoped_lock lock(mutex);
47  for (auto& r : robots[inflation])
48  {
49  if (r.use_count() == 1)
50  {
51  return r;
52  }
53  }
54  auto newRobot = baseRobot->clone(baseRobot->getName(), VirtualRobot::CollisionCheckerPtr(new VirtualRobot::CollisionChecker()));
55  newRobot->inflateCollisionModel(inflation);
56  ARMARX_CHECK_EQUAL(newRobot.use_count(), 1);
57  ARMARX_DEBUG << "created new robot clone n with inflation " << inflation;
58  robots[inflation].push_back(newRobot);
59  return newRobot;
60  }
61 
62  size_t RobotPool::getPoolSize() const
63  {
64  std::scoped_lock lock(mutex);
65  size_t size = 0;
66  for (auto& pair : robots)
67  {
68  size += pair.second.size();
69  }
70  return size;
71  }
72 
74  {
75  std::scoped_lock lock(mutex);
76  size_t count = 0;
77  for (auto& pair : robots)
78  {
79  for (auto& r : pair.second)
80  {
81  if (r.use_count() > 1)
82  {
83  count++;
84  }
85  }
86  }
87  return count;
88  }
89 
91  {
92  std::scoped_lock lock(mutex);
93  size_t count = 0;
94  for (auto& pair : robots)
95  {
96  std::vector<VirtualRobot::RobotPtr> newList;
97  for (auto& r : pair.second)
98  {
99  if (r.use_count() > 1)
100  {
101  newList.push_back(r);
102  }
103  else
104  {
105  count++;
106  }
107  }
108  pair.second = newList;
109  }
110  return count;
111  }
112 
113 } // namespace armarx
armarx::RobotPool::RobotPool
RobotPool(VirtualRobot::RobotPtr robot, size_t defaultSize=1)
Definition: RobotPool.cpp:34
armarx::RobotPool::getPoolSize
size_t getPoolSize() const
Definition: RobotPool.cpp:62
armarx::RobotPool::clean
size_t clean()
Removes unused robots from the pool.
Definition: RobotPool.cpp:90
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
ExpressionException.h
armarx::RobotPool::getRobot
VirtualRobot::RobotPtr getRobot(int inflation=0)
getRobot
Definition: RobotPool.cpp:44
Logging.h
ARMARX_CHECK_EQUAL
#define ARMARX_CHECK_EQUAL(lhs, rhs)
This macro evaluates whether lhs is equal (==) rhs and if it turns out to be false it will throw an E...
Definition: ExpressionException.h:130
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::RobotPool::getRobotsInUseCount
size_t getRobotsInUseCount() const
Definition: RobotPool.cpp:73
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
RobotPool.h