// -*- mode: C++; tab-width: 4; indent-tabs-mode: t; -*- vim:ts=4:sw=4 // // Copyright (C) 2004-2005 David Lau (skunix) // Chong Kai Xiong (descender) // // This file is part of The Plains of Usata. // // The Plains of Usata is licensed under the GNU General Public // License (GPL) version 2. For details, please see the COPYING file // included in the software distribution, or visit // http://www.fsf.org/licenses/gpl.html. // // $Id: gproperties.hpp,v 1.1 2005/02/02 20:52:04 skunix Exp $ #ifndef USATA_GPROPERTIES_HPP #define USATA_GPROPERTIES_HPP #include #include #include namespace usata { template > class GProperties { typedef TKey key_type; struct value_type { key_type key; boost::any value; template value_type(const key_type& k, const Tdata& data) : key(k), value(data){} bool operator<(const value_type& rhs) const { return Tcompare()(key, rhs.key); } bool operator< (const key_type&_key) const { return Tcompare()(key, _key); } }; typedef std::vector storage_type; typedef typename storage_type::iterator storage_iter; storage_type mData; bool mNeed_sort; void sort() { if (mNeed_sort) std::sort(mData.begin(), mData.end(), std::less()); } public: GProperties(size_t reserve=0) {} template void add(const key_type& key,const T& data) { mNeed_sort = true; mData.push_back(value_type(key,data)); } boost::any& retr(const key_type& key) { sort(); storage_iter it( std::lower_bound(mData.begin(), mData.end(), key)); if ((it == mData.end()) || (it->key != key)) throw 1; return it->value; } template T get(key_type _key) { boost::any& v = retr(_key); return boost::any_cast(v); } template T get(key_type _key, T _default) { try { return get(_key); } catch (boost::bad_any_cast) {} catch (int) {} return _default; } }; } #endif