csutil/comparator.h
Go to the documentation of this file.00001 /* 00002 Template providing various comparison and ordering functions. 00003 Copyright (C) 2005 by Eric Sunshine 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library 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 GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 #ifndef __CSUTIL_COMPARATOR_H__ 00020 #define __CSUTIL_COMPARATOR_H__ 00021 00026 #include "csstring.h" 00027 00034 template <class T1, class T2> 00035 class csComparator 00036 { 00037 public: 00054 static int Compare(T1 const &r1, T2 const &r2) 00055 { 00056 if (r1 < r2) return -1; 00057 else if (r2 < r1) return 1; 00058 else return 0; 00059 } 00060 }; 00061 00071 template <class T> 00072 class csComparatorString 00073 { 00074 public: 00075 static int Compare (T const& r1, T const& r2) 00076 { 00077 return strcmp ((const char*)r1, (const char*)r2); 00078 } 00079 }; 00080 00084 CS_SPECIALIZE_TEMPLATE 00085 class csComparator<const char*, const char*> : 00086 public csComparatorString<const char*> {}; 00087 00092 CS_SPECIALIZE_TEMPLATE 00093 class csComparator<csString, csString> : 00094 public csComparatorString<csString> {}; 00095 CS_SPECIALIZE_TEMPLATE 00096 class csComparator<csStringBase, csStringBase> : 00097 public csComparatorString<csStringBase> {}; 00109 template<class T> 00110 class csComparatorStruct 00111 { 00112 public: 00113 static int Compare (T const& r1, T const& r2) 00114 { 00115 return memcmp (&r1, &r2, sizeof (T)); 00116 } 00117 }; 00118 00121 #endif // __CSUTIL_COMPARATOR_H__
Generated for Crystal Space by doxygen 1.4.6