CPPSERV


Home Projects Jobs Clientele Contact
CPPSERV Documentation Download TODO Mailing lists Bug tracker News RSS Feed Browse source

util.h

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2004 by Ilya A. Volynets-Evenbakh                       *
00003  *   ilya@total-knowledge.com                                              *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 #ifndef CSERVUTIL_H
00021 #define CSERVUTIL_H
00022 
00023 #include <string>
00024 #include <list>
00025 #include <vector>
00026 #include <map>
00027 #include <memory>
00028 #include <boost/shared_ptr.hpp>
00029 #include <algorithm>
00030 #include <ext/functional>
00031 //#include <ext/hash_fun.h>
00032 
00033 namespace container {
00034 namespace util {
00035 
00042 struct TrEq
00043 {
00044     char operator() (char c) const  { return (c!='=' && c!='/')?c:'0'; }
00045 };
00046 typedef std::pair<std::string, std::string> nvpair;
00047 typedef std::list<nvpair> pairlist_t;
00048 typedef std::map<std::string,boost::shared_ptr<void> > attr_t;
00049 typedef std::map<std::string,std::string> param_t;
00050 typedef std::multimap<std::string,std::string> mparam_t;
00051 unsigned long getRandomLong();
00052 std::string getRandomString(int len);
00053 
00054 template <class T>
00055  std::auto_ptr< std::vector<std::string> > getMapKeyNames(const std::map<std::string,T>& map)
00056 {
00057     std::auto_ptr<std::vector<std::string> > ret(new std::vector<std::string>);
00058     std::back_insert_iterator<std::vector<std::string> > ii(*ret);
00059     std::transform(map.begin(), map.end(), ii, __gnu_cxx::select1st<std::pair<std::string, T> >());
00060     return ret;
00061 }
00062 
00063 template <class T>
00064 struct GetMKey: public std::unary_function<const std::pair<std::string, T >&,void>
00065 {
00066     std::vector<std::string>& m_out;
00067     std::string m_last;
00068     GetMKey(std::vector<std::string>& out):m_out(out){}
00069     void operator()(const std::pair<std::string, T >& p)
00070     {
00071         if( m_last != p.first)
00072             m_out.push_back(p.first);
00073         m_last=p.first;
00074     }
00075 };
00076 
00077 template <class T>
00078  std::auto_ptr< std::vector<std::string> > getMapKeyNames(const std::multimap<std::string,T>& map)
00079 {
00080     std::auto_ptr<std::vector<std::string> > ret(new std::vector<std::string>);
00081     GetMKey<T> getKey(*ret);
00082     std::for_each(map.begin(), map.end(), getKey);
00083     return ret;
00084 }
00085 
00086 template <typename K, typename V>
00087  std::auto_ptr< std::vector<V> > getMapValues(const std::multimap<K, V>& map, const K& name)
00088 {
00089     std::auto_ptr<std::vector<V> > ret(new std::vector<V>);
00090     std::back_insert_iterator<std::vector<V> > ii(*ret);
00091     transform(map.lower_bound(name), map.upper_bound(name), ii, __gnu_cxx::select2nd<std::pair<K, V> >());
00092     return ret;
00093 }
00094 
00095 }
00096 }
00097 #endif

SourceForge.net Logo