object_pool.hpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */// Object pool, a template
25 
26 #ifndef ACADO_TOOLKIT_OBJECT_POOL
27 #define ACADO_TOOLKIT_OBJECT_POOL
28 
29 #include <map>
30 #include <vector>
31 #include <utility>
32 
34 template
35 <
37  typename T,
39  typename C = std::less< T >
40 >
42 {
43 public:
46  {}
47 
50  {}
51 
57  bool add(const T& obj)
58  {
59  typename poolMap::const_iterator it = pool.find( obj );
60  if (it == pool.end())
61  {
62  pool.insert( std::make_pair(obj, true) );
63 
64  return true;
65  }
66 
67  return false;
68  }
69 
70  bool busy( void )
71  {
72  if (pool.size() == 0)
73  return true;
74 
75  typename poolMap::const_iterator it = pool.begin();
76  for (; it != pool.end(); ++it)
77  if (it->second == false)
78  return false;
79 
80  return true;
81  }
82 
87  bool acquire(T& obj)
88  {
89  if ( pool.size() )
90  {
91  typename poolMap::iterator it = pool.begin();
92  for (; it != pool.end(); ++it)
93  {
94  if (it->second == false)
95  break;
96  }
97 
98  if (it != pool.end())
99  {
100  obj = it->first;
101  it->second = true;
102 
103  return true;
104  }
105  }
106 
107  return false;
108  }
109 
111  bool release(const T& obj)
112  {
113  typename poolMap::iterator it = pool.find( obj );
114  if (it != pool.end())
115  {
116  it->second = false;
117 
118  return true;
119  }
120 
121  return false;
122  }
123 
125  std::vector< T > getPool() const
126  {
127  std::vector< T > v;
128 
129  typename poolMap::const_iterator it = pool.begin();
130  for (; it != pool.end(); ++it)
131  v.push_back( it->first );
132 
133  return v;
134  }
135 
137  unsigned size( void )
138  {
139  return pool.size();
140  }
141 
142 private:
143  typedef std::map<T, bool, C> poolMap;
144 
145  poolMap pool;
146 };
147 
148 #endif // ACADO_TOOLKIT_OBJECT_POOL
unsigned size(void)
USING_NAMESPACE_ACADO typedef TaylorVariable< Interval > T
bool busy(void)
Definition: object_pool.hpp:70
bool acquire(T &obj)
Definition: object_pool.hpp:87
poolMap pool
std::vector< T > getPool() const
std::map< T, bool, C > poolMap
bool add(const T &obj)
Definition: object_pool.hpp:57
#define v
bool release(const T &obj)


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:54