object_pool.hpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */// Object pool, a template
00025 
00026 #ifndef ACADO_TOOLKIT_OBJECT_POOL
00027 #define ACADO_TOOLKIT_OBJECT_POOL
00028 
00029 #include <map>
00030 #include <vector>
00031 #include <utility>
00032 
00034 template
00035 <
00037         typename T,
00039         typename C = std::less< T >
00040 >
00041 class ObjectPool
00042 {
00043 public:
00045         ObjectPool()
00046         {}
00047         
00049         ~ObjectPool()
00050         {}
00051         
00057         bool add(const T& obj)
00058         {
00059                 typename poolMap::const_iterator it = pool.find( obj );
00060                 if (it == pool.end())
00061                 {
00062                         pool.insert( std::make_pair(obj, true) );
00063 
00064                         return true;
00065                 }
00066 
00067                 return false;
00068         }
00069 
00070         bool busy( void )
00071         {
00072                 if (pool.size() == 0)
00073                         return true;
00074 
00075                 typename poolMap::const_iterator it = pool.begin();
00076                 for (; it != pool.end(); ++it)
00077                         if (it->second == false)
00078                                 return false;
00079 
00080                 return true;
00081         }
00082 
00087         bool acquire(T& obj)
00088         {
00089                 if ( pool.size() )
00090                 {
00091                         typename poolMap::iterator it = pool.begin();
00092                         for (; it != pool.end(); ++it)
00093                         {
00094                                 if (it->second == false)
00095                                         break;
00096                         }
00097                         
00098                         if (it != pool.end())
00099                         {
00100                                 obj = it->first;
00101                                 it->second = true;
00102 
00103                                 return true;
00104                         }
00105                 }       
00106                         
00107                 return false;
00108         }
00109         
00111         bool release(const T& obj)
00112         {
00113                 typename poolMap::iterator it = pool.find( obj );
00114                 if (it != pool.end())
00115                 {
00116                         it->second = false;
00117 
00118                         return true;
00119                 }
00120 
00121                 return false;
00122         }
00123         
00125         std::vector< T > getPool() const
00126         {
00127                 std::vector< T > v;
00128 
00129                 typename poolMap::const_iterator it = pool.begin();
00130                 for (; it != pool.end(); ++it)
00131                         v.push_back( it->first );
00132 
00133                 return v;
00134         }
00135 
00137         unsigned size( void )
00138         {
00139                 return pool.size();
00140         }
00141 
00142 private:
00143         typedef std::map<T, bool, C> poolMap;
00144 
00145         poolMap pool;
00146 };
00147 
00148 #endif // ACADO_TOOLKIT_OBJECT_POOL


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:59:22