Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

HandleMaker.h

Go to the documentation of this file.
00001 #ifndef TAGCOLL_HANDLE_MAKER_H
00002 #define TAGCOLL_HANDLE_MAKER_H
00003 
00004 /*
00005  * Associate integer handles to strings or other kinds of items
00006  *
00007  * Copyright (C) 2003  Enrico Zini <enrico@debian.org>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
00022  */
00023 
00024 #include <map>
00025 
00026 namespace Tagcoll
00027 {
00028 
00029 template <class T>
00030 class HandleMaker
00031 {
00032     std::map<int, T> handles_to_items;
00033     std::map<T, int> items_to_handles;
00034     int handle_seq;
00035     
00036 public:
00037     HandleMaker() throw () : handle_seq(0) {}
00038 
00039     int getHandle(T item) throw ()
00040     {
00041         typename std::map<T, int>::iterator i = items_to_handles.find(item);
00042         if (i == items_to_handles.end())
00043         {
00044             int res = handle_seq++;
00045             items_to_handles.insert(std::make_pair(item, res));
00046             handles_to_items.insert(std::make_pair(res, item));
00047             return res;
00048         } else
00049             return i->second;
00050     }
00051 
00052     T getItem(int handle) const throw ()
00053     {
00054         typename std::map<int, T>::const_iterator i = handles_to_items.find(handle);
00055         //assert(i != handles_to_items.end());
00056         return i->second;
00057     }
00058 };
00059 
00060 };
00061 
00062 // vim:set ts=4 sw=4:
00063 #endif

Generated on Sun Jan 23 02:09:10 2005 for libtagcoll by  doxygen 1.4.0