cstool/identstrings.h
Go to the documentation of this file.00001 /* 00002 Copyright (C) 2004 by Jorrit Tyberghein 00003 (C) 2004 by Frank Richter 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 00020 #ifndef __CS_CSTOOL_IDENTSTRINGS_H__ 00021 #define __CS_CSTOOL_IDENTSTRINGS_H__ 00022 00023 #include "csutil/csstring.h" 00024 00032 class csIdentStrings 00033 { 00034 public: 00035 struct csIdentString 00036 { 00037 int ident; 00038 const char* str; 00039 }; 00040 protected: 00041 const csIdentString* strings; 00042 size_t stringCount; 00043 csString* scratch; 00044 public: 00045 csIdentStrings (const csIdentString* str, csString* scratch, size_t cnt) : 00046 strings(str), stringCount(cnt), scratch(scratch) { } 00052 const char* StringForIdent (int ident) 00053 { 00054 size_t l = 0, r = stringCount; 00055 while (l < r) 00056 { 00057 size_t m = (l+r) / 2; 00058 if (strings[m].ident == ident) 00059 { 00060 return strings[m].str; 00061 } 00062 else if (strings[m].ident < ident) 00063 { 00064 l = m + 1; 00065 } 00066 else 00067 { 00068 r = m; 00069 } 00070 } 00071 scratch->Format ("%d", ident); 00072 return scratch->GetData(); 00073 } 00074 }; 00075 00092 #define CS_IDENT_STRING_LIST(ListName) \ 00093 static const csIdentStrings::csIdentString ListName##_strings[] = { 00094 00098 #define CS_IDENT_STRING(Ident) {Ident, #Ident}, 00099 00103 #define CS_IDENT_STRING_EXPLICIT(Ident, Str) {Ident, str}, 00104 00107 #define CS_IDENT_STRING_LIST_END(ListName) \ 00108 {0, 0} \ 00109 }; \ 00110 CS_IMPLEMENT_STATIC_VAR(Get##ListName##Scratch, csString, ()) \ 00111 static csIdentStrings ListName (ListName##_strings, \ 00112 Get##ListName##Scratch(), \ 00113 (sizeof (ListName##_strings) / sizeof (csIdentStrings::csIdentString)) - 1); 00114 00115 #endif // __CS_CSTOOL_IDENTSTRINGS_H__
Generated for Crystal Space by doxygen 1.4.7