# HandleManager.hh

Go to the documentation of this file.

    00001 // ----------------------------------------------------------------------------
    00002 // CERTI - HLA RunTime Infrastructure
    00003 // Copyright (C) 2002-2005  ONERA
    00004 //
    00005 // This program is free software ; you can redistribute it and/or
    00006 // modify it under the terms of the GNU Lesser General Public License
    00007 // as published by the Free Software Foundation ; either version 2 of
    00008 // the License, or (at your option) any later version.
    00009 //
    00010 // This program is distributed in the hope that it will be useful, but
    00011 // WITHOUT ANY WARRANTY ; without even the implied warranty of
    00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
    00013 // Lesser General Public License for more details.
    00014 //
    00015 // You should have received a copy of the GNU Lesser General Public
    00016 // License along with this program ; if not, write to the Free Software
    00017 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
    00018 // USA
    00019 //
    00020 // $Id: HandleManager.hh,v 3.6 2008/11/01 21:39:53 erk Exp $
    00021 // ----------------------------------------------------------------------------
    00022
    00023 #ifndef LIBCERTI_HANDLE_MANAGER
    00024 #define LIBCERTI_HANDLE_MANAGER
    00025
    00026 #include "certi.hh"
    00027
    00028 #include <limits>
    00029
    00030 #ifdef _WIN32
    00031 #ifdef max
    00032 #undef max
    00033 #endif
    00034 #endif
    00035
    00036 namespace certi {
    00037
    00042 template<typename T>
    00043 class HandleManager
    00044 {
    00045 public:
    00052     HandleManager(T);
    00058     HandleManager(T init, size_t hmax);
    00064     T provide() throw (RTIinternalError);
    00071     void free(T handle);
    00072
    00073 private:
    00074     size_t maximum ;
    00075     T highest ;
    00076     std::list<T> available ;
    00077 };
    00078
    00079
    00080 template<typename T>
    00081 HandleManager<T>::HandleManager(T init)
    00082 : maximum(std::numeric_limits<T>::max()), highest(init) { }
    00083
    00084 template<typename T>
    00085 HandleManager<T>::HandleManager(T init, size_t hmax)
    00086 : highest(init), maximum(hmax) { }
    00087
    00088 template<typename T> T
    00089 HandleManager<T>::provide() throw (RTIinternalError)
    00090 {
    00091     T handle = 0 ;
    00092
    00093     if (available.size() > 0) {
    00094         handle = available.front();
    00095         available.pop_front();
    00096     }
    00097     else {
    00098         if (highest < maximum)
    00099             handle = highest++ ;
    00100         else
    00101             throw RTIinternalError("Maximum handle reached");
    00102     }
    00103
    00104     return handle ;
    00105 } /* end of provide */
    00106
    00107 template<typename T> void
    00108 HandleManager<T>::free(T handle)
    00109 {
    00110     //if (handle + 1 == highest)
    00111     //--highest ;
    00112     //else
    00113     //available.push_back(handle);
    00114 }
    00115
    00116 } // certi
    00117
    00118 #endif // LIBCERTI_HANDLE_MANAGER