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 
55  ARMARX_INFO << "Cloning robot";
56  auto newRobot = baseRobot->clone(baseRobot->getName(), VirtualRobot::CollisionCheckerPtr(new VirtualRobot::CollisionChecker()));
57  newRobot->inflateCollisionModel(inflation);
58  ARMARX_CHECK_EQUAL(newRobot.use_count(), 1);
59  ARMARX_INFO << "created new robot clone n with inflation " << inflation;
60  robots[inflation].push_back(newRobot);
61  return newRobot;
62  }
63 
64  size_t RobotPool::getPoolSize() const
65  {
66  std::scoped_lock lock(mutex);
67  size_t size = 0;
68  for (auto& pair : robots)
69  {
70  size += pair.second.size();
71  }
72  return size;
73  }
74 
76  {
77  std::scoped_lock lock(mutex);
78  size_t count = 0;
79  for (auto& pair : robots)
80  {
81  for (auto& r : pair.second)
82  {
83  if (r.use_count() > 1)
84  {
85  count++;
86  }
87  }
88  }
89  return count;
90  }
91 
93  {
94  std::scoped_lock lock(mutex);
95  size_t count = 0;
96  for (auto& pair : robots)
97  {
98  std::vector<VirtualRobot::RobotPtr> newList;
99  for (auto& r : pair.second)
100  {
101  if (r.use_count() > 1)
102  {
103  newList.push_back(r);
104  }
105  else
106  {
107  count++;
108  }
109  }
110  pair.second = newList;
111  }
112  return count;
113  }
114 
115 } // 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:64
armarx::RobotPool::clean
size_t clean()
Removes unused robots from the pool.
Definition: RobotPool.cpp:92
ExpressionException.h
armarx::RobotPool::getRobot
VirtualRobot::RobotPtr getRobot(int inflation=0)
getRobot
Definition: RobotPool.cpp:44
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
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:75
VirtualRobot::RobotPtr
std::shared_ptr< class Robot > RobotPtr
Definition: Bus.h:18
RobotPool.h