Stl.hh

00001 /*
00002 ** TowBowlTactics, a turn-based strategy football game.
00003 ** 
00004 ** Copyright (C) 2006-2010 The TBT Team.
00005 ** 
00006 ** This program is free software; you can redistribute it and/or
00007 ** modify it under the terms of the GNU General Public License
00008 ** as published by the Free Software Foundation; either version 2
00009 ** of the License, or (at your option) any later version.
00010 ** 
00011 ** The complete GNU General Public Licence Notice can be found as the
00012 ** `NOTICE' file in the root directory.
00013 ** 
00014 ** The TBT Team consists of people listed in the `AUTHORS' file.
00015 */
00016 
00017 #ifndef STL_HH_
00018 # define STL_HH_
00019 
00021 struct Deleter
00022 {
00023   template<class T>
00024   T* operator()(T* x)
00025   {
00026     delete x;
00027     return 0;
00028   }
00029 };
00030 
00032 struct ArrayDeleter
00033 {
00034   template<class T>
00035   T* operator()(T* x)
00036   {
00037     delete[] x;
00038     return 0;
00039   }
00040 };
00041 
00045 inline std::string trim(const std::string& s)
00046 {
00047   if (s.length() == 0)
00048     return s;
00049   int b = s.find_first_not_of(" \t");
00050   int e = s.find_last_not_of(" \t");
00051   if (b == -1) // No non-spaces
00052     return "";
00053   return std::string(s, b, e - b + 1);
00054 }
00055 
00059 inline void trimAll(std::string& s)
00060 {
00061   s.erase(std::remove(s.begin(), s.end(), ' '), s.end());
00062 }
00063 
00064 struct ToLower
00065 {
00066   char operator() (char c) const
00067   {
00068     return std::tolower(static_cast<unsigned char>(c));
00069   }
00070 };
00071 
00075 inline void toLower(std::string& s)
00076 {
00077   std::transform(s.begin(), s.end(), s.begin(), ToLower());
00078 }
00079 
00082 template <typename Collection, typename Function>
00083 inline Function for_all(Collection &c, const Function &f) 
00084 {
00085   return std::for_each(c.begin(), c.end(), f);
00086 }
00087 
00088 #endif /* !STL_HH_ */
Generated on Mon Apr 5 21:17:13 2010 for Stechec/TBT by  doxygen 1.6.3