| ## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# 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 class csString;
    00027 class csStringBase;
    00028
    00035 template <class T1, class T2>
    00036 class csComparator
    00037 {
    00038 public:
    00055   static int Compare(T1 const &r1, T2 const &r2)
    00056   {
    00057     if (r1 < r2) return -1;
    00058     else if (r2 < r1) return 1;
    00059     else return 0;
    00060   }
    00061 };
    00062
    00072 template <class T>
    00073 class csComparatorString
    00074 {
    00075 public:
    00076   static int Compare (T const& r1, T const& r2)
    00077   {
    00078     if (((const char*)r1) == 0)
    00079     {
    00080       return (((const char*)r2) == 0) ? 0 : 1;
    00081     }
    00082     else if (((const char*)r2) == 0)
    00083     {
    00084       return -1;
    00085     }
    00086     return strcmp ((const char*)r1, (const char*)r2);
    00087   }
    00088 };
    00089
    00093 template<>
    00094 class csComparator<const char*, const char*> :
    00095   public csComparatorString<const char*> {};
    00096
    00101 template<>
    00102 class csComparator<csString, csString> :
    00103   public csComparatorString<csString> {};
    00104 template<>
    00105 class csComparator<csStringBase, csStringBase> :
    00106   public csComparatorString<csStringBase> {};
    00118 template<class T>
    00119 class csComparatorStruct
    00120 {
    00121 public:
    00122   static int Compare (T const& r1, T const& r2)
    00123   {
    00124     return memcmp (&r1, &r2, sizeof (T));
    00125   }
    00126 };
    00127
    00130 #endif // __CSUTIL_COMPARATOR_H__